// #include <avr/io.h>
// #include <avr/interrupt.h>
// #include <avr/sleep.h>
// void TimerInit();
// unsigned char periode;
// ISR (TIMER0_COMPA_vect)
// {
// PORTB ^= (1 << PB7);
// }
// /*
// //////////// Frequensi 20KHz //////////
// prescaller 8 , Xtal 16MHz
// tick = 8/16Mhz= 0.5 us
// delay=counter*tick
// delay=counter*0.5us
// untuk sampling rate 40 khz maka Tsampling= 1/40Khz= 25us
// counter = 125/0.5us = 50
// nilai TCNT1= -50 -> karena counter up
// */
// int main(void) {
// DDRB |= _BV(PB7);
// TimerInit(); /// schedule 10ms
// sei();
// /// enable global interupsi
// while (1) {
// sleep_mode(); // sleep
// }
// }
// void TimerInit(void) {
// // TCNT0 = -50; // reset counter
// TCCR0B = (1 << CS01) ; //prescaller 8
// TIMSK0 |= (1 << OCIE0A); // enable compare maatch interrupt
// TCCR0A |=_BV(WGM01); //mode Timer CTC
// OCR0A = 50;
// }
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
void TimerInit();
unsigned char periode;
ISR (TIMER0_COMPA_vect)
{
PORTB ^= (1 << PB7);
}
/*
//////////// Frequensi 2KHz //////////
prescaller 64 , Xtal 16MHz
tick = 64/16Mhz= 4 us
delay=counter*tick
delay=counter* 4 us
untuk sampling rate 4 khz maka Tsampling= 1/4hz= 250us
counter = 250/ 4us = 62
nilai TCNT1= -62 -> karena counter up
*/
int main(void) {
DDRB |= _BV(PB7);
TimerInit(); /// schedule 10ms
sei();
/// enable global interupsi
while (1) {
sleep_mode(); // sleep
}
}
void TimerInit(void) {
// TCNT0 = -62; // reset counter
TCCR0B = (1 << CS01) | (1 << CS00) ; //prescaller 64
TIMSK0 |= (1 << OCIE0A); // enable compare maatch interrupt
TCCR0A |=_BV(WGM01); //mode Timer CTC
OCR0A = 62;
}