/* * Time-Keeping helper */ //#define MILLISECONDS //#define MICROSECONDS //#define TEN_MICROSECONDS #define HUNDRED_MICROSECONDS #include #include #include "timer.h" volatile time_t systemTime = 0; void timerInit(void) { #if defined(__AVR_ATmega328__) TCCR2A |= (1 << WGM21); // CTC Mode #if defined(MILLISECONDS) TCCR2B |= (1 << CS22); // Prescaler: 64 OCR2A = 250; // Count to 250 #elif defined(MICROSECONDS) TCCR2B |= (1 << CS20); // Prescaler: 1 OCR2A = 16; // Count to 16 #elif defined(TEN_MICROSECONDS) TCCR2B |= (1 << CS20); // Prescaler: 1 OCR2A = 160; // Count to 160 #elif defined(HUNDRED_MICROSECONDS) TCCR2B |= (1 << CS22); // Prescaler: 64 OCR2A = 25; // Count to 25 #endif TIMSK2 |= (1 << OCIE2A); // Enable compare match interrupt #elif defined __AVR_ATtiny85__ // Using 8bit Timer1 TCCR1 |= (1 << CTC1); // CTC Mode #if defined(MILLISECONDS) TCCR1 |= (1 << CS12) | (1 << CS11) | (1 << CS10); // Prescaler: 64 OCR1A = 250; // Count to 250 #elif defined(MICROSECONDS) TCCR1 |= (1 << CS10); // Prescaler: 1 OCR1A = 16; // Count to 16 #elif defined(TEN_MICROSECONDS) TCCR1 |= (1 << CS10); // Prescaler: 1 OCR1A = 160; // Count to 160 #elif defined(HUNDRED_MICROSECONDS) TCCR1 |= (1 << CS12) | (1 << CS11) | (1 << CS10); // Prescaler: 64 OCR1A = 25; // Count to 25 #endif TIMSK |= (1 << OCIE1A); // Enable compare match interrupt #endif } #if defined(__AVR_ATmega328__) ISR(TIMER2_COMPA_vect) { #elif defined __AVR_ATtiny85__ ISR(TIMER1_COMPA_vect) { #endif #if defined(MILLISECONDS) || defined(MICROSECONDS) systemTime++; #elif defined(TEN_MICROSECONDS) systemTime += 10; #elif defined(HUNDRED_MICROSECONDS) systemTime += 100; #endif } time_t timerGet(void) { return systemTime; }