#ifndef TIMER_H_ #define TIMER_H_ TIMER_H_ #include #include #include extern volatile uint32_t timestamp; extern volatile uint16_t milliseconds; /* * Timer setting: * MCU running at 16MHz: * Prescaler is 64 which results in 250000 ticks per second * Preloading the counter with 6 leads to 1000 overflow interrupts per second * or one overflow every millisecond. */ static inline void timer_init(void) { TCNT0 = 6; //Preload for 250 ticks to overflow TIMSK |= (1 << TOIE0); TCCR0 = (1 << CS00) | (1 << CS01); // Prescaler 64 timestamp = 0; milliseconds = 0; } static inline void timer_set(uint32_t stamp) { TCCR0 &= ~((1 << CS00) | (1 << CS01)); // stop the timer TCNT0 = 6; //Preload for 250 ticks to overflow timestamp = stamp; milliseconds = 0; TCCR0 = (1 << CS00) | (1 << CS01); // Restart timer } #endif