Relay_Tester/src/timer/timer.c

84 lines
1.6 KiB
C

/**
* @brief PUT_TEXT_HERE
* @details This file is...
* @author username
* @date todays_date
* @copyright None
* @file module_name.h
*/
// Used during testing and for the LSP
#ifndef __AVR_ATtiny404__
#define __AVR_ATtiny404__
#endif
#include "timer.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#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)
{
return overflow_count;
}
void Timer_Start(void)
{
//clear the overflow event count
overflow_count = 0;
sei();
//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();
}