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