Anda di halaman 1dari 8

MODUL 3 TIMER & COUNTER

Pangeran Letare Sitompul (14S17009)


Tanggal percobaan : 13 Maret 2020
14S3204 Praktikum Sistem Mikrokontroler
Laboratorium Sistem Digital – Teknik Elektro
Institut Teknologi Del

Abstrak

Practicum module 1 microprocessor system practicum will discuss more about the components of the input
output base on the ATMega 8535 board. Practice it learn how to port to make as input or output. The input
output application will be made by using WinAVR software in C. Besides practicum also aims to understand Ude
settings related to large uses and crystal types.

Keywords: ATMega 8535, Input, Output, Fuse, WinAVR

nyala atau mati akan merepresentasikan biner dari


port D. Port A akan juga digunakan kembali sebagai
I. PENDAHULUAN output dan dihubungkan ke LED untuk menga mati
keluaran progra m. Program akan diatur sedemi kian
Praktikum terdiri dari beberapa sub percobaan.
rupa sehingga
Percobaan perta ma berkaitan dengan penggunaan
PortA seba gai output. Progra m akan dibuat sedemi Untuk konfi gurasi input tertentu akan
kian rupa sehingga menghasilkan konfigurasi output menghasilkan konfigurasi output yang sesuai dengan
port A yang telah ditentukan. Kemudian portA akan apa yang telah ditentukan.
dihubungkan ke LED sebagai representasi biner dari
port A. Adapun tujuan yang ingin dicapai dalam praktikum
ini antara lain, yaitu :
Kedua, pra ktikan akan menga mati penggunaan 1. Memahami datasheet ATMega 8535
delay pada AVR gcc. Delay bertujuan untuk 2. Mampu membuat aplikasi Timer dan
Counter pada AVR dengan menggunakan
mengatur seberapa cepat lampu LED hidup dan mati.
bahasa pemrograman C pada CV AVR.
Sama seperti perc obaan sebelumnya, port A akan
digunakan sebagai output. Nilai biner port A akan
berubah dari kondisi satu ke kondisi dua dalam
selang delay yang telah ditentukan. Kemudian
representasi biner bisa dilihat pada nyala dan matinya
LED.

Keti ga, praktikan akan mulai menggunakan sebuah


port s ebagai input. Port D a kan digunakan sebagai
input di mana port D akan dihubungkan langsung
dengan toggle/switch. Kon figurasi dari toggl e yang
II. LANDASAN TEORETIS PB5            MOSI (SPI Bus Master
Output/Slave Input)
PB6            MISO (SPI Bus Master
Input/Slave Output)
PB7            SCK (SPI Bus Serialock)
Port C Port I/O 8-bit dua arah dengan
(PC7..PC0) resistor pull-up internal.
Dua pin yaitu PC6 dan PC7 berfungsi
sebagai oscillator luar
untuk Timer/Counter2.
Port D Port I/O 8-bit dua arah dengan
Gambar 1. ATmega8535 (PD7..PD0) resistor pull-up internal.
Fungsi khusus masing-masing pin :
Port Pin Fungsi lain
PD0            RXD (UART Input Line)
PD1            TXD (UART Output Line)
PD2            INT0 (External Interrupt 0
Input)
PD3            INT1 (External Interrupt 1
Input)
PD4            OC1B (Timer/Counter1
Output CompareB Match Output)
PD5            OC1A (Timer/Counter1
Output CompareA Match Output)
PD6            ICP (Timer/Counter1 Input
Capture Pin)
PD7            OC2 (Timer/Counter2
Output Compare Match Output)
RESET Masukan reset. Sebuah reset terjadi jika
pin ini diberi logika rendah melebihi
periode minimum yang diperlukan.
XTAL1 Masukan ke inverting oscillator
Gambar 2. Konfigurasi pin ATmega8535 amplifier dan masukan ke
rangkaian clock internal.
Nama Pin Fungsi XTAL2 Keluaran dari inverting oscillator
VCC Catu daya amplifier.
GND Ground AVCC Catu daya untuk port A dan ADC.
Port A Port I/O 8-bit dua arah dengan AREF Referensi masukan analog untuk ADC.
(PA7..PA0) resistor pull-up internal. AGND Ground analog.
Juga berfungsi sebagai masukan analog
ke ADC (ADC0 s.d. ADC7)
Port B Port I/O 8-bit dua arah dengan
(PB7..PB0) resistor pull-up internal. II.1. Fitur Atmega8535
Fungsi khusus masing-masing pin :
Port Pin Fungsi lain Berikut adalah fitur-fitur yang terdapat dalam
PB0            T0 (Timer/Counter0 ATmega8535:
External Counter Input) a. 32 Port I/O
PB1            T1 (Timer/Counter1 b. 8 k Bytes Flash PEROM
External Counter Input) c. 512 Byte EEPROM
PB2            AIN0 (Analog Comparator d. 512 byte SRAM
Positive Input) e. 3 buah timer/counter
PB3            AIN1 (Analog Comparator f. 4 channel PWM
Negative Input) g. 8 channel ADC 10 bit
PB4            SS (SPI Slave Select Input) h. Osilator Internal 1 Mhz
i. USART bits depends on the WGM01:0 bit setting. Table 2
j. 3 buah sumber interupsi eksternal shows the COM01:0 bit functionality when the
k. Power supply: WGM01:0 bits are set to a normal or CTC mode
- 2,7 - 5,5 V untuk ATmega8535L (nonPWM). Table 3 shows the COM01:0 bit
- 4,5 - 5,5 V untuk ATmega8535 functionality when the WGM01:0 bit are set to fast
--Kecepatan: PWM mode. Table 4 shows the COM01:0 bit
- Maksimal 8 MHz untuk ATmega 8535L functionality when the WGM01:0 bit are set to phase
- Maksimal 16 MHz untuk ATmega 8535 correct PWM mode.

--Bit 7 – FOC0: Force Output Compare The FOC0 bit


is only active when the WGM00 bit specifies a non-
PWM mode. However, for ensuring compatibility
with future devices, this but must be set to zero
when TCCR0 is written when operating in PWM
mode. When writing a logical one to the FOC0 bit, an
immediate Compare Match is forces on the
Waveform Generation unit. The FOC0 output in
changed according to its COM0 1:0 bit setting. Note
that FOC0 bit is implemented as a strobe. Therefore,
it is the value present in the COM0 1: 0 bit that
determines the effect of the forced compare. A FOC0
strobe will not generate any interrupt, nor will it
clear the timer in CTC mode using OCR0 as TOP. The
FOC0 bit is always read as zero. 2.2.
--Bit 2:0 – CS02:0: Clock Select The three Clock
--Bit 6, 3 – WGM01:0: Waveform Generation Mode Select bit select the clock source to be used by
These bits control the counting sequence of the Timer/Counter.
counter, the source for the maximum (TOP) counter
value, and what type of waveform generation to be
used. Table 1 shows the Wave Generation Mode

--Bit 5:4 – COM01:0: Compare Match Output Mode --Timer/Counter Register – TCNT0
These bits control the Output Compare pin (OC0)
behavior. If one or both of the COM01: 0 bit are set,
the OC0 output overrides the normal port
functionality of the I.O pin it is connected to.
However, note that the Data Direction Register
(DDR) bit corresponding to the 0Co pin must be set The Timer/Counter Register gives direct access, both
in order to enable the output driver. When OC0 is for read and write operations, to the Timer/Counter
connected to the pin, the function of the COM01:0 unit 8-bit counter. Writing to the TCNt0 Register
block (removes) the Compare Match on the one to the flag. When the I-bit in SREG, OCIE0
following timer clock. Modifying the counter (TCNT0) (Timer/Counter0 Compare Match Interrupt is
while the counter is running, introduces a risk of executed.
missing a Compare between TCNT0 and the OCR0
Register.
>>>Bit 0 – TOV0: Timer/Counter 0 Overflow
--.Output Compare Register – OCR0
Flag The bit TOV0 is set (one) when an overflow
occurs in Timer/Counter0. TOV0 is cleared by
hardware when executing the corresponding
interrupt handling vector. Alternatively, TOV0 is
The Output Compare Register contains an 8 bit value cleared by writing a logic one to the flag. When
that is continuously compared with the counter the SREG I-bit, TOIE0 (Timer/Counter0 Overflow
value (TCNT0). A match can be used to generate an Interrupt is executed. In phase correct PWM
output compare interrupt, or to generate a mode, this bit is set when Timer/Counter0
waveform output on the OC0 pin changes counting direction at 0x00.

--Timer/Counter Interrupt Mask Register – TIMSK II.2. LCD

LCD (Liquid Crystal Display) adalah suatu jenis


media tampil yang menggunakan kristal cair sebagai
penampil utama. LCD sudah digunakan diberbagai
>>>Bit 1 – OCIE0: Timer/Counter0 Output Compare bidang misalnya alal–alat elektronik seperti televisi,
Match Interrupt Enable When the OCIE0 bit is kalkulator, atau pun layar komputer. Pada postingan
written to one, and the I-bit the Status Register is set aplikasi LCD yang dugunakan ialah LCD dot matrik
(one), the Timer/Counter0 Compare Match interrupt dengan jumlah karakter 2 x 16. LCD sangat berfungsi
is enable. The corresponding interrupt is executed if sebagai penampil yang nantinya akan digunakan
a Compare Match in Timer/Counter0 occurs (i.e.
untuk menampilkan status kerja alat.
when the OCF0 bit is set in the Timer/Counter
Interrupt Flag Register – TIFR).

>>>Bit 0 – TOIE0: Timer/Counter0 Overflow


Interrupt Enable When the TOIE0 bit is written to
one, and the I-bit in the Status Register is set (one),
the Timer/Counter0 Overflow interrupt is enabled.
The Corresponding interrupt is executed if an
overflow in Timer/Counter0 occurs (i.e., when the
TOV0 bit is set in the Timer/Counter Interrupt Flag
Register – TIFR)
Gambar 3. LCD 2x16
--.Timer/Counter Interrupt Flag Register – TIFR

>>>Bit 1 – OCF0: Output Compare Flag 0 The


OCF0 bit is set (one) when a Compare Match
occur between the Timer/Counter0 and the
data in OCR0 – Output Compare Register0.
OCF0 is cleared by hardware when executing
the corresponding interrupt handling vector.
Alternatively, OCF0 is cleared by writing a logic
80L
1A
R
P
S
JB
yH
ofm
Mikro
CN11
g.M
D
O
N
C
t()vT
rescaldibkpuhnw
III.

PB.0
1

Void main()
{
DDRB=0×FF;
PB.1
2

#include <mega8535.h>

PORTB=0b10101010;
While (1)

}
{
TCCR0=0b101;
TCNT0=125;
E
53642m
N
O
H
JM
A
X
D
,7
PROSEDUR PERCOBAAN

3.1 Delay dengan Timer 0

PB.2 PB.3
3
Tabel 1.
4

While((TIFR &0b00000001)==0);
TCCR0=0;
TIFR=0b00000001;
PORTB= ̴ PORTB;
}

Program1.
PB.4
5
PB.5
6

2
1
3
4
5
6
PB.6
7

}
T=

CN11
Tabel
1
fclc
Rumus 1.

2.
1
× ( FFh+1 ) × N

3.2 Delay dengan Timer 1

Mikro PB.0 PB.1


2

#include <mega8535.h>

Void main()
{
DDRB=0×FF;
While (1)
{
TCNT1=49910;
TCCR0=0b101;
PB.2
3
PB.3
4

While((TIFR &0b00000100)==0);
TCCR1B=0;
TIFR=0b00000100;

PORTB= ̴ PORTB;
}

Program 2.
PB.4
5
PB.5
6
PB.6
7
2
PB.7
8
u3)L
flJprB
yshgoM
detik.anm
Char str[10];
Void main()
{
DDRB=0×FF;
PORTB.0=1;
Lcd_init(16);
Lcd_clear();
b(S
D
421
3.3 External Clock Sebagai Counter

#include <mega8535.h>
#include <alcd.h>
#include <stdio.h>

Lcd_putsf(“Counter 0=”);
TCCR0=0b110;
While (1)
{
Sprint(str,”%i”, TCNT0);
Lcd_gotoxy(10,0);
Lcd_puts(str);

}
}

Program 3.

IV.
4.1.1 Percobaan 1.
HASIL ANALISIS

#include <mega8535.h>

Void main()
2
1
3
4
{
DDRB=0×FF;
PORTB=0b10101010;
While (1)

T=

T=

T=

}
{

1
TCCR0=0b101;
TCNT0=125;
While((TIFR &0b00000001)==0);
TCCR0=0;
TIFR=0b00000001;
PORTB= ̴ PORTB;
}

Rumus Matematika yang digunakan;

fclc
× ( FFh+1 ) × N
1
16.000.000
1
16.000.000
T= 0,000007875

4.1.2

void main()
{
DDRB = 0xFF;
× ( 125+1 ) ×1

× ( 126 )

#include <mega8535.h>

PORTB = 0b10101010;
while (1)
{

}
TCCR0 = 0b101;
TCNT0 = 112;
while ((TIFR & 0b00000001) ==0);
TCCR0 = 0;
TIFR = 0B00000001;

PORTB = ~PORTB ;

Rumus Matematika yang digunakan;


T=

T=

T=
1
f clc
× ( FFh+1 ) × N
1
16.000.000
1
16.000.000
T= 0,0000070625
× ( 112+1 ) ×1

× ( 113 )
4.2.1 Percobaan 1 Modifikasi. b. AVR GCC memiliki syntax delay untuk
#include <mega8535.h> membantu pemrogra man yang
membutuhkan fungsi waktu.
Void main() c. Untuk membuat port berfungsi sebagai
{ input dengan cara mengeset DDRx nya
dengan 0x00 (semua pin jadi pin input).
DDRB=0×FF;
Swi tch digunakan sebagai input dari user
While (1)
di Port D.
{ d. Dalam membuat port berfungsi sebagai
TCNT1=49910; output dengan cara mengeset DDRx nya
TCCR0=0b101; den gan 0xFF (semua pin jadi pin output).
While((TIFR &0b00000100)==0); LED digunakan sebagai output di Port A.
TCCR1B=0; e. Keselahan pada penempatan pin akan
TIFR=0b00000100; berdapak pada lampu LED yang akan
menyala/mati.
PORTB= ̴ PORTB; f. Kabel jumper yang dipasang pada ATMega
} harus sesuai dengan urutan jika salah
} maka lampu LED yang diinginkan
menyala/mati tidak sesuai dengan yang
diinginkan.
g. Ketepatan dan ketelitian dalam membuat
4.2.2
code program sangat berpengaruh pada
output yang keluar.
4.3.1 Percobaan 3. h. Pada praktikum modul 1 ini kami hanya
#include <mega8535.h> berhasil menyelesaikan percobaan sampai
#include <alcd.h> 1C.1 dimana keterbatasan waktu yang
#include <stdio.h> tersedia saat praktikum. Dan melanjutkan
Char str[10]; mengerjakan percobaan 1C.2 diluar sesi
Void main() praktikum.
{
DDRB=0×FF;
PORTB.0=1; DAFTAR PUSTAKA
Lcd_init(16);
Lcd_clear(); [1]. Kadir, Abdul (2012). Buku Pintar
Lcd_putsf(“Counter 0=”); MATLABuntuk Pemula. Yogyakarta : MediaKom.
TCCR0=0b110;
While (1) [2]. Utami, Ema. dan Sukrisno (2005). 10
{ Langkah Belajar Logika dan Algoritma
Sprint(str,”%i”, TCNT0); Menggunakan MATLAB di GNU/Linux.
Lcd_gotoxy(10,0); Yogyakarta : Andi Offset
Lcd_puts(str);
} [3]. Jackstar H. S., Panduan Penulisan Laporan,
} Jacks Publishing, Bandung, 2008.

LAMPIRAN

1. Tugas 1

#include <mega8535.h>
V. KESIMPULAN
Void main()
Adapun kesimpulan yang dapat diambil dai {
praktikum ini adalah sebagai berikut : DDRB=0×FF;
a. Sebelum melakukan praktikum sebaiknya PORTB=0b10101010;
baca petunjuk buku panduan mengenai While (1)
ATMega dan juga modul yang tersedia {
TCCR0=0b101; }
TCNT0=125; }
While((TIFR &0b00000001)==0);
TCCR0=0;
TIFR=0b00000001;
PORTB= ̴ PORTB;
}
}

Tugas 2
#include <mega8535.h>

Void main()
{
DDRB=0×FF;
While (1)
{
TCNT1=49910;
TCCR0=0b101;
While((TIFR &0b00000100)==0);
TCCR1B=0;
TIFR=0b00000100;

PORTB= ̴ PORTB;
}
}

Tugas 3.
#include <mega8535.h>
#include <alcd.h>
#include <stdio.h>
Char str[10];
Void main()
{
DDRB=0×FF;
PORTB.0=1;
Lcd_init(16);
Lcd_clear();
Lcd_putsf(“Counter 0=”);
While (1)
{
Sprint(str,”%i”, TCNT0);
Lcd_gotoxy(10,0);
Lcd_puts(str);

Anda mungkin juga menyukai