#include <avr/io.h>
int main (void)
{
DDRD |= (1 << PD0); // Set LED di portD0
TCCR1B |= (1 << CS10); // Set up counter dengan full speed
int a; variable
a=0; //nilai awal dari variable
while (1)
{
if(TCNT1 == 10000)
{ a++;
TCNT1=0;
}
if(a == 100)
{
PORTD ^= (1 << PD0); // Toggle the LED
a=0;
}
}
}
- PRESCALER
#define F_CPU 1000000UL //clock dari avr
#include <avr/io.h>
int main (void)
{
DDRD |= (1 << PD0); // Set LED di portD0
TCCR1B |= (1 << CS11); // Set up counter dengan prescale 64
//Baca datasheet untuk mencari konfigurasi prescaler yg laen
while (1)
{
if(TCNT1 == 15624)
// nilai max 1 detik, perhitungan mulai 0 maka nilai dikurangi 1 dari 15625
{
PORTD ^= (1 << PD0); // Toggle the LED
TCNT1=0;
}
}
}
Code:
#define F_CPU 1000000UL //clock dari avr
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(TIMER1_COMPA_vect) //vector interupt compare
{
PORTD ^= (1 << PD0); // Toggle the LED
}
int main (void)
{
DDRD |= (1 << PD0); // Set LED di portD0
TCCR1B |= (1 << CS11); // Set up counter dengan prescale 64
//Baca datasheet untuk mencari konfigurasi prescaler yg laen
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
TIMSK |= (1 << OCIE1A); // Enable CTC interrupt
OCR1A = 15624; // nilai atas dari TCNT1, untuk perhitungan 1 detik
sei(); //hidupkan interupt
while (1)
{
}
}
Untuk counter dengan nilai frekuensi clock / XTAL lainnya dapat menggunakan rumus matematika sederhana untuk menetukan OCR1A
OCR1A = [(Frekuensi AVR / Prescale) / frekuensi yg diinginkan ] - 1
atau
OCR1A = [(Frekuensi AVR / Prescale) * Periode yg di inginkan ] - 1
*) catatan : nilai prescale = 1,8,64,256,1024 bukan 1/prescale , untuk mempermudah saja
SELAMAT MENCOBA

















