#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
void TimerInit();
/*
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
*/
ISR (TIMER0_COMPA_vect)
{
PORTB ^= (1 << PB5);
}
int main(void) {
DDRB |= _BV(PB5);
TimerInit(); /// schedule 10ms
sei();
/// enable global interupsi
while (1) {
sleep_mode(); // sleep
}
}
void TimerInit(void) {
// TCNT0 = -250; // reset counter
TCCR0B = (1 << CS01)|(1 << CS00) ; //prescaller 64
TIMSK0 |= (1 << OCIE0A); // enable compare match interrupt
TCCR0A |=_BV(WGM01); //mode Timer CTC
OCR0A = 125;
}