#include <avr/io.h>
void TimerInit();
unsigned char periode;
/*
prescaller 64 , Xtal 16MHz
tick = 64/16Mhz= 4 us
delay=counter*tick
delay=counter*4us
counter=delayus/4us
untuk sampling rate 2 khz maka Tsampling= 1/2Khz= 500us
counter = 500/4us = 125
nilai TCNT1= -125 -> karena counter up
*/
int main(void) {
DDRB |= _BV(PB7);
TimerInit();
while (1) {
if (TIFR0&(1<<TOV0)) {
TCNT0= (unsigned char)-125;
//clear flag
TIFR0|=(1<<TOV0);
PORTB ^= (1 << PB7);
}
}
}
void TimerInit(void) {
TCNT0 = (unsigned char) -125; // reset counter
TCCR0B = (1 << CS01)|(1 << CS00); //prescaller 64
}