Aplikasi ini merupakan sebuah stopwatch sederhana yang dapat menghitung sampai satuan
waktu terkecil yaitu 1/100 detik atau 10 ms. Untuk membangkitkan timer 10 ms saya
menggunakan fitur timer0 mikrokontroler AVR. Untuk lebih jelas mengenai fitur timer AVR
silakan baca postingan saya mengenai tutorial Timer dan Counter AVR.
Pada aplikasi ini terdapat 3 buah tombol (push button) yang terdiri dari tombol START, STOP
dan RESET. Tombol START digunakan untuk memulai stopwatch, STOP digunakan untuk
memberhentikan stowatch dan tombol RESET untuk me-reset (menset ulang kembali nilai
stopwatch).
Saya juga menyertakan program dalam file CodeVision AVR dan file simulasi proteus pada
akhir artikel ini. Berikut adalah schematik rangkaiannya.
#include <mega16.h>
#include <stdio.h>
#include <stdlib.h>
void hitung_waktu()
{
if (count>=100)
{
lcd_clear();
detik++;
count=0;
}
if (detik>=60)
{
menit++;
detik=0;
}
if (menit>=60)
{
jam++;
menit=0;
}
}
void detek_tombol()
{
if (PINA.0==0 && (indeks==0 || indeks==2))//start timer
{
indeks=1;
TIMSK=0x01;//memulai timer
lcd_clear();
}
void tampil_lcd()
{
lcd_gotoxy(0,0);
sprintf(temp,Timer %d:%d:%d:%d,jam,menit,detik,count);
lcd_puts(temp);//tampilkan waktu di LCD baris pertama
if (indeks==0)
{
lcd_gotoxy(0,1);
lcd_putsf(START);
}
if (indeks==1)
{
lcd_gotoxy(0,1);
lcd_putsf(STOP);
}
if (indeks==2)
{
lcd_gotoxy(0,1);
lcd_putsf(START RESET);
}
}
void main(void)
{
PORTA=0x0f;
DDRA=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 11.719 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x05;
TCNT0=0x8A;
OCR0=0x00;
while (1)
{
detek_tombol();
hitung_waktu();
tampil_lcd();
};
}