#include <avr/io.h> // Library untuk mengakses register mikrokontroler AVR
void TimerInit(); // Deklarasi prototipe fungsi TimerInit()
unsigned char periode; // Variabel untuk menyimpan nilai periode
/*
* Prescaller: 8, Xtal: 16MHz
* Tick = 8/16MHz = 0.5 us
* Delay = counter * tick
* Delay = counter * 1/0.5 us
* Untuk sampling rate 16 kHz, Tsampling = 1/16kHz = 62.5us
* Counter = 62.5us / 0.5us = 125
*/
int main(void) {
DDRB |= _BV(PB7); // Set pin PB7 sebagai output
TimerInit(); // Inisialisasi timer
while (1) {
if (TIFR0 & (1 << OCF0A)) { // Cek apakah terjadi compare match pada timer0
PORTB ^= (1 << PB7); // Toggle nilai pada pin PB7
TIFR0 |= (1 << OCF0A); // Clear flag compare match timer0
}
}
}
void TimerInit(void) {
TCCR0B = (1 << CS01); // Set prescaler menjadi 8
TCCR0A |= _BV(WGM01); // mengaktifkan Mode Timer CTC (Clear Timer on Compare Match)
OCR0A = 125; // Set nilai perbandingan untuk mencapai sampling rate 16 kHz
}