EmbeddedSystems/EmbeddedSystemsTHM/Timer.c

44 lines
877 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){
TCCR0A |= (1<<WGM01); /* Set WGM01 to high to enable CTC mode */
TCCR0B |= (3<<CS00); /* Set the clock select bit to pre-scaler 3
16MHz / 64 (pre-scaler 3) ==> 250KHz */
TIMSK0 |= (1<<OCIE0A); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */
OCR0A = 250 - 1; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms
16MHz / 64 (pre-scaler 3) / (250 ) ==> 1KHz (1ms) */
/*
For internal 1MHz clock:
1MHz / 8 (pre-scaler 2) ==> 125 KHz
1MHz / 8 (pre-scaler 2) / 125 (OCR) ==> 1 KHz (1ms)
*/
}
uint16_t
Timer_getTick(void){
uint16_t tmp;
cli();
tmp = cntr;
sei();
return tmp;
}
ISR(TIMER0_COMPA_vect){
cntr++;
}