#include <mega128.h>
#include <delay.h>
void main(void)
{
// Declare your local variables here
PORTC=0x00;
DDRC=0xFF;
PORTC.0=0;
PORTC.1=0;
PORTC.2=0;
PORTC.3=0;
while (1)
{
char c2;
// Place your code here
PORTC.1=~PORTC.1;
for(c2 = 0; c2 < 10; c2++)
{
PORTC.0=~PORTC.0;
delay_ms(10);
};
}
}
B. TUGAS 5.1:
Modifikasi program pada subbab 5.2 diatas untuk menghasilkan gelombang kotak
dengan spesifikasi sebagai berikut:
PORTC.0 => T = 100ms; PORTC.1 => T = 300ms; PORTC.2 => T = 600ms;
PORTC.4 => T = 1200ms.
#include <mega128.h>
#include <delay.h>
void main(void)
{
// Declare your local variables here
PORTC=0x00;
DDRC=0xFF;
PORTC.0=0;
PORTC.1=0;
PORTC.2=0;
PORTC.3=0;
while (1)
{
char c2,c3,c4;
// Place your code here
PORTC.3=~PORTC.3;
for (c4=0;c4<2;c4++)
{
PORTC.2=~PORTC.2;
for (c3=0; c3<2;c3++)
{
PORTC.1=~PORTC.1;
for(c2 = 0; c2 < 3; c2++)
{
PORTC.0=~PORTC.0;
delay_ms(50);
};
}
}
}
}
B. TUGAS 5.2 :
Ulangi Tugas 5.1 dengan menggunakan timer internal.
#include <mega128.h>
int loop1,loop2,loop3;
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A 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
PORTA=0x00;
DDRA=0xFF;
// 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=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
PORTC=0x00;
DDRC=0xFF;
// Port D 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
PORTD=0x00;
DDRD=0x00;
// Port E 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
PORTE=0x00;
DDRE=0x00;
// Port F 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
PORTF=0x00;
DDRF=0x00;
// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;
// Timer/Counter 0 initialization
PORTC.2=0;
PORTC.3=0;
loop1=0;
loop2=0;
loop3=0;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
};
}
C. TUGAS 5.3 :
Program Jam Digital menggunakan Timer pada LCD.
#include <mega128.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1b //PORTC
#endasm
#include <lcd.h>
int loop1,loop2,loop3;
unsigned char buffer1[16];
unsigned char buffer2[16];
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
TCNT1H=0xD5D0 >> 8; //FBC8 => 0.1 detik; D5D0 => 1 detik
TCNT1L=0xD5D0 & 0xff;
loop1++;
if (loop1>=60)
{
loop2++;
loop1=0;
if (loop2>=60)
{
loop3++;
loop2=0;
if (loop3>=24)
{
loop3=0;
}
}
}
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A 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
PORTA=0x00;
DDRA=0xFF;
// 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=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
PORTC=0x00;
DDRC=0xFF;
// Port D 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
PORTD=0x00;
DDRD=0x00;
// Port E 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
PORTE=0x00;
DDRE=0x00;
// Port F 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
PORTF=0x00;
DDRF=0x00;
// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C 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
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x05; // prescaler = 1024
TCNT1H=0xD5; //FBC8 => 0.1 detik; D5D0 => 1 detik
TCNT1L=0xD0;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
ETIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
loop1=0;
loop2=0;
loop3=0;
lcd_init(16);
lcd_clear();
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
sprintf(buffer1,"Jam: %2d:%2d:%2d ",loop3,loop2,loop1);
sprintf(buffer2,"POLINES");
lcd_gotoxy(0,0);
lcd_puts(buffer1);
lcd_gotoxy(0,1);
lcd_puts(buffer2);
};
}
#include <mega128.h>
#include <stdlib.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
// Declare your global variables here
unsigned char temp[6];
int data;
void main(void)
{
// Timer/Counter 2 initialization
// Clock source: T2 pin Falling Edge
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x06;
TCNT2=0x00;
OCR2=0x00;
// LCD module initialization
lcd_init(16);
while (1)
{
// Place your code here
data=TCNT2;//hasil counter (TCNT0) dipindah ke data
if (data>=256)
{
lcd_clear();
}
lcd_gotoxy(0,0);
lcd_putsf("ElectrO-Polines");
itoa(data,temp); //menampilkan di LCD
lcd_gotoxy(0,1);
lcd_puts(temp);
};
}
#include <mega128.h>
#include <stdlib.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
// Declare your global variables here
unsigned char temp[6];
unsigned int data;
void main(void)
{
TCCR1A=0x00;
TCCR1B=0x06;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
// LCD module initialization
lcd_init(16);
while (1)
{
// Place your code here
data=TCNT1;//hasil counter (TCNT0) dipindah ke data
if (data>=0xFFFF)
{
lcd_clear();
}
lcd_gotoxy(0,0);
lcd_putsf("ElectrO-Polines");
itoa(data,temp); //menampilkan di LCD
lcd_gotoxy(0,1);
lcd_puts(temp);
};
}
B. TUGAS 5.4 :
Gabungkan kedua program pada percobaan 5.5 dalam 1 program (baris atas LCD
menampilkan hitungan dari tombol, sdangkan baris bawah LCD untuk
penghitungan pulsa)
#include <mega128.h>
#include <stdlib.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
// Declare your global variables here
unsigned char buffer1[16],buffer2[16];
unsigned int data1,data2;
void main(void)
{
TCCR1A=0x00;
TCCR1B=0x06;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
// Timer/Counter 2 initialization
// Clock source: T2 pin Falling Edge
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x06;
TCNT2=0x00;
OCR2=0x00;
// LCD module initialization
lcd_init(16);
while (1)
{
// Place your code here
data1=TCNT1;//hasil counter (TCNT0) dipindah ke data
data2=TCNT2;
if (data1>=0xFFFF)
{
lcd_clear();
}
if (data2>=0xFF)
{
lcd_clear();
}
sprintf(buffer1,"Pulsa: %d ",data1);
sprintf(buffer2,"Tombol: %d ",data2);
lcd_gotoxy(0,0);
lcd_puts(buffer1);
lcd_gotoxy(0,1);
lcd_puts(buffer2);
};
}
LCD
LCD Port: Port
A
Chars./line: 16
Timer 1
Clock Source:
T1 pin Falling Edge
Interrupt: Non
Active
Timer 3
Clock Source:
system Clock
Clock
10.800 kHz
Value:
Interrupt: Active
ETIMSK=0x04;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
// Place your code here
//data=TCNT1;//hasil counter (TCNT0) dipindah ke data
if (data>=0xFFFF)
{
lcd_clear();
}
lcd_gotoxy(0,0);
sprintf(buffer,"frek.:%5d Hz",data);
lcd_puts(buffer); // display data on LCD
};
}
6. Lihat perubahan tampilan pada LCD, bandingkan dengan nilai pada VSM Generator.
B. TUGAS 5.5 :
Modifikasi program pada contoh dimana baris kedua LCD menampilkan
frekuensi dalam KHz.
#include <mega128.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
#include <stdlib.h>
#include <stdio.h>
// Declare your global variables here
unsigned char buffer[16];
float data;
// Timer 3 overflow interrupt service routine
interrupt [TIM3_OVF] void timer3_ovf_isr(void)
{
// Place your code here
TCNT3H=0xD5D0 >> 8; //FBC8 => 0.1 detik; D5D0 => 1 detik
TCNT3L=0xD5D0 & 0xff;
data=TCNT1;
TCNT1H=0x00;
TCNT1L=0x00;
}
void main(void)
{
// Timer/Counter 1 initialization
// Clock source: T1 pin Falling Edge
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C 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
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x06;
TCNT1H=0x00; //D5D0
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFFFh
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Timer 3 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x05;
TCNT3H=0xD5; //D5D0
TCNT3L=0xD0;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ETIMSK=0x04;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
// Place your code here
//data=TCNT1;//hasil counter (TCNT0) dipindah ke data
if (data>=0xFFFF)
{
lcd_clear();
}
lcd_gotoxy(0,0);
sprintf(buffer,"frek.:%.2f Hz",data);
lcd_puts(buffer); // display data on LCD
lcd_gotoxy(0,1);
sprintf(buffer,"frek.:%.2f kHz",data/1000);
lcd_puts(buffer); // display data on LCD
};
}
B. TUGAS 5.6 :
Buat program simulasi TACHO-METER (RPM Meter).
II.Hasil Percobaan
Percobaan 5.1
Tugas 5.1
Percobaan 5.2
Tugas 5.3
Tugas 5.4
Percobaan 5.5
Tugas 5.5
III. Analisa
Untuk menghitung waktu / timer pada praktikum diatas konsepnya adalah dalam waktu
10 ms menghasilkan jumlah pulsa tiap satuan waktu, dengan cara mengaktifkan timer
yang sudah disediakan AVR
Timer 0:8 bit
Timer 1:16 bit misal kita akan memilih sebagai counter
Timer 2 :8 bit
Timer 3: 16bit misal kita akan memilih sebagai timer
Untuk menghitung pulsa frequensi yang dihasilkan tiap satuan waktu dengan
menggunakan konsep overflow yaitu ketika perhitungan waktu timer sampai dengan FF
maka akan menghitung mulai dari 0 lagisetelah itu data akan ditampilkan misal dalam
LCD ,oschiloscope untuk melihat bentuk sinyalnya atau output yang lainnya.
Untuk pengambilan datanya jika digunakan metode internal terlebih dahulu harus di
hitung TCNT dengan rumus :
Ttimer = Tosc*(256-TCNT0)*N (8 bit = 256)
Ttimer = Tosc*(65536-TCNT1)*N (16 bit = 65536)
Disini misalnya menghitung dari data TCNT 3 hasilnya dinyatakan dalam Hexa adalah
D5D0 maka untuk overflow ,namun tidak dapat mengerjakan interrupt jika nilai TCNTx
belum tentu FF,clock sourcnya untuk internal menggunakan system clock sumbernya
internal dari system timer ,jika falling edge sumbernyadari luar counter ,misal
perhitungannya yang dihasilkan:
TCNT3=D5D0
Data=TCNT1
TCNT1=00
Maksudnya diatas untuk dapat mengambil data maka data di sama dengankan TCNT1
dan TCNT1 disamadengankan 00 untuk overflow sebelum di nolkan ,selain untuk
menghitung frequensi dapat diaplikasikan juga untuk menghitung putaran per menit (rpm)
pada sensor octocoupler untuk menghitung tegangan dari arus yang dilewatkan .