/* * Timer.c * * Created: 28/10/2021 17:51:29 * Author: n0x */ #include #include volatile uint16_t cntr = 0; void Timer_init (void){ TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */ TCCR0B |= (11<<0); /* Set the clock select bit to pre-scaler 3 16MHz / 64 (pre-scaler 3) ==> 250KHz */ TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */ OCR0A = 250 ; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms 16MHz / 64 (pre-scaler 3) / 250 ==> 1KHz (1ms) */ } uint16_t Timer_getTick(void){ uint16_t tmp; cli(); tmp = cntr; sei(); return tmp; } ISR(TIMER0_COMPA_vect){ cntr++; }