Anda di halaman 1dari 114

A Software Oriented Device

Triwiyanto - Jurusan Teknik Elektromedik

APAKAH MIKROKONTROLLER ?
uC :sebuah komputer didalam chip untuk mengontrol peralatan elektronik

uC

Device Programmer
Sebuah device memerlukan komponen-komponen pendukung seperti IC TTL dan CMOS dapat direduksi/diperkecil dan akhirnya terpusat serta dikendalikan oleh mikrokontroler ini
Triwiyanto - Jurusan Teknik Elektromedik

KENAPA MENGGUNAKAN MIKROKONTROLLER ?


Dengan penggunaan mikrokontroler ini maka : Sistem elektronik akan menjadi lebih ringkas Rancang bangun sistem elektronik akan lebih cepat karena sebagian besar dari sistem adalah perangkat lunak yang mudah dimodifikasi Pencarian gangguan lebih mudah ditelusuri karena sistemnya yang kompak
Triwiyanto - Jurusan Teknik Elektromedik

APA MANFAATNYA ?
Dengan menguasainya kita dapat menerapkan :
1. Dalam kehidupan sehari-hari: a. Toaster b. Mesin Cuci c. Microwave dll. 2. Di bidang peralatan kedokter: a. Incubator baby b. Phototherapy c. Autoclave d. Photometer dll. 3. Di bidang industri a. Pengaturan proses produksi b. Pengaturan Motor, Robotika, Telekomunikasi dll.
Triwiyanto - Jurusan Teknik Elektromedik

BAGAIMANA CARANYA?
Desain Program Assembly (MIDE-51)
Org 0h mov A,10000000b mov P0,A end

Compile (MIDE-51)
0001110101 0101010101 0101010101 0101010101

Download (ATMEL Soft.)

Test

PROGRAM
01000101001 01111000100 01001010100 00111101010

Triwiyanto - Jurusan Teknik Elektromedik

BAGAIMANA CARA MEMPELAJARINYA ?


1.Belajar sendiri (otodidak), Anda bisa mempelajari sendiri mikrokontroler dengan panduan buku dan peralatan yang diperlukan, mulailah dari contoh-contoh sederhana. 2.Melalui lembaga Pendidikan, cara kedua ini bisa Anda dapatkan baik melalui pendidikan formal seperti sekolah, perguruan tinggi, maupun pendidikan non formal (kursus, pelatihan, les dan sejenisnya).
Triwiyanto - Jurusan Teknik Elektromedik

BELAJAR MIKROKONTROLLER ?
SOFTWARE, BHS. ASSEMBLY-> 60 %
HARDWARE, ARSITEKTUR -> 40 %

Triwiyanto - Jurusan Teknik Elektromedik

MIKROKONTROLLER AT89S51 Mikrokontroller 8 bit dengan 4K byte ISP ( In System Programmable )


FITUR:

Kompatibel dengan produk MCS-51 4K byte In System Programmable Flas Memory Dapat dilakukan pemrograman 1000 tulis dan hapus Range catu daya 4,0V s/d 5,0V Operasi statis: 0 Hz s/d 33 MHz Tiga Tingkat Program memory lock 128 x 8 bit RAM internal 32 Programmable Jalur I/O Dua 16 bit Timer/ Counter Enam Sumber Interupsi Full Duplex Serial Channel Low Power Idle dan Mode Power Down Watcht Dog Timer Fast Programming Time Fleksibel ISP programming
Triwiyanto - Jurusan Teknik Elektromedik

Introduction
General-purpose microprocessor
CPU for Computers No RAM, ROM, I/O on CPU chip itself Example: Intels x86, Motorolas 680x0
CPU GeneralPurpose Microprocessor

Data Bus

Many chips on mothers board


ROM I/O Port Timer Serial COM Port

RAM

Address Bus

General-Purpose Microprocessor System


Triwiyanto - Jurusan Teknik Elektromedik

Microcontroller :
A smaller computer On-chip RAM, ROM, I/O ports... Example: Motorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X

CPU

RAM

ROM

A single chip
Microcontroller

I/O PORT TIMER PORT SERIAL


Triwiyanto - Jurusan Teknik Elektromedik

Microprocessor vs. Microcontroller


Microprocessor CPU is stand-alone, RAM, ROM, I/O, timer are separate designer can decide on the amount of ROM, RAM and I/O ports. expansive versatility general-purpose

Microcontroller CPU, RAM, ROM, I/O and timer are all on a single chip fix amount of on-chip ROM, RAM, I/O ports for applications in which cost, power and space are critical single-purpose

Triwiyanto - Jurusan Teknik Elektromedik

Tiga kriteria dalam memilih mikrokontroller


1. meeting the computing needs of the task efficiently and cost effectively speed, the amount of ROM and RAM, the number of I/O ports and timers, size, packaging, power consumption easy to upgrade cost per unit 2. availability of software development tools assemblers, debuggers, C compilers, emulator, simulator, technical support 3. wide availability and reliable sources of the microcontrollers. Triwiyanto - Jurusan Teknik
Elektromedik

1. ARSITEKTUR
1.1. Diagram blok
External interrupts Interrupt Control On-chip Flash ROM
Timer/Counter

On-chip RAM

Timer 1 Timer 0

Counter Inputs

CPU
Serial Port

OSC

Bus Control

4 I/O Ports

P0 P1 P2 P3

TxD RxD

Address/Data
Triwiyanto - Jurusan Teknik Elektromedik

1.2 Comparison of the 8051 Family Members


Feature ROM (bytes) RAM (bytes) Timers I/O pins 8031 89C51 0K 128 2 32 4k 128 2 32 89S51 4k 128 2 32

Serial port Interrupt sources Programming ROM

1 6 Ext

1 6 Parallel

1 6 Serial/ ISP

Triwiyanto - Jurusan Teknik Elektromedik

1.3. Memory mapping in 8051

1.3.1. ROM memory map in 8051 family


4k
0000H 0000H

8k

0FFFH

1FFFH AT89C51 AT89S51

from Atmel Corporation

AT89C52 AT89S52

Triwiyanto - Jurusan Teknik Elektromedik

1.3.2. Memory map in 89s51 family 8951


Register ACC B PSW SP DPTR P0-P3
IP

Nilai ( biner ) 00000000 00000000 00000000 00000111 00000000 11111111


0FFF H P0

4 K ROM INTERNAL 0000 H


RAM INTERNAL FF H INDIRECT DIRECT ADDRESSING ADDRESSING 128 RAM UPPER DIRECT/ INDIRECT ADDRESSING 128 RAM LOWER 128 RAM SFR FF H

ALE

00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
0 80 H 7F H

P2

IE TMOD TCON THO TL0 TH1 TL1 SCON

/EA

80 H

/RD

/WR

Triwiyanto - Jurusan Teknik Elektromedik

1.4. Register and Port


What is a register? Register adalah memory penyimpan khusus dengan kapasitas 8 bit, yang tampak sebagai berikut:

D7

D6

D5

D4

D3

D2

D1

D0

Sebuah register dapat menyimpan bilangan dari 0 s/d 255 (bilangan positif, tanpa nilai negatif), atau bilangan dari -128 s/d +127 (dengan bit tanda di bit 7), atau suatu nilai yang mewakili suatu kode karakter ASCII ( contoh A).

Port
What is a Port? Port adalah gerbang dari CPU ke internal dan ekstenal hardware. CPU berkomunikasi dengan Port ini, membaca dari atau menulis ke port.

Port mempunyai address tertentu, untuk berkomunikasi dengan CPU, alamat-alamat dari port tidak tergantung dari tipe mikrokontroller pada keluarga MCS-51. Contoh alamat port P0 adalah selalu 80h. Anda tidak perlu mengingat alamat port ini.

P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0


Triwiyanto - Jurusan Teknik Elektromedik

BASIS BILANGAN
Desimal (BASIS 10) Hexa (BASIS 16) Biner 8 BIT (BASIS 2) ASCHII (CODE)

0
1 2 3 :

00
01 02 03 :

0000 0000
0000 0001 0000 0010 0000 0011 :

30
31 32 33 :

9
10 11 12 13 14 15

9
A B C D E F

0000 1001
0000 1010 0000 1011 0000 1100 0000 1101 0000 1110 0000 1111

39
-

Triwiyanto - Jurusan Teknik Elektromedik

Lanjutan: KONVERSI BILANGAN


Desimal (BASIS 10) Hexa (BASIS 16) Biner 8 BIT (BASIS 2) ASCHII (CODE)

16
17 18 19 :

10
11 12 13 :

0000 0000
0000 0001 0000 0010 0000 0011 :

25
26 27 28 29 30 31

19
1A 1B 1C 1D 1E 1F

0001 1001
0001 1010 0001 1011 0001 1100 0001 1101 0001 1110 0001 1111

Triwiyanto - Jurusan Teknik Elektromedik

KUNCI BASIS BILANGAN


Biner 8 bit D7 D6 MSB D5 D4 D3 D2 D1 D0 LSB

2
64

2
32

2
16

2
8

2
4

2 2
2 1

128

Contoh: Konversikan bilangan desimal 56 ke biner dan hexa Solusi: 56 d = terdiri dari penjumlahan (32+16+8) = bilangan biner, 00111000 b
Untuk konversi ke hexa, pecah bilangan tersebut menjadi 4 bit: 0011 1000b Dan terjemahkan masing-masing biner 4 bit tersebut ke hexa

= bilangan hexa, 38 h
Triwiyanto - Jurusan Teknik Elektromedik

1.5. Memori Data (RAM)


8951
F0 H E0 H D0 H B8 H B0 H A8 H A0 H B ACC PSW IP P3 IE P2 SBUF

REGISTER PSW
CY AC FO RS1 RS0 OV P

98 H SCON 90 H P1

88 H TCON TMOD 80 H P0 7F H SP

TL0 DPL

TL1 DPH

TH0

TH1 PCON

8F H 87 H

Carry flag Auxilarry carry flag General purpose Register Bank Select Register Bank Select Over Flow General purpose Parity

CY AC F0 RS1 RS0 OV P

PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0

RAM SERBAGUNA
30 H 2F H

RS1 0
R6 1EH R6 16H R6 OEH R6 O6H R7 1FH R7 17H R7 OFH R7 O7H BANK 3

RS0 0 1 0 1

Bank 0 1 2 3

Address 00H-07H 08H-0FH 10H-17H 18H-1FH

BIT-ADDRESSABLE SPACE
20 H RO 18H RO 1OH RO O8H RO OOH R1 19H R1 11H R1 O9H R1 O1H R2 1AH R2 12H R2 OAH R2 O2H R3 1BH R3 13H R3 OBH R3 O3H R4 1CH R4 14H R4 OCH R4 O4H R5 1DH R5 15H R5 ODH R5 O5H

0 1 1

BANK 2 BANK 1

BANK 0

Triwiyanto - Jurusan Teknik Elektromedik

1.6. Memori Program ( ROM )


Contoh Program LOC 0000 0000 0001 0003 0005 0006 OBJ LINE 1 2 3 4 5 6 SOURCE
P0 D0 S/D D7

8951

ROM EKSTERNAL

00 E5B0 F590 80FA

org 0h nop start: mov a,p3 mov p1,a sjmp start end

FFFF H 0FFF H 4K ROM INTERNAL 0000 H


P2 PSEN OE ALE ADDR. 60K ROM EKSTERNAL

Contoh hasil kompilasi tersebut menjelaskan 1. Data 00 di save ke alamat 0000H 2. Data B0 di save ke alamat 0001H 3. Data E5 di save ke alamat 0002h 4. Data 90 di save ke alamat 0003H 5. Data F5 di save ke alamat 0004H 6. Data FA di save ke alamat 0005H 7. Data 80 di save ke alamat 0006H
Triwiyanto - Jurusan Teknik Elektromedik

1000 H
/EA

Pin Description of the 8051


PDIP/Cerdip
P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) EA/VPP ALE/PROG PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8)

8051 (8031) 89X51

Triwiyanto - Jurusan Teknik Elektromedik

Pins of 805: 1/4


Vcc (pin 40) Vcc provides supply voltage to the chip. The voltage source is +5V. GND (pin 20) ground XTAL1 and XTAL2 (pins 19,18) These 2 pins provide external clock. Way 1:using a quartz crystal oscillator Way 2:using a TTL oscillator Example 4-1 shows the relationship between XTAL and the machine cycle.
Triwiyanto - Jurusan Teknik Elektromedik

Pins of 8051: 2/4


RST (pin 9) reset It is an input pin and is active high (normally low). The high pulse must be high at least 2 machine cycles. It is a power-on reset. Upon applying a high pulse to RST, the microcontroller will reset and all values in registers will be lost. Reset values of some 8051 registers Way 1:Power-on reset circuit Way 2:Power-on reset with debounce

Triwiyanto - Jurusan Teknik Elektromedik

Pins of 8051: 3/4


/EA (pin 31) external access There is no on-chip ROM in 8031 and 8032 . The /EA pin is connected to GND to indicate the code is stored externally. /PSEN and ALE are used for external ROM. For 8051, /EA pin is connected to Vcc. / means active low. /PSEN (pin 29) program store enable This is an output pin and is connected to the OE pin of the ROM. See Chapter 14.
Triwiyanto - Jurusan Teknik Elektromedik

Pins of 8051: 4/4


ALE (pin 30), address latch enable It is an output pin and is active high. 8051 port 0 provides both address and data. The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch. I/O port pins The four ports P0, P1, P2, and P3. Each port uses 8 pins. All I/O pins are bi-directional.
Triwiyanto - Jurusan Teknik Elektromedik

XTAL Connection to 8051


Using a quartz crystal oscillator We can observe the frequency on the XTAL2 pin. C2
XTAL2 30pF C1 XTAL1 30pF GND

Triwiyanto - Jurusan Teknik Elektromedik

XTAL Connection to an External Clock Source


N C XTAL2

Using a TTL oscillator XTAL2 is unconnected.

EXTERNAL OSCILLATOR SIGNAL

XTAL1

GND

Triwiyanto - Jurusan Teknik Elektromedik

Power-On RESET Circuit


Vcc + 10 uF 30 pF 11.0592 MHz 8.2 K 30 pF 18 X2 9 RST 31 EA/VPP X1

19

Triwiyanto - Jurusan Teknik Elektromedik

Power-On RESET with Debounce


Vcc

31

10 uFC:\WINDOWS\hinhem.scr 30 pF

EA/VPP X1

X2 RST 9 8.2 K

Triwiyanto - Jurusan Teknik Elektromedik

2. Set Instruction
2.1. Instruksi copy data
MOV dest, source ; dest = source
MOV A,#72H ;A=72H MOV A, #r ;A=r OR 72H MOV R4,#62H ;R4=62H MOV B,0F9H ;B=the content of F9th byte of RAM MOV DPTR,#7634H ;DPTR=7634 MOV P1,A ;mov A to port 1 Note 1: MOV A,#72H MOV A,72H After instruction MOV A,72H the content of 72th byte of RAM will replace in Accumulator. Note 2: MOV A,R3 MOV A,3
Triwiyanto - Jurusan Teknik Elektromedik

Contoh 1 :
Start: org 0h mov PSW,#00000000B ; select register bank 0 mov R0,#5 ; simpan data 5 ke R0 mov R1,#7 ; simpan data 7 ke R1 mov R2,#8 ; simpan data 8 ke R2 ; mov PSW,#00001000B ;select register bank 2 mov R0,#0Ah ; simpan data Ah ke R0 mov R1,#9 ; simpan data 9 ke R1 mov R2,#01Bh ; simpan data 1bh ke R2 end Start: org 0h mov 00h,#5 mov 01h,#7 mov 02h,#8 ; mov 10h,#0Ah mov 11h,#9 mov 12h,#01Bh end

OR

Contoh 2:
Start:
Org 0h setb 20h.1 ; setting bit=1, 00000010 pada address 20h setb 27h.7 ; setting bit=1, 10000000 pada address 27h clr 2Ah.5 ; setting bit =0, 11011111 pada address 2Ah end Org 0h mov 0E0h,#00000001b mov 90h,0E0h end

Contoh 3:
Start: Org 0h mov A,#00000001b mov P1,A end Start:

OR

Triwiyanto - Jurusan Teknik Elektromedik

SETB CLR
SETB SETB SETB SETB SETB C P0.0 P3.7 ACC.2 05

bit bit

; bit=1 ; bit=0
; CY=1 ;bit 0 from port 0 =1 ;bit 7 from port 3 =1 ;bit 2 from ACCUMULATOR =1 ;set high D5 of RAM loc. 20h

Note: CLR instruction is as same as SETB i.e: CLR C ;CY=0 But following instruction is only for CLR: CLR A ;A=0

Triwiyanto - Jurusan Teknik Elektromedik

2.1. Instruksi Aritmatika


2.1.1 Instruksi Penjumlahan
ADD A, Source ;A=A+SOURCE ADD A,#6 ;A=A+6 ADD A,R6 ;A=A+R6 ADD A,6 ;A=A+[6] or A=A+R6 ADD A,0F3H ;A=A+[0F3H] ADC A,source ;A=A+source+CY SETB C ;CY=1 ADC A,R5 ;A=A+R5+1

2.1.2 Instruksi Pengurangan


SUBB SETB SUBB A,source C A,R5 ;A=A-source-CY ;CY=1 ;A=A-R5-1

Triwiyanto - Jurusan Teknik Elektromedik

Example:
MOV ADD A,#88H A,#93H 10001000 +10010011 -------------00011011 P=0

Example:
MOV ADD A,#9CH A,#64H 10011100 +01100100 -------------00000000 P=0

88 +93 ---11B CY=1 AC=0

9C +64 ---100 CY=1 AC=1

Example:
MOV ADD A,#38H A,#2FH 00111000 +00101111 -------------01100111 P=1

38 +2F ---67 CY=0 AC=1

Note: 1. Flag C is set if the sum exceeds FFh and cleared otherwise 2. Flag AC is set if there is a carry from low nibble to the high nible that is from bit 3 to bit 4, and cleared otherwise 3. Flag P is set if 0 or 1 bit count is odd

Triwiyanto - Jurusan Teknik Elektromedik

2.1.3 Instruksi Perkalian


MUL AB ;B|A = A*B MOV A,#25H MOV B,#65H MUL AB ;25H*65H=0E99 ;B=0EH, A=99H

2.1.4 Instruksi Pembagian


DIV AB ;A = A/B, B = A mod B MOV A,#25 MOV B,#10 DIV AB ;A=2, B=5
Triwiyanto - Jurusan Teknik Elektromedik

2.1.5 Instruksi Decrement dan Increment


DEC INC Example: INC DEC DEC byte byte R7 A 40H ;byte=byte-1 ;byte=byte+1 ;[R7]=[R7]+1 ;[A]=[A]-1 ; [40]=[40]-1

Triwiyanto - Jurusan Teknik Elektromedik

Table 1.3. Instruksi Aritmatika


Mnemonic Operation Addressing Mode Dir Ind Reg Imm Exect. Timer uS

Add A,<byte>
Addc A,<byte> Subb A,<byte> Inc A

A=A+<byte>
A=A+<byte>+C A=A-<byte>-C A=A+1

V
V V

V
V V

V
V V

V
V V

1
1 1 1

Accumulator Only

Inc <byte>
Inc DPTR Dec A Dec <byte> Mul AB Div AB DA A

<byt>=<byt>+1
DPTR=DPTR+1 A=A-1 <byt>=<byt>-1 B:A=BxA A=Int[A/B] B=Mod[A/B] Dec Adjust

1
2 1 1 4 4 1

Data Pointer Only Accumulator Only V V V Accumulator and B Only Accumulator and B only Accumulator Only

Triwiyanto - Jurusan Teknik Elektromedik

2.2. Instruksi Logika


ANL - ORL XRL-CPL EXAMPLE: MOV R5,#89H ANL R5,#08H RR RL RRC RLC EXAMPLE: Mov A,#11000000b RR A ;[A]=01100000b

Triwiyanto - Jurusan Teknik Elektromedik

Example:
RL A
D7 Contoh:
START: PUTAR: ORG 0H MOV A,#00000001B RL A SJMP PUTAR END

;rotate left A
D6 D5 D4 D3 D2 D1 D0

A = 00000010 A = 00000100 A = 00001000 : A = 10000000 A = 00000001 A = dst.

RLC A
CY Contoh:
START: PUTAR:

;rotate left and carry A


D7 D6 D5 D4 D3 D2 D1 D0

ORG 0H MOV A,#00000001B RLC A SJMP PUTAR END

C=1

A = 00000010 A = 00000100 : : A = 10000000 A = 00000000 A = 00000001 dst.

RR A ;rotate right A RRC A ;rotate right ;and carry A

Triwiyanto - Jurusan Teknik Elektromedik

Mnemonic Anl A,<byte> Anl <byte>,A Anl <byte>,#data Orl A,<byte>

Operation A=A and <byte> <byte>=<byte>anl A <byte>=<byte>and #data A=A or <byte>

Addressing Mode Dir V V V Ind V V V Reg V V V Imm V V V

Exect. uS 1 1 1 1

Accumulator Only

Orl <byte>,A
Orl <byte>,#data Xrl A,<byte>

<byt>=<byt>orl A
<byte>=<byte> or #data A=A xor<byte>

1
2 1

Data Pointer Only Accumulator Only

Xrl<byte>,A
Xrl <byte>,#data CLR A CPL A

<byt>=<byt>xor A
<byte>=<byte>xor #data A=00h A= not A

1
4 1 1

Accumulator and B Only Accumulator only Accumulator only

RL A
RLC A RR A RRC SWAP A

Rotate A left 1 bit


Rotate A left trough Carry Rotate A right 1 bit Rotate A right trough carry Swap nibbles in A

Accumulator only
Accumulator only Accumulator only Accumulator only Accumulator only

1
1 1 1 1

Triwiyanto - Jurusan Teknik Elektromedik

2.3. Addressing Modes


Immediate Register Direct Register Indirect Indexed

Triwiyanto - Jurusan Teknik Elektromedik

2.3.1. Immediate Addressing Mode


MOV MOV MOV MOV MOV A,#65H A,#A R6,#65H DPTR,#2343H P1,#65H

Example : Num MOV MOV ORG data1:

EQU 30 R0,Num DPTR,#data1 100H db IRAN


Triwiyanto - Jurusan Teknik Elektromedik

2.3.2. Register Addressing Mode


MOV ADD MOV MOV MOV Rn, A A, Rn DPL, R6 DPTR, A Rm, Rn ;n=0,..,7

Triwiyanto - Jurusan Teknik Elektromedik

2.3.3. Direct Addressing Mode


Although the entire of 128 bytes of RAM can be accessed using direct addressing mode, it is most often used to access RAM loc. 30 7FH.

MOV R0, 40H MOV 56H, A MOV A, 4 MOV 6, 2

; MOV A, R4 ; copy R2 to R6 ; MOV R6,R2 is invalid !

SFR register and their address

MOV 0E0H, #66H ; MOV A,#66H MOV 0F0H, R2 ; MOV B, R2 MOV 80H,A ; MOV P1,A
Triwiyanto - Jurusan Teknik Elektromedik

2.3.4. Register Indirect Addressing Mode


In this mode, register is used as a pointer to the data.
MOV
MOV

A,@Ri
@R1,B

; move content of RAM loc.Where address is held by Ri into A ;( i=0 or 1 )

In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB insructions. Example:

Write a program to copy a block of 10 bytes from RAM location sterting at 30h to RAM location starting at 60h. Solution: MOV R0,#30h ; source pointer MOV R1,#60h ; dest pointer MOV R2,#10 ; counter L1:MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2,L1

Triwiyanto - Jurusan Teknik Elektromedik

2.3.5. Indexed Addressing Mode And ROM Access


This mode is widely used in accessing data elements of look-up table entries located in the program (code) space ROM at the 8051 MOVC A,@A+DPTR

A= content of address A +DPTR from ROM Note: Because the data elements are stored in the program (code ) space ROM of the 8051, it uses the instruction MOVC instead of MOV. The C means code.
Triwiyanto - Jurusan Teknik Elektromedik

Example:
Assuming that ROM space starting at 250h contains Hello., write a program to transfer the bytes into RAM locations starting at 40h.
Solution: ORG 0 MOV DPTR,#MYDATA MOV R0,#40H L1: CLR A MOVC A,@A+DPTR JZ L2 MOV @R0,A INC DPTR INC R0 SJMP L1 L2: SJMP L2 ;------------------------------------ORG 250H MYDATA:DB Hello,0 END
Notice the NULL character ,0, as end of string and how we use the JZ instruction to detect that.
Triwiyanto - Jurusan Teknik Elektromedik

Example:

Write a program to get the x value from P1 and send x2 to P2, continuously .
Solution: ORG 0 MOV DPTR, #TAB1 L01: MOV A,P1 MOVC A,@A+DPTR MOV P2,A SJMP L01 ;---------------------------------------------------ORG 300H TAB1: DB 0,1,4,9,16,25,36,49,64,81 END

Triwiyanto - Jurusan Teknik Elektromedik

2.4. LOOP and JUMP Instructions 2.4.1. DJNZ ( Decrement & Jump if Not Zero )
Write a program to clear ACC, then add 3 to the accumulator ten time

Solution: MOV A,#0; MOV R2,#10 AGAIN:ADD A,#03 DJNZ R2,AGAIN ;repeat until R2=0 (10 times) MOV R5,A

2.4.2. CJNE (Compare & Jump if Not Equal )


Get: Mov A, P1 Cjne A,#8, get end
Triwiyanto - Jurusan Teknik Elektromedik

Contoh: Soal UAS


1. Tuliskan sebuah program untuk menolkan isi Akumulator (A), dan menambah dengan konstanta 10 pada akumulator sebanyak 5 kali. Petunjuk: gunakan MOV, ADD, DJNZ. 2. Tuliskan program untuk mengurangi nilai R0 dari 20 sampai 0, bila isi R0 tidak sama dengan 0 maka P0.0 mengeluarkan logika 0 bila isi R0 sama dengan 0 maka P0.0 mengeluarkan logika 1. Petunjuk: gunakan MOV, DEC, CJNE, CLR, SETB 3. Tuliskan program untuk menggeser data Akumulator (A) yang berisi konstanta 11111110 ke kiri sebanyak 5 kali, dan menggeser ke kanan sebanyak 3 kali. Petunjuk: gunakan MOV, RL, RR, DJNZ.

Triwiyanto - Jurusan Teknik Elektromedik

Contoh: Soal UAS 2


4. Tuliskan program untuk mengeluarkan logika 0 ke P0.6 sebanyak 10 kali, dilanjutkan mengeluarkan logika 1 ke P0.7 sebanyak 40 kali. Petunjuk: gunakan MOV, DJNZ. 5. Rencanakan rangkaian dengan menggunakan seven segmen common katoda 1 buah, dengan driver transistor tipe NPN. Segmen terhubung ke P0 dan driver terhubung ke P3.0, dan tulis program untuk mencetak karakter H, pada seven segmen tersebut. Petunjuk: gunakan MOV, SETB, CLR 6. Tuliskan program untuk melakukan proses aritmatika berikut: R4=10d, R5=5d, R6=4d, R7=2d; a. R3 = R7 + R4 * R5 R6; b. R3= R4/R5 + R6* R7
Petunjuk: gunakan instruksi ADD A, RX; SUBB A, RX; MUL AB, DIV AB
Triwiyanto - Jurusan Teknik Elektromedik

AKHIRNYA
TANKS ALOT TRIMAKASIH MATUR SUWON
ILL SEE YOU SOON, IN THE NEXT SEMESTER, TO ROCK AND ROLL AND HAVING FUN

Triwiyanto - Jurusan Teknik Elektromedik

2.4.3. SJMP and LJMP:


LJMP(long jump) LJMP is an unconditional jump. It is a 3-byte instruction in which the first byte is the opcode, and the second and third bytes represent the 16-bit address of the target location. The 20byte target address allows a jump to any memory location from 0000 to FFFFH. SJMP(short jump) In this 2-byte instruction. The first byte is the opcode and the second byte is the relative address of the target location. The relative address range of 00-FFH is divided into forward and backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the current PC.

Triwiyanto - Jurusan Teknik Elektromedik

Other conditional jumps :


JZ JNZ Jump if A=0 Jump if A/=0

DJNZ
CJNE A,byte,lompat1

Decrement and jump if A/=0


Jump if A/=byte

CJNE reg,#data,lompat1
JC JNC JB JNB JBC

Jump if byte/=#data
Jump if CY=1 Jump if CY=0 Jump if bit=1 Jump if bit=0 Jump if bit=1 and clear bit
Triwiyanto - Jurusan Teknik Elektromedik

2.4.4. CALL Instructions


Another control transfer instruction is the CALL instruction, which is used to call a subroutine. LCALL(long call) In this 3-byte instruction, the first byte is the opcode an the second and third bytes are used for the address of target subroutine. Therefore, LCALL can be used to call subroutines located anywhere within the 64K byte address space of the 8051.
Triwiyanto - Jurusan Teknik Elektromedik

ACALL (absolute call)


ACALL is 2-byte instruction in contrast to LCALL, which is 13 bytes. Since ACALL is a 2-byte instruction, the target address of the subroutine must be within 2K bytes address because only 11 bits of the 2 bytes are used for the address. There is no difference between ACALL and LCALL in terms of saving the program counter on the stack or the function of the RET instruction. The only difference is that the target address for LCALL can be anywhere within the 64K byte address space of the 8051 while the target address of ACALL must be within a 2Kbyte range.
Triwiyanto - Jurusan Teknik Elektromedik

Port 0 with Pull-Up Resistors


Vcc 10 K
P0.0 P0.1 P0.2 P0.3 P0.4 P0.5 P0.6 P0.7

Port

8751 8951

Triwiyanto - Jurusan Teknik Elektromedik

Port 3 Alternate Functions


P3 Bit
P3.0 P3.1 P3.2 P3.3 P3.4 P3.5 P3.6 P3.7

Function
RxD TxD INT0 INT1 T0 T1 WR RD

Pin
10 11 12 13 14 15 16 17

Triwiyanto - Jurusan Teknik Elektromedik

Structure of Assembly language


Label Opcode
Start: ORG Mov Mov END

Operand
0H A,#10001000b P0,A

Comment
;StartROM ; fill A = 10001000b ;copy A to P0

Desain Program Assembly (MIDE-51) Org 0h mov A,10000000b mov P0,A end

Compile (MIDE-51)

Download (ATMEL Soft.)

TEST

0001110101 0101010101 0101010101 0101010101

PROGRAM
01000101001 01111000100 01001010100 00111101010

Triwiyanto - Jurusan Teknik Elektromedik

Percobaan 1.1. Menghidupkan/ Matikan LED


5V

Start:

Org 0h ;Starting ROM 0h Mov P0, #00000001;Send 1 to P0.0 end ;LED OFF Org 0h Mov P0, #00000000; Send 0 to P0.0 end ; LED ON
1 2 3 P1.0 P1.1 P1.2 VCC P0.0 P0.1 40 39 38

Start:

Start:

Org 0h Setb P0.0 end


Org 0h Clr P0.0 end

; Send 1 to P0.0
/EA 18 XTAL2 XTAL1 GND P2.1 P2.0 22 21 31

Start:

19 20

Triwiyanto - Jurusan Teknik Elektromedik

Percobaan 1.2. LED Blink


Rencanakan program untuk mengedipkan sebuah LED pada P0.0. Solusi: ;====================== Org 0h ;Subrutin untuk membangkitkan Start: Setb P0.0 ;waktu tunda ;====================== Call Delay Delay: Mov R0,#5d Clr P0.0 Del1: DJNZ R0, Del1 Call Delay ret sjmp Start End
Triwiyanto - Jurusan Teknik Elektromedik

3.1. LCD Karakter 2x16


8951

LCD CHARACTER 2 X 16
D0-D7
1 2 3 16

RS ( Register Select ) RS = 0 Command register RS = 1 Data Register R/W ( Read/ Write ) R/W= 0 Write R/W = 1 Read EN ( Enable ) Used to latch the data present on the data pins. A high-to-low edge is needed to latch the data. D0 D7 Bi-directional data/command pins. Alphanumeric characters are sent in ASCII format

P0.0 - P0.7

A 1 4
P3.6 P3.7 RS EN R/W

0 7
16

g L
1 2

Triwiyanto - Jurusan Teknik Elektromedik

3.2. Pin dan Fungsi


Pin 1 Simbol Vss Nama Signal DB0 DB7 E R/W Fungsi Data - Untuk mengirimkan data karakter atau dan instruksi Enable - Signal start untuk mulai pengiriman data atau instruksi Read/ Write - Signal yang digunakan untuk memilih mode baca atau tulis 0 : tulis 1 : baca Register Select 0: Instruction register (Write) 1: Data register (Write, Read)

2
3 4 5 6 7-14 15 16

Vcc
Vee RS R/W E DB0 BPL GND RS

Vee
Vcc Vss

Tegangan pengaturan kontras pada LCD


Tegangan Vcc Tegangan 0V atau Ground
Triwiyanto - Jurusan Teknik Elektromedik

3.3. Inisialisasi LCD Karakter


Function Set
RS 0 R/W 0 DB7 0 DB6 0 DB5 1 DB4 DL DB3 N DB2 F DB1 X DB0 X

DL : Set Data Length (1= data length 8 bit, 0= data length 4 bit ) N : Set jumlah baris ( 1= 2 line display, 0=1 line display) F : Set Character font (1= 5 x 10 dots; 0= 5 x 7 dots ) Entry Mode Set
RS R/W
0 0

DB7
0

DB6
0

DB5
0

DB4
0

DB3
0

DB2
1

DB1
I/D

DB0
S

I/D : Set increment/ decrement ( 1= increment, 0= decrement ) S : Display Sift ( 1=on, 0=off ) Display On/ Off Control
RS 0 R/W 0 DB7 0 DB6 0 DB5 0 DB4 0 DB3 1 DB2 D DB1 C DB0 B

D : display On ( 1= display on) B : Cursor blink ( 1= blink on,0=blink off ) C : Cursor display (1=cursor on, 0=cursor off)
Triwiyanto - Jurusan Teknik Elektromedik

Cursor of Display Sift


Shifts the cursor position or display to the right or left without writing or reading display data. This function is used to corect or search for the display RS 0 R/W 0 DB7 0 DB6 0 DB5 0 DB4 1 DB3 S/C DB2 R/L DB1 X DB0 X

Note : x = Dont care


S/C 0 R/L 0 Note Shift cursor position to the left

0
1 1

1
0 1

Shift cursor position to the right


Shift the entire display to the left Shift the entire display to the right

Triwiyanto - Jurusan Teknik Elektromedik

Clear Display
RS 0 R/W 0 DB7 0 DB6 0 DB5 0 DB4 0 DB3 0 DB2 0 DB1 0 DB0 1

Dengan menuliskan instruksi tersebut maka semua tamplan akan reset

Ram Display Address ( in Hexa )


Baris 1
Baris 2
00
40

01
41

02
42

03
43

04
44

05
45

06
46

07
47

08
48

09
49

0A 0B 0C 0D 0E 0F
4A 4B 4C 4D 4E 4F

.
.

Instruksi Pemilihan RAM


RS 0 R/W 0 DB7 1 DB6 A DB5 A DB4 A DB3 A DB2 A DB1 A DB0 A

A : Lokasi memori display 0000000 1111111 ( 00 h 7F h )


Triwiyanto - Jurusan Teknik Elektromedik

3.4. LCD ( Cetak sebuah karakter A)


A
org 0h mov r1,#00000001b ;Display clear call write_inst mov r1,#00111000b ;Function set,Data 8 bit,2 line font 5x7 call write_inst mov r1,#00001100b ;Display on, ;cursor off,cursor blink off call write_inst mov r1,#00000110b ;Entry mode, Set increment call write_inst start: mov R1,#80h call write_inst mov R1,#'A' Memanggil call write_data subrutine stop: sjmp stop

Subrutine Write_inst Write_data

Write_inst: clr P3.6 mov P0,R1 setb P3.7 call delay clr P3.7 ret ; Write_data: setb P3.6 mov P0,R1 setb P3.7 call delay clr p3.7 ret end ; P3.6 = RS =1 ; copy R1 ke P0 ; P3.7 =EN = 1 ; call delay time ; P3.7 =EN = 0 ; P3.6 = RS =0 ; copy R1 ke P0 ; P3.7 =EN = 1 ; call delay time ; P3.7 =EN = 0

Triwiyanto - Jurusan Teknik Elektromedik

My heart beat periode is 1 Hz now

COUNTER UP/DOWN UP 4 BIT Co 74193 Load


D3 D2 D1 D0

Watch out !, you may miss my overflow every 10s. If you get sleep
Think smart, you must load data : 16-10 = 6 or 0110 b on me What a number to be loaded to my register, so I can get pulse out every 10 s on Co

COUNTER UP Clock 16 BIT TF1


TH1(D15..D8) TL1(D7..D0)

I just finished My work out. So, my heart Beat is 1 MHz, Now

If you loaded, TL1 =00 TH1 =00, then you, need 65536 pulse. To Get overflow on my TF1
Triwiyanto - Jurusan Teknik Elektromedik

If my periode input clock is 1 u Then I wil interupt you, after 1 uS x 65536 = 0.065536 s

9. Basic Timer/ Counter


Over flow, after 20000 us = 0.02 s

COUNTER UP Clock 16 BIT TF1


Clock = 1 Mhz
TH1(D15..D8) TL1(D7..D0)

Data to be loaded to TL1 and TH1 Register

0B1h 0E0h To get over flow after 20000 uS or 0.02 s : 65536 20000 = 45536 or 0B1E0 h To get over flow after 10000 uS or 0.01 s : 65536 10000 = 55536 or 0D8F0 h

Triwiyanto - Jurusan Teknik Elektromedik

THE Action of 89s51 aS COUNTER OR TIMER


Put me, up or down. Up = working as timer Down = working as counter, in register TMOD Close my swicth, To get run as Timer or COunter

89C51
0 = S1 keatas 1 = S1 kebawah XTALL/ 12 0 = S2 buka 1 = S2 tutup

S1

S2 TMOD

TL1

TH1

Timer1
T1/ P3.5

Timer 0

G C/T M1 M0 G C/T M1 M0

Dont get sleep, If you dont want miss my overflow, test me by Software !

INT1/ P3.3

TCON TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Limpahan Timer/ Counter

Triwiyanto - Jurusan Teknik Elektromedik

MORE ABOUT TIMER/ COUNTER 8951

TMOD REGISTER
Gate C/T M1 M0 Gate C/T M1 M0

TIMER 1

TIMER 0

M1 0 0 1

M0 0 1 0

OPERATING
Mode 0: 13 bit Timer/ Counter Mode 1: 16 bit Timer/ Counter Mode 2: 8 bit auto reload Timer/ Counter. THx menerima data yang akan direload ke TLx setiap kali TLx overflow Mode 3: 8 bit Timer/ Counter by TL0. TH0 merupakan 8 bit Timer yang dikontrol dari Timer 1
Triwiyanto - Jurusan Teknik Elektromedik

TCON REGISTER ( BIT ADDRESSABLE REGISTER)


TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TIMER 1
BIT TCON.7 SYMBOL TF1

TIMER 0

TIMER 1
FUNCTION

TIMER 0

Timer 1 overflow flag. Set by harware on timer/counter overflow. Clear by hardware when processor vectors to interupt routine, or clearing by software Timer 1 run control bit. Set/cleared by software to turn timer/counter on/off Idem timer 0 Idem timer 0 Interupt 1 Edge flag, set by harware when external interrupt edge detected. Cleared when interupt processed Interrupt 1 type control bit. Set/ Cleared by software to specify falling edge/ low level triggered external interupt Idem timer 0 Idem timer 0
Triwiyanto - Jurusan Teknik Elektromedik

TCON.6 TCON.5 TCON.4 TCON.3

TR1 TF0 TR0 IE1

TCON.2 TCON.1 TCON.0

IT1 IE0 IT0

MODE 1 : TIMER 16 BIT ON TIMER 1


They are trying to configure my function as a timer in MODE 1,by MOV TMOD,#10H

If your xtall is 12MHz Then I wil generate 1 Mhz

89C51
TH1

Please load a number to my register ( TL1, TH1) tobe counted, by MOV TH1,#0D8h MOV TL1,#0EFh
P1.0

0 = S1 keatas 1 = S1 kebawah XTALL/ 12

0 = S2 buka 1 = S2 tutup

S1 TMOD 0 0 0 1 0 0 0

S2

TL1

COUNTER 16 BIT 0

T1/ P3.5

Dont move From your seat keep spy on me, Maybe I get Over flow

INT1/ P3.3 TCON

TR1

TF1

OSCILOSCOPE

Lets go, to rock and roll By, SETB TR1

Read my Frequency !
Triwiyanto - Jurusan Teknik Elektromedik

Contoh Soal
1. Sebuah led dihubungkan pada P0.7 melalui sebuah resistor, dengan katoda terhubung ke ground.Rencanakan program assembly untuk membuat subrutine waktu tunda, agar led dapat berkedip dengan Ton=0.5 detik dan Toff=0.5 detik. Gunakan Timer1 mode 13 bit. Idem. Rencanakan program assembly untuk membuat subrutine waktu tunda, agar LED dapat berkedip dengan Ton=2 detik dan Toff=1 detik. Gunakan Timer1 mode 16 bit.

2.

Triwiyanto - Jurusan Teknik Elektromedik

Contoh Soal 2
1. Sebuah pin P0.7 digunakan sebagai pembangkit osilasi. Rencanakan program assembly untuk membuat subrutine waktu tunda, agar dapat dibangkitkan clock dengan Ton=0.05 detik dan Toff=0.05 detik. Gunakan Timer1 mode 2. Idem. Ton=0.02 detik dan Toff=0.04 detik. Gunakan Timer0 mode 2.

2.

Triwiyanto - Jurusan Teknik Elektromedik

MODE 2 : 8 BIT AUTO-RELOAD


0 = S1 keatas 1 = S1 kebawah XTALL/ 12

89C51
0 = S2 buka 1 = S2 tutup COUNTER 16 BIT

S1 TMOD 0 0 1 0 0 0 0

S2

TL1( 8 BIT )

LATCH

See, my job is very important, P1.0 to reload the data TH1 to TL1 register when overflows

T1/ P3.5 TH1 (8 BIT)

INT1/ P3.3 TCON

TR1

TF1

Always keep On eye on my TF1 bit. Never Forget

Get rock and Look, my con- roll by fuguration as SETB TR1 timer mode 2, now

OSCILOSCOPE

Triwiyanto - Jurusan Teknik Elektromedik

MODE 3 : COUNTER 8 BIT ON TIMER1


0 = S1 keatas 1 = S1 kebawah XTALL/ 12 0 = S2 buka 1 = S2 tutup

89C51
TL1 TF1
Watch out !, dont get sleep till you Catch me, if In logic 1. Its Mean I am in Overflow

S1 TMOD 0 1 1 1 0 0 0

S2

COUNTER 8 BIT 0

T1/ P3.5

1
INT1/ P3.3

Give me a number To configure my mode by software:


TCON TR1
MOV TMOD,#01110000b

Look at to my pin, I am so useless. Because The gate on TMOD= 0 When do I can get action?

Well, to start the all system as a counter/timer Set on me by software: SETB TR1
Triwiyanto - Jurusan Teknik Elektromedik

POOLING OR INTERUPTING SYSTEMS


RUTINITAS ATAU PRIORITAS KARENA INTERUPSI
05.00 BANGUN PAGI

06.00 SARAPAN

07.00 KULIAH

22.00 TIDUR

15.00 KERJAKAN TGS.

INTR

19.00 BELAJAR
Triwiyanto - Jurusan Teknik Elektromedik

18.00 MAIN

INTERUPSI MIKROKONTROLLER

Triwiyanto - Jurusan Teknik Elektromedik

INTERRUPT ENABLE REGISTER ( IE )


MSB LSB

EA
BIT IE.7

X
SYMBOL EA

ES

ET1

EX1

ET0

EX0

FUNCTION

Disables all interrupts.If EA=0, no interrupt will be acknoledged. If EA=1,each interrupt source is individually enabled/ dis.by software
Reserved Reserved Enables or dis. the serial port interrupt, if ES=0, disabled Enables or dis. the timer 1 overflow interrupt, if ET1=0, disabled Enables or dis. External interrupt 1.If EX1=0, disabled Enables or dis. the timer 0 overflow interrupt, if ET0=0, disabled Enables or dis. External interrupt 0.If EX0=0, disabled
Triwiyanto - Jurusan Teknik Elektromedik

IE.6 IE.5 IE.4 IE.3

ES ET1

IE.2 IE.1

EX1 ET0

IE.0

EX0

TCON REGISTER ( BIT ADDRESSABLE REGISTER)


TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TIMER 1
BIT TCON.7 SYMBOL TF1

TIMER 0
FUNCTION Timer 1 overflow flag. Set by harware on timer/counter overflow. Clear by hardware when processor vectors to interupt routine, or clearing by software Timer 1 run control bit. Set/cleared by software to turn timer/counter on/off Idem timer 0 Idem timer 0 Interupt 1 Edge flag, set by harware when external interrupt edge detected. Cleared when interupt processed Interrupt 1 type control bit. Set/ Cleared by software to specify falling edge/ low level triggered external interupt Idem Interupt 0 Idem Interupt 0
Triwiyanto - Jurusan Teknik Elektromedik

TCON.6 TCON.5 TCON.4 TCON.3

TR1 TF0 TR0 IE1

TCON.2 TCON.1 TCON.0

IT1 IE0 IT0

ALAMAT INTERUPSI
Source
IE0 TF0 IE1 TF1 RI + TI
Triwiyanto - Jurusan Teknik Elektromedik

Vector Address
0003H 000BH 0013H 001BH 0023H

STRUKTUR PROGRAM
Org 0000h Ljmp Start Org 000bh Ljmp Timer0Interupt Start: ; Instruksi Rutinitas A ; Instruksi Rutinitas B ; Instruksi Rutinitas C Sjmp Start; { Looping Forever } ; Timer0Interupt:
; Instruksi ; Instruksi

Reti End
Triwiyanto - Jurusan Teknik Elektromedik

Interupsi Timer
1. Dengan memanfaatkan interupsi TF0 yang dibangkitkan setiap 10.000us, maka lakukan increment data pada Accumulator dan outputkan ke LED setiap 2 detik. 2. Dengan memanfaatkan interupsi TF1 yang dibangkitkan setiap 20.000us, maka lakukan decrement data pada Accumulator dan outputkan ke 7 Segmen setiap 1 detik, dengan nilai awal 60. 3. Buatlah jam digital bagian detik yang menghitung mulai dari 00 s/d 99 detik, dengan interupsi yang dibangkitkan dari TF0 dengan waktu interupsi 8.000us dengan mode 0 timer 13 bit.

Triwiyanto - Jurusan Teknik Elektromedik

Contoh: Interupsi Eksternal 0


INT0 P0.0 Rencanakan program untuk membunyikan buzer, dengan menggunakan interupsi eksternal 0, pada saat transisi negatif dengan syarat sbb: 1. Jika tidak ada interupsi transisi negatif, mikrokontroller memerintahkan buzer untuk berhenti berbunyi. 2. Jika ada interupsi transisi negatif, maka mikrokontroller akan memerintahkan buzer untuk berbunyi. Delay: mov R7,#255 Del1: mov R6,#255 AdaIntExt0: setb P0.0 djnz R6,$ call delay djnz R7,Del1 reti ret

Org 0h Sjmp start Org 03h ljmp AdaIntExt0 ; Start: call InitIntExt0 InitIntExt0: setb IT0 Forever:clr P0.0 setb EX0 sjmp Forever setb EA ret
Triwiyanto - Jurusan Teknik Elektromedik

2.
INT1 P0.7

Rencanakan program untuk membunyikan buzer, dengan menggunakan interupsi eksternal 1, pada saat level logika high dengan syarat sbb: 1. Jika tidak ada interupsi transisi negatif, mikrokontroller memerintahkan buzer untuk berhenti berbunyi. 2. Jika ada interupsi transisi negatif, maka mikrokontroller akan memerintahkan buzer untuk berbunyi.

Triwiyanto - Jurusan Teknik Elektromedik

Project Interupsi Eksternal


1. Rencanakan program untuk menanggapi interupsi transisi negatif dengan komentar bahaya dan ok 2. Rencanakan program untuk menanggapi interupsi dengan increment data pada A dan display pada 7 segmen.

Triwiyanto - Jurusan Teknik Elektromedik

Contoh Interupsi Timer 0


Rencanakan program untuk menggeser data accumulator, setiap 1 detik, dengan output data ke LED yang terhubung ke P0. Gunakan timer 0 16 bit.
Org 0h Ljmp Start Org 0bh Ljmp InterupsiTF0 mov R7,#....... mov A,#00000001b call InitInterupsi call InitTimer0 mov P0,A sjmp hang

InitInterupsi:

setb ET0 setb EA ret

Start:

; InterupsiTF0:

; Hang:

; InitTimer0: mov TMOD,#..... mov TL0,#...... mov TH0,#..... setb TR0 ret

call InitTimer0 djnz R7, EndInterupsiTF0 RL A mov R7,#...... EndInterupsiTF0: reti ; end

Triwiyanto - Jurusan Teknik Elektromedik

2. Rencanakan program untuk menghidupkan LED yang terhubung ke P0.0 selama satu detik, dan mematikan LED tersebut selama satu detik juga. Gunakan interupsi timer 1 dan counter 1 dengan mode 13 bit. 3. Rencanakan program untuk increment data output ke LED yang terhubung ke P0 ( 0 s/d 255 ) setiap 1 detik. Gunakan interupsi timer 0 dan counter 0 dengan mode 16 bit.

Triwiyanto - Jurusan Teknik Elektromedik

INTERRUPT PRIORITY REGISTER ( IP )


MSB LSB

X
BIT
IP.7 IP.6 IP.5 IP.4

X
SYMBOL
PS

PS

PT1

PX1

PT0

PX0

FUNCTION
Reserved Reserved Reserved Defines the serial port interrupt. PS=1 programs it to the higher priority

IP.3
IP.2

PT1
PX1

Defines the timer 1


Defines the external 1

IP.1
IP.0

PT0
PX0

Defines the timer 0


Defines the external 0
Triwiyanto - Jurusan Teknik Elektromedik

Soal-soal latihan
1. Rencanakan program untuk menghitung pulsa yang diinputkan pada T0 dan data hasil cacahan dioutputkan pada P0 dengan konfigurasi sebagai counter 8 bit. Catatan: Manfaatkan input pada INT0 untuk membekukan proses pencacahan melalui saklar push button, bila saklar ditekan pencacahan akan terjadi, bila saklar dilepas maka pencacahan tidak terjadi. 2. Idem, sebagai counter 13 bit input pada T1, input eksternal pada INT1. dengan output pada P0 ( 8 bit ) dan P1 (5 bit) 3. Rencanakan program untuk membangkitkan waktu tunda 0,5 detik sehingga led akan tampak berkedip ( bila led dihubungkan pada P2.0 ), bila timer yang digunakan adalah TIMER 1 13 bit dan bila interupsi yang dibangkitkan adalah setiap 8000 udetik 4. Rencanakan program untuk mendeteksi interupsi level logika pada INT1. dengan output display ke seven segmen common anoda yang terhubung ke P0.0 s/d P0.6 dan driver transistor PNP terhubung ke P2.0. Catatan, bila ada interupsi maka pada display akan keluar angka 1 dan bila tidak ada interupsi maka akan keluar angka 0. 5. Idem pada INT0, dengan output ke Buzer yang terhubung ke P0.0 dan LED yang terhubung ke P0.1. Catatan bila ada interupsi maka Buzer akan berbunyi dan LED nyala, bila tidak ada interupsi maka Buzer akan mati dan LED off.
Triwiyanto - Jurusan Teknik Elektromedik

The Power of Serial Comm. RS232


SINKRON KOMUNIKASI
1. Dua buah divais diinisialisasi atau disinkronisasi menggunakan clock yang sama dan secara kontinu dapat mengirimkan atau menerima karakter untuk tetap sinkron. 2. Komunikasi sinkron mempunyai kecepatan yang lebih tinggi bila dibandingkan dengan asinkron, karena tidak ada penambahan bit, untuk menandai permulaan dan akhiran byte data. 3. Port Serial pada PC adalah asinkron divais, sehingga hanya support untuk komunikasi serial asinkron
DATA D0 D1 D2 D3 D4 D5 D6 D7

CLOCK
Triwiyanto - Jurusan Teknik Elektromedik

ASINKRON KOMUNIKASI
1. Asinkron berarti tanpa sinkronisasi, sehingga tidak diperlukan pengiriman sinyal clock. 2. Akan tetapi, pada setiap awalan dan akhiran harus ditandai dengan bit start dan bit stop. 3. Start bit menunjukkan, bahwa data akan segera dikirim atau diterima, dan bit stop menyatakan akhiran dari sinyal. 4. Keperluan penambahan pengiriman dua bit ini akan menyebabkan komunikasi asinkron akan lebih lambat bila dibandingkan dengan komunikasi sinkron. 5. Pada jalur asinkron, kondisi idle dinyatakan sebagai nilai 1 ( yang juga disebut sebagai keadaan mark )

Triwiyanto - Jurusan Teknik Elektromedik

PANJANG KABEL KOM. RS232


1. Standart RS232 menyarankan batasan panjang kabel 50 feet ( 1 m = 3,3 feet ) 2. Sesungguhnya kita dapat mengabaikan standard ini, karena kabel dapat lebih panjang dari 10.000 feet pada baudrate sampai 19200 bps, jika kita menggunakan kabel yang berkualitas dan terlindungi.
Baudrate BPS 9600 4800 2400 1200 300 Shielded Cable Length (feet) 250 500 2000 3000 4000 Unshielded Cable Length (feet) 100 250 500 500 1000

110

5000
Triwiyanto - Jurusan Teknik Elektromedik

1000

Standart RS232
RS232 LEVEL

Triwiyanto - Jurusan Teknik Elektromedik

10. Interfacing Serial Port pada Mikrokontroller


Interfacing port serial lebih sulit dibandingkan dengan port parallel. Pada beberapa peralatan telah memiliki port serial untuk berkomunikasi dengan komputer atau peralatan lain.

Apa keuntungan menggunakan serial komunikasi ?


1. Kabel serial dapat lebih panjang dibandingkan kabel parallel. Port Serial mentransmisikan logika 1 dengan tegangan ~ -25V dan logika 0 dengan tegangan ~ +25V. Dibandingkan parallel : 0 dan 5 V. Cable Loss bukan menjadi masalah utama pada transmisi data secara serial. 2. Anda tidak memerlukan kabel yang banyak, dibandingkan parallel. Hanya perlu 3 kabel, bandingkan dengan cara parallel, sehingga biaya lebih murah.

Triwiyanto - Jurusan Teknik Elektromedik

SERIAL COMMUNICATION ON THE RUN


YOU MAY CONTROL YOUR DEVICE FROM REMOTE PLACE, JUST BY THREE CABLE

My job is to convert RS232 level to TTL Logic Level


89C51
P3.0/ RXD P3.1/ TXD

RS232
T1OUT RD(2) TD(3) GND (5) R1IN GND R1OUT T1IN

USE MY PORT SERIAL COMM 1,COMM 2 Etc. ( DB 9 )

Start

2400 kbps
Triwiyanto - Jurusan Teknik Elektromedik

Stop

Port Serial PC
Pin
1

Assignment
DCD

Description
Data carrier detect

2 3
4

RXD TXD
DTR

Receive data Transmit data


Data terminal ready

5
6

GND
DSR RTS

Signal ground
Data set ready Request to send

Base Address: -3F8 h -2F8 h

8
9

CTS
RI

Clear to send
Ring indicator

Triwiyanto - Jurusan Teknik Elektromedik

KONVERTER RS232 TO TTL/ TTL TO RS232


P3.0 ( Rx ) P3.1 ( Tx )
VCC

16
VCC

MAX232
R1OUT R2OUT T1OUT T2OUT 12 9 14 7

PC DB9 Male
1 6 2 7 3 8 4 9 5 P1

10u
+

13 8 11 10 1 3 4 5 2 6

10u
+

R1IN R2IN T1IN T2IN C+ C1C2+ C2V+ V-

10u
+

10u

15

Triwiyanto - Jurusan Teknik Elektromedik

Converter RS232 to TTL


+5V 16 1 3 4 5 C1+ C1C2+ C2+5V 11 10 12 T1IN +5V T2IN R1OUT T2OUT R1IN 7 13 T1OUT 14 VCC 2 6 V+ V-

R2OUT

R2IN

15

Triwiyanto - Jurusan Teknik Elektromedik

WHAT KIND OF SERIAL MODE DO YOU LIKE TO RUN ?


THE SERIAL PORT CAN OPERATE IN 4 MODE 1. Mode O : Mode Sinkron, data dikirim dan diterima melalui P3.0, P3.1 untuk menyalurkan
clock

2. Mode 1 : 10 bit are transmitted ( through TxD ) or receive ( through RxD ), a start bit (0), a 8 bit data ( LSB first ), and a stop bit (1). On receive, the stop bit goes into RB8 in SFR SCON. The baud rate is variable. 3. Mode 2 : 11 bit are transmitted ( through TxD ) or receive ( through RxD ), a start bit (0), a 8
bit data ( LSB first ), 9th bit and a stop bit (1). 4. Mode 3 : idem mode 2, baudrate dapat diatur

SBUF - SERIAL BUFFER REGISTER D7 D6 D5 D4 D3 D2 D1 D0

SBUF merupakan SFR (Special Function Register) yang terletak pada memori-data internal dengan nomor $99. SBUF mempunyai kegunaan ganda, data yang disimpan pada SBUF akan dikirim keluar MCS51 lewat port seri, sedangkan data dari luar MCS51 yang diterima port seri diambil dari SBUF pula
Triwiyanto - Jurusan Teknik Elektromedik

SCON - SERIAL PORT CONTROL REGISTER SM0 SM0 0 0 1 1 SM1 SM1 0 1 0 1 SM2 MODE 0 1 2 3 REN TB8 RB8 TI Baudrate TETAP (fosc/12) VARIABEL TETAP (fosc/32 dan fosc/64) VARIABEL RI

Keterangan Sinkron UART 8 BIT UART 9 BIT UART 9 BIT

SM2 REN TB8 RB8

Enables the multiprocessor communication feature in Modes 2 and 3. In Mode 2 or 3, if SM2 is set to 1, then Rl will not be activated if the received 9th data bit (RB8) is 0. In Mode 1, if SM2=1 then RI will not be activated if a valid stop bit was not received. In Mode 0, SM2 should be 0. Enables serial reception. Set by software to enable reception. Clear by software to disable reception. The 9th data bit that will be transmitted in Modes 2 and 3. Set or clear by software as desired. In Modes 2 and 3, is the 9th data bit that was received. In Mode 1, it SM2=0, RB8 is the stop bit that was received. In Mode 0,RB8 is not used

TI RI

Transmit interrupt flag. Set by hardware at the end of the 8th bit time in Mode 0, or at the beginning of the stop bit in the other modes, in any serial transmission. Must be cleared by software. Receive interrupt flag. Set by hardware at the end of the 8th bit time in Mode 0, or halfway through the stop bit time in the other modes, in any serial reception (except see SM2). Must be cleared by software.
Triwiyanto - Jurusan Teknik Elektromedik

Menggunakan Timer 1 untuk membangkitkan Baud Rate


Ketika timer 1 digunakan untuk membangkitkan clock baud rate, baud rate pada mode 1 dan 3 adalah ditentukan oleh laju overflow timer 1 dan nilai dari SMOD. Penentuan baud rate untuk mode 1 dan 3 adalah sebagai berikut:

Interupsi timer 1 harus disable pada aplikasi ini. Pada kebanyakan aplikasi, timer ini dioperasikan sebagai timer, dengan mode auto reload mode 2. Pada kasus ini baud rate diberikan dengan rumus sebagai berikut:

Triwiyanto - Jurusan Teknik Elektromedik

PCON - POWER CONTROL REGISTER SMOD

GF1

GF0

PD

IDL

NOTE:
Baudrate untuk mode 2 bergantung pada nilai bit SMOD pada register PCON. Jika SMOD=0, baudratenya 1/64 frekuensi oscilator, jika SMOD=1 maka baudratenya 1/32 frekuensi oscilator

Baudrate untuk Timer1 sebagai generator Baudrate


Baud Rate Frekuensi Kristal SMOD Nilai Isi Ulang TH1 Baud rate Aktual

9600
2400 1200

12 MHz
12 MHz 12 MHz

1
0 0
Triwiyanto - Jurusan Teknik Elektromedik

F9h
F3h F6h

8923
2400 1202

Kirim Data ke PC
3 cables

MAX232
T1OUT R1IN GND R1OUT T1IN

Converter S2P RXD TXD

Triwiyanto - Jurusan Teknik Elektromedik

Software pada Mikrokontroller:


initserial: org 0h mov scon,#52h ; serial mode 1 mov tmod,#20h ; Baud rate 2400 BPS mov th1,#0F3H setb tr1 Mov A,#A

TestTi:

jnb ti,TestTi; clr ti ; mov sbuf,a ; end


Triwiyanto - Jurusan Teknik Elektromedik

Software pada PC ( Delphi/ VB dll. ):


Procedure Initserial; procedure TForm1.Timer1Timer(Sender: TObject); Begin begin {bla-bla-bla} asm {buka catatan masa lalu} {bla-bla-bla} {baud rate, 8 bit data, no parity} Receive_Data_Serial; {buka catatan masalalu } edit1.text:=inttostr(data); end; end; End; Procedure Receive_Data_Serial; begin asm mov dx,$3F8 in al,dx mov data,al end end; procedure TForm1.Button1Click(Sender: TObject); begin initserial; timer1.enabled:=true; end;

Triwiyanto - Jurusan Teknik Elektromedik

Kirim Data ke Mikrokontroller


3 cables

MAX232 +5V
T1OUT R1IN GND R1OUT T1IN

RXD TXD

P0.0 P0.1 P0.2 P0.3 P0.4 P0.5 P0.6 P0.7

Triwiyanto - Jurusan Teknik Elektromedik

Software pada PC ( Delphi/ VB dll. ):


Procedure Initserial; Begin asm {bla-bla-bla} {baud rate, 8 bit data, no parity} {buka catatan masalalu } end; End; Procedure Send_Data_Serial; begin asm mov dx,base mov al,data out dx,al end end; procedure TForm1.FormCreate(Sender: TObject); begin Initserial; end; procedure TForm1.Button1Click(Sender: TObject); begin {bla-bla-bla} {baud rate, 8 bit data, no parity} {buka catatan masalalu } data:=1; Send_Data_Serial; end;

Triwiyanto - Jurusan Teknik Elektromedik

Software pada Mikrokontroller:


org 0h call initserial gets: call inchar mov P0,a sjmp gets initserial: mov scon,#52h ; mov tmod,#20h ; mov th1,#0F3H setb tr1 ret

inchar: detect: jnb ri,detect ; clr ri mov a,sbuf ret

Triwiyanto - Jurusan Teknik Elektromedik

OS anda WINXP
Tambahkan komponen ComPort !. Download saja!

Triwiyanto - Jurusan Teknik Elektromedik

WHAT DO YOU THINK ABOUT INTERRUPTS


89C51
POLLING SEQUENCE HIGH PRIORITY INTERRUPT
IP PS PT1 PT0 PX1 PX0 EA IE ES ET1 TCON ET0 EX1 EX0 0 = SW LEFT 1 = SW RIGHT 1 = SW LEFT 0 = SW RIGHT 0 = SW LEFT 1 = SW RIGHT

What do you like ? pooling by waiting the sequence, or interupting as you like Look, my job is very Important, to decide Using the interupt or not Choose by soft. What kind interupt Do you like
To detect transition, or logic level

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0


TIMER 0 TIMER 1

0= 1=

SW UP SW DOWN

RxD/ P3.0

UART

INT0/ P3.2

TxD/ P3.1

INT1/ P3.3

Triwiyanto - Jurusan Teknik Elektromedik

Anda mungkin juga menyukai