40 lines
727 B
C
40 lines
727 B
C
|
/*
|
||
|
* Timer.c
|
||
|
*
|
||
|
* Created: 28/10/2021 17:51:29
|
||
|
* Author: n0x
|
||
|
*/
|
||
|
|
||
|
#include <avr/io.h>
|
||
|
#include <avr/interrupt.h>
|
||
|
|
||
|
volatile uint16_t cntr = 0;
|
||
|
|
||
|
void
|
||
|
Timer_init (void){
|
||
|
TCCR0B |= (1<<1); /* Set the clock select bit to pre-scaler 2
|
||
|
1MHz / 8 (pre-scaler 2) ==> 125KHz */
|
||
|
|
||
|
TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */
|
||
|
TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */
|
||
|
|
||
|
OCR0A = 125; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms
|
||
|
1MHz / 8 (pre-scaler 2) / 125 ==> 1KHz (1ms) */
|
||
|
}
|
||
|
|
||
|
|
||
|
uint16_t
|
||
|
Timer_getTick(void){
|
||
|
return cntr;
|
||
|
}
|
||
|
|
||
|
|
||
|
ISR(TIMER0_COMPA_vect){
|
||
|
cntr++;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
ISR(TIMER0_OVF_vect){
|
||
|
cntr++;
|
||
|
}
|
||
|
//*/
|