Fixed issue with not bitshifting the clock prescaler bitmap

This commit is contained in:
jakeg00dwin 2024-08-05 15:37:43 -07:00
parent 6d73c2e0f9
commit 2eb2253571
2 changed files with 66 additions and 17 deletions

View File

@ -13,24 +13,72 @@
#endif #endif
#include "timer.h" #include "timer.h"
#include "avr/io.h" #include <avr/io.h>
#include <avr/interrupt.h>
void Timer_SelectClock(void) #define FCLK_PER 3333333UL
#define DIV8 0x3
#define PERIOD_VALUE 40
//These are expiremential found values that account for overhead
//for smaller durations.
#define OVERHEAD_ONE 226
#define OVERHEAD_TWO 151
#define OVERHEAD_THREE 75
static uint16_t overflow_count = 0;
uint16_t Timer_GetOverflowCount(void)
{ {
//set the timer to divde the clock by 256 return overflow_count;
//Check datasheet page: 189
TCA0.SINGLE.CTRLA |= 0x6;
} }
void Timer_Enable(void) void Timer_Start(void)
{ {
TCA0.SINGLE.CTRLA |= (1<<0); //clear the overflow event count
} overflow_count = 0;
void Timer_Period(void) sei();
{
TCA0.SINGLE.PER = 10000; //Enable the overflow Interrupt.
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm;
//set Normal mode.
TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc;
//Disable event counting.
TCA0.SINGLE.EVCTRL &= ~(TCA_SINGLE_CNTEI_bm);
//Set the Period Value
TCA0.SINGLE.PER = PERIOD_VALUE;
//set the Timer to divide FCLK_PER by 8.
TCA0.SINGLE.CTRLA |= (DIV8<<1);
//Enable the Timer
TCA0.SINGLE.CTRLA |= TCA_SINGLE_ENABLE_bm;
} }
void Timer_Disable(void)
{
cli();
TCA0.SINGLE.CTRLA &= ~(1<<0);
TCA0.SINGLE.CTRLESET |= ((0x3)<<2);
}
//Triggered on the overflow of the timer A's counter.
ISR(TCA0_OVF_vect)
{
cli();
//Increment the Overflow counter.
overflow_count += 1;
// The interrupt flag has to be cleared manually
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm;
sei();
}

View File

@ -10,6 +10,7 @@
#ifndef TIMER #ifndef TIMER
#define TIMER #define TIMER
#include "stdbool.h"
#include "inttypes.h" #include "inttypes.h"
/** /**
@ -17,11 +18,11 @@
* @param a The first argument * @param a The first argument
*/ */
/**
* The timer select clock function sets the divider to 256 assuming a void Timer_Start(void);
* default clock diver of 6 is applied to the 20MHz crystal. This gives a
* 13020.8333 Hz or approximatly 13.02Khz tick rate. void Timer_Disable(void);
*/
void Timer_SelectClock(void); uint16_t Timer_GetOverflowCount(void);
#endif // TIMER #endif // TIMER