Jelajahi eBook
Kategori
Jelajahi Buku audio
Kategori
Jelajahi Majalah
Kategori
Jelajahi Dokumen
Kategori
1. Mengakses Timer dengan mode normal, compare match interrupt dan CTC
2. Memprogram timer dengan ketiga mode tersebut
3. Mengaplikasi fungsi timer pada mikrokontroler AVR
TEORI
Jenis timer yang diberikan oleh AVR ada dua yaitu timer dengan resolusi 8-bit dan 16-bit.
Sedangkan fitur timer dari masing-masing resolusi tersebut adalah sebagai berikut :
1. TIMER 0 dan 2 (resolusi 8-bit)
Features
a. Single Compare Unit Counter
b. Clear Timer on Compare Match (Auto Reload)
c. Glitch-free, Phase Correct Pulse Width Modulator (PWM)
d. Frequency Generator
e. External Event Counter
f. 10-bit Clock Prescaler (1, 8, 64, 256, 1024)
g. Overflow and Compare Match Interrupt Sources (TOV0 and OCF0)
taufiq[at]pens[dot]ac.id 39
Modul Praktikum Mikrokontroler 2014
2. TIMER 1 (resolusi 16-bit)
Features
a. True 16-bit Design (i.e., Allows 16-bit PWM)
b. Two Independent Output Compare Units
c. Double Buffered Output Compare Registers
d. One Input Capture Unit
e. Input Capture Noise Canceler
f. Clear Timer on Compare Match (Auto Reload)
g. Glitch-free, Phase Correct Pulse Width Modulator (PWM)
h. Variable PWM Period
i. Frequency Generator
j. External Event Counter
k. Four Independent Interrupt Sources (TOV1, OCF1A, OCF1B, and ICF1)
taufiq[at]pens[dot]ac.id 40
Modul Praktikum Mikrokontroler 2014
Secara umum mode pada Timer/Counter 0, 1 dan 2, diantaranya :
a. Normal
Mode yang paling sederhana dalam penggunaan timer adalah mode Normal (WGM01:00=0).
Didalam mode normal ini arah hitungan timer bersifat hitung naik (incrementing). Pola kerja
dari timer mode normal ini menghitung hingga TOP=0xff dan kembali restart ke 0x00 (untuk
timer resolusi 8-bit). Dan nilai hitungan timer dapat diisi setiap saat pada register TCNTx
b. CTC (Clear Timer on Compare Match)
Didalam mode CTC menggunakan register OCRx untuk memanipulasi nilai hitungan timer
(WGM01:00=2). Mode CTC ini mulai menghitung naik (incrementing) dan otomatis kembali Nol
ketika nilai register TCNTx sama dengan nilai register OCRx. Fenomena ini dikatakan
mekanisme autoreload, dimana internal AVR secara otomatis memberikan isi register TCNT
bernilai 0 kembali dan berjalan hitung naik kembali dan seterusnya. Sehingga timer dengan
mode CTC ini lebih presisi dibandingkan mode normal.
Dari mode CTC ini, terdapat istilah pembagi clock (prescaller = N) yang harus diketahui dalam
menghitung waktu atau frekuensi yang akan ditentukan nantinya, rumusan tersebut yaitu :
atau
taufiq[at]pens[dot]ac.id 41
Modul Praktikum Mikrokontroler 2014
Rumusan waktu maksimum dari suatu timer adalah sebagai berikut.
= 2.314814
Sehingga jika ingin didapatkan waktu maksimum dari timer 1 yang memiliki resolusi 16 bit dengan
prescaller =1 didapatkan
= 0.005926 = 5.926
Gunakan rumus berikut ini untuk menentukan Timer Value berdasarkan Interval Timer yang
diinginkan :
1
=( − )
Dimana :
Interval Timer = Waktu yang diinginkan
taufiq[at]pens[dot]ac.id 42
Modul Praktikum Mikrokontroler 2014
PROSEDUR PERCOBAAN
Prosedur Umum
1. Secara keseluruhan beberapa percobaan dibawah ini menggunakan wizard sehingga kode di-
generate secara otomatis.
2. Atur semua kode program yang ada kemudian diletakkan sesuai pada bagian-bagian kode hasil
generate wizard.
3. Pastikan hasil compile tidak menghasilkan error dengan menekan F9.
4. Selanjutnya download program tersebut ke MS-16 via USB dengan menekan Shift-F9 dan
tekan tombol Program the chip.
5. Amati dan analisa hasilnya kemudian catat hasil tersebut sebagai laporan sementara.
Prosedur Khusus
Percobaan ke :
1. Generate delay 1 detik TIMER1 menggunakan mode normal
taufiq[at]pens[dot]ac.id 43
Modul Praktikum Mikrokontroler 2014
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
#include <mega16.h>
int kondisi=0;
void main(void)
{
// Declare your local variables here
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
taufiq[at]pens[dot]ac.id 44
Modul Praktikum Mikrokontroler 2014
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x05;
TCNT1H=0xD5;
TCNT1L=0xD0;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
taufiq[at]pens[dot]ac.id 45
Modul Praktikum Mikrokontroler 2014
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
while (1)
{
// Place your code here
};
}
2. Generate delay 1 detik TIMER0 menggunakan mode normal match compare output
dengan cara yang sama seperti percobaan 1, buat interval waktu sebesar 100µS didapatkan
nilai Compare = 76H
taufiq[at]pens[dot]ac.id 46
Modul Praktikum Mikrokontroler 2014
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
#include <mega16.h>
int kondisi=0,looping=0;
void main(void)
{
// Declare your local variables here
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
taufiq[at]pens[dot]ac.id 47
Modul Praktikum Mikrokontroler 2014
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1382.400 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x02;
TCNT0=0x00;
OCR0=0x76;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
taufiq[at]pens[dot]ac.id 48
Modul Praktikum Mikrokontroler 2014
MCUCR=0x00;
MCUCSR=0x00;
while (1)
{
// Place your code here
};
}
3. Generate delay 1 detik TIMER1 menggunakan mode CTC (Clear Timer on Compare Match
Interrupt)
Mekanisme berikut ini adalah membuat delay 1 detik pada timer 1. Agar memperoleh waktu
yang lebih presisi, didapatkan nilai Compare A = 2A2FH
taufiq[at]pens[dot]ac.id 49
Modul Praktikum Mikrokontroler 2014
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
#include <mega16.h>
void main(void)
{
// Declare your local variables here
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
taufiq[at]pens[dot]ac.id 50
Modul Praktikum Mikrokontroler 2014
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0D;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x2A;
OCR1AL=0x2F;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
taufiq[at]pens[dot]ac.id 51
Modul Praktikum Mikrokontroler 2014
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
};
}
TUGAS
1. Hitung konstanta prescaler tiap percobaan.
2. Hitung waktu sesungguhnya setiap nilai heksa yang dihasilkan untuk tiap mode timer
berdasarkan rumus yang ada.
3. Cuplik register pada timer hasil generator program, Jelaskan!
4. Catat pemakaian memory flash yang dipakai dari masing-masing percobaan.
5. Kemudian jelaskan setiap baris terhadap syntax dari semua percobaan diatas kemudian
tarik kesimpulan.
6. Buat laporan resmi berdasarkan hasil pengamatan.!
taufiq[at]pens[dot]ac.id 52