SlideShare a Scribd company logo
Triwiyanto - Jurusan Teknik
Elektromedik
A Software
Oriented Device
Triwiyanto - Jurusan Teknik
Elektromedik
uC
Device
Programmer
APAKAH MIKROKONTROLLER ?
uC :sebuah komputer
didalam chip untuk
mengontrol peralatan
elektronik
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
0001110101
0101010101
0101010101
0101010101
Compile
(MIDE-51)
PROGRAM
Download
(ATMEL Soft.)
01000101001
01111000100
01001010100
00111101010
Test
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
CPU
General-
Purpose
Micro-
processor
RAM ROM I/O
Port
Timer
Serial
COM
Port
Data Bus
Address Bus
General-Purpose Microprocessor System
• CPU for Computers
• No RAM, ROM, I/O on CPU chip itself
• Example: Intel’s x86, Motorola’s 680x0
Many chips on mother’s board
General-purpose microprocessor
Triwiyanto - Jurusan Teknik
Elektromedik
RAM ROM
I/O
PORT
TIMER
PORT
SERIAL
Microcontroller
CPU
• A smaller computer
• On-chip RAM, ROM, I/O ports...
• Example: Motorola’s 6811, Intel’s 8051,
Zilog’s Z8 and PIC 16X
A single chip
Microcontroller :
Triwiyanto - Jurusan Teknik
Elektromedik
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
Microprocessor vs. Microcontroller
Triwiyanto - Jurusan Teknik
Elektromedik
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.
Tiga kriteria dalam memilih mikrokontroller
Triwiyanto - Jurusan Teknik
Elektromedik
1.1. Diagram blok
CPU
On-chip
RAM
On-chip
Flash
ROM
4 I/O Ports
Timer 0
Serial
Port
OSC
Interrupt
Control
External interrupts
Timer 1
Timer/Counter
Bus
Control
TxD RxD
P0 P1 P2 P3
Address/Data
Counter
Inputs
1. ARSITEKTUR
Triwiyanto - Jurusan Teknik
Elektromedik
1.2 Comparison of the 8051 Family Members
Feature 8031 89C51 89S51
ROM (bytes) 0K 4k 4k
RAM (bytes) 128 128 128
Timers 2 2 2
I/O pins 32 32 32
Serial port 1 1 1
Interrupt sources 6 6 6
Programming ROM Ext Parallel Serial/ ISP
Triwiyanto - Jurusan Teknik
Elektromedik
1.3. Memory mapping in 8051
1.3.1. ROM memory map in 8051 family
0000H
0FFFH
0000H
1FFFH
AT89C51
AT89S51 AT89C52
AT89S52
4k 8k
from Atmel Corporation
Triwiyanto - Jurusan Teknik
Elektromedik
DIRECT/
INDIRECT
ADDRESSING
128 RAM
LOWER
DIRECT
ADDRESSING
128 RAM
SFR
0
7F H
80 H
FF H
INDIRECT
ADDRESSING
128 RAM
UPPER
80 H
FF H
4 K ROM
INTERNAL
0000 H
0FFF H
RAM INTERNAL
P0
/EA
ALE
P2
/RD
/WR
8951
Register Nilai ( biner )
ACC 00000000
B 00000000
PSW 00000000
SP 00000111
DPTR 00000000
P0-P3 11111111
IP 00000000
IE 00000000
TMOD 00000000
TCON 00000000
THO 00000000
TL0 00000000
TH1 00000000
TL1 00000000
SCON 00000000
1.3.2. Memory map in 89s51 family
Triwiyanto - Jurusan Teknik
Elektromedik
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
1.4. Register and Port
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 00 0000 0000 30
1 01 0000 0001 31
2 02 0000 0010 32
3 03 0000 0011 33
: : : :
9 9 0000 1001 39
10 A 0000 1010 -
11 B 0000 1011 -
12 C 0000 1100 -
13 D 0000 1101 -
14 E 0000 1110 -
15 F 0000 1111 -
Triwiyanto - Jurusan Teknik
Elektromedik
Lanjutan: KONVERSI BILANGAN
Desimal
(BASIS 10)
Hexa
(BASIS 16)
Biner 8 BIT
(BASIS 2)
ASCHII
(CODE)
16 10 0000 0000 -
17 11 0000 0001 -
18 12 0000 0010 -
19 13 0000 0011 -
: : :
25 19 0001 1001 -
26 1A 0001 1010 -
27 1B 0001 1011 -
28 1C 0001 1100 -
29 1D 0001 1101 -
30 1E 0001 1110 -
31 1F 0001 1111 -
Triwiyanto - Jurusan Teknik
Elektromedik
KUNCI BASIS BILANGAN
Biner 8 bit
7
2 6
2 5
2 4
2 3
2 2
2 1
2 0
2
D7
MSB
D6 D5 D4 D3 D2 D1 D0
LSB
128 64 32 16 8 4 2 1
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
CY AC FO RS1 RS0 OV - P
REGISTER PSW
Carry flag CY PSW.7
Auxilarry carry flag AC PSW.6
General purpose F0 PSW.5
Register Bank Select RS1 PSW.4
Register Bank Select RS0 PSW.3
Over Flow OV PSW.2
General purpose - PSW.1
Parity P PSW.0
RS1 RS0 Bank Address
0 0 0 00H-07H
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH
1.5. Memori Data (RAM)
RO R7
R6
R5
R4
R3
R2
R1
RO R7
R6
R5
R4
R3
R2
R1
RO R7
R6
R5
R4
R3
R2
R1
RO R7
R6
R5
R4
R3
R2
R1
OOH
12H
11H
1OH
OFH
O7H
O8H
18H
16H
15H
14H
13H
OEH
ODH
OCH
OBH
O6H
O5H
O4H
O3H
O2H
O1H
OAH
O9H
1EH
1DH
1CH 1FH
1AH
19H 1BH
17H
20 H
2F H
BIT-ADDRESSABLE SPACE
30 H
7F H
RAM SERBAGUNA
BANK 0
BANK 3
BANK 2
BANK 1
P0
P2
SCON SBUF
P1
TH1
TH0
TL1
TL0
TMOD
TCON
PCON
DPH
DPL
SP
B
ACC
PSW
IP
P3
IE
80 H 87 H
88 H
90 H
98 H
A0 H
A8 H
B0 H
B8 H
D0 H
E0 H
F0 H
8F H
8951
Triwiyanto - Jurusan Teknik
Elektromedik
60KROM
EKSTERNAL
4KROM
INTERNAL
0000H
0FFFH
1000H
FFFFH
D0S/DD7
ADDR.
P0
/EA
ALE
PSEN OE
8951 ROMEKSTERNAL
P2
Contoh Program
LOC OBJ LINE SOURCE
0000 1 org 0h
0000 00 2 nop
0001 E5B0 3 start: mov a,p3
0003 F590 4 mov p1,a
0005 80FA 5 sjmp start
0006 6 end
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
1.6. Memori Program ( ROM )
Triwiyanto - Jurusan Teknik
Elektromedik
Pin Description of the 8051
PDIP/Cerdip
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
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
RST
(RXD)P3.0
(TXD)P3.1
(T0)P3.4
(T1)P3.5
XTAL2
XTAL1
GND
(INT0)P3.2
(INT1)P3.3
(RD)P3.7
(WR)P3.6
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
C2
30pF
C1
30pF
XTAL2
XTAL1
GND
• Using a quartz crystal oscillator
• We can observe the frequency on the XTAL2
pin.

Triwiyanto - Jurusan Teknik
Elektromedik
XTAL Connection to an External Clock Source
N
C
EXTERNAL
OSCILLATOR
SIGNAL
XTAL2
XTAL1
GND
• Using a TTL oscillator
• XTAL2 is unconnected.

Triwiyanto - Jurusan Teknik
Elektromedik
Power-On RESET Circuit
30 pF
30 pF
8.2 K
10 uF
+
Vcc
11.0592 MHz
EA/VPP
X1
X2
RST
31
19
18
9

Triwiyanto - Jurusan Teknik
Elektromedik
Power-On RESET with Debounce
EA/VPP
X1
X2
RST
Vcc
10 uF
8.2 K
30 pF
9
31

C:WINDOWShinhem.scr
Triwiyanto - Jurusan Teknik
Elektromedik
2. Set Instruction
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 F9’th 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 72’th byte of
RAM will replace in Accumulator.
Note 2:
MOV A,R3 ≡ MOV A,3
2.1. Instruksi copy data
Triwiyanto - Jurusan Teknik
Elektromedik
Contoh 1 :
org 0h
Start: 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
org 0h
Start: mov 00h,#5
mov 01h,#7
mov 02h,#8
;
mov 10h,#0Ah
mov 11h,#9
mov 12h,#01Bh
end
OR
Contoh 2:
Org 0h
Start: 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
Contoh 3:
Org 0h
Start: mov A,#00000001b
mov P1,A
end
Org 0h
Start: mov 0E0h,#00000001b
mov 90h,0E0h
end
OR
Triwiyanto - Jurusan Teknik
Elektromedik
SETB bit ; bit=1
CLR bit ; bit=0
SETB C ; CY=1
SETB P0.0 ;bit 0 from port 0 =1
SETB P3.7 ;bit 7 from port 3 =1
SETB ACC.2 ;bit 2 from ACCUMULATOR =1
SETB 05 ;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.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. Instruksi Aritmatika
2.1.2 Instruksi Pengurangan
SUBB A,source ;A=A-source-CY
SETB C ;CY=1
SUBB A,R5 ;A=A-R5-1
Triwiyanto - Jurusan Teknik
Elektromedik
Example:
MOV A,#38H
ADD A,#2FH
38 00111000
+2F +00101111
---- --------------
67 01100111
CY=0 AC=1 P=1
Example:
MOV A,#88H
ADD A,#93H
88 10001000
+93 +10010011
---- --------------
11B 00011011
CY=1 AC=0 P=0
Example:
MOV A,#9CH
ADD A,#64H
9C 10011100
+64 +01100100
---- --------------
100 00000000
CY=1 AC=1 P=0
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 byte ;byte=byte-1
INC byte ;byte=byte+1
Example:
INC R7 ;[R7]=[R7]+1
DEC A ;[A]=[A]-1
DEC 40H ; [40]=[40]-1
Triwiyanto - Jurusan Teknik
Elektromedik
Table 1.3. Instruksi Aritmatika
Mnemonic
Operation
Addressing Mode Exect.
Dir Ind Reg Imm Timer uS
Add A,<byte> A=A+<byte> V V V V 1
Addc A,<byte> A=A+<byte>+C V V V V 1
Subb A,<byte> A=A-<byte>-C V V V V 1
Inc A A=A+1 Accumulator Only 1
Inc <byte> <byt>=<byt>+1 V V V 1
Inc DPTR DPTR=DPTR+1 Data Pointer Only 2
Dec A A=A-1 Accumulator Only 1
Dec <byte> <byt>=<byt>-1 V V V 1
Mul AB B:A=BxA Accumulator and B Only 4
Div AB
A=Int[A/B]
B=Mod[A/B]
Accumulator and B only 4
DAA Dec Adjust Accumulator Only 1
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:
D7 D6 D5 D4 D3 D2 D1 D0
RL A ;rotate left A
Contoh:
ORG 0H
START: MOV A,#00000001B
PUTAR: RL A
SJMP PUTAR
END
A = 00000010
A = 00000100
A = 00001000
:
A = 10000000
A = 00000001
A = dst.
CY D7 D6 D5 D4 D3 D2 D1 D0
RLC A ;rotate left and carry A
Contoh:
ORG 0H
START: MOV A,#00000001B
PUTAR: RLC A
SJMP PUTAR
END
A = 00000010
A = 00000100
: :
A = 10000000
C=1 A = 00000000
A = 00000001
dst.
RR A ;rotate right A
RRC A ;rotate right
;and carry A
Triwiyanto - Jurusan Teknik
Elektromedik
Mnemonic Operation
Addressing Mode Exect.
Dir Ind Reg Imm uS
Anl A,<byte> A=A and <byte> V V V V 1
Anl <byte>,A <byte>=<byte>anl A V V V V 1
Anl <byte>,#data <byte>=<byte>and #data V V V V 1
Orl A,<byte> A=A or <byte> Accumulator Only 1
Orl <byte>,A <byt>=<byt>orl A V V V 1
Orl <byte>,#data <byte>=<byte> or #data Data Pointer Only 2
Xrl A,<byte> A=A xor<byte> Accumulator Only 1
Xrl<byte>,A <byt>=<byt>xor A V V V 1
Xrl <byte>,#data <byte>=<byte>xor #data Accumulator and B Only 4
CLR A A=00h Accumulator only 1
CPL A A= not A Accumulator only 1
RL A Rotate A left 1 bit Accumulator only 1
RLC A Rotate A left trough Carry Accumulator only 1
RR A Rotate A right 1 bit Accumulator only 1
RRC Rotate A right trough carry Accumulator only 1
SWAP A Swap nibbles in A Accumulator only 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 A,#65H
MOV A,#’A’
MOV R6,#65H
MOV DPTR,#2343H
MOV P1,#65H
Example :
Num EQU 30
…
MOV R0,Num
MOV DPTR,#data1
…
ORG 100H
data1: db “IRAN”
Triwiyanto - Jurusan Teknik
Elektromedik
2.3.2. Register Addressing Mode
MOV Rn, A ;n=0,..,7
ADD A, Rn
MOV DPL, R6
MOV DPTR, A
MOV Rm, Rn
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 A, R4
MOV 6, 2 ; 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 A,@Ri ; move content of RAM loc.Where address is held by Ri into A
;( i=0 or 1 )
MOV @R1,B
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
DJNZR2,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; MULAB, DIV AB
Triwiyanto - Jurusan Teknik
Elektromedik
TANKS ALOT
TRIMAKASIH
MATUR SUWON
I’LL SEE YOU SOON, IN THE NEXT SEMESTER,
TO ROCK AND ROLL AND HAVING FUN
A K H I R N Y A
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 Jump if A=0
JNZ Jump if A/=0
DJNZ Decrement and jump if A/=0
CJNE A,byte,lompat1 Jump if A/=byte
CJNE reg,#data,lompat1 Jump if byte/=#data
JC Jump if CY=1
JNC Jump if CY=0
JB Jump if bit=1
JNB Jump if bit=0
JBC 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 2K-
byte range.
Triwiyanto - Jurusan Teknik
Elektromedik
Port 0 with Pull-Up Resistors
P0.0
P0.1
P0.2
P0.3
P0.4
P0.5
P0.6
P0.7
8751
8951
Vcc
10 K
Port
0
Triwiyanto - Jurusan Teknik
Elektromedik
Port 3 Alternate Functions
17
RD
P3.7
16
WR
P3.6
15
T1
P3.5
14
T0
P3.4
13
INT1
P3.3
12
INT0
P3.2
11
TxD
P3.1
10
RxD
P3.0
Pin
Function
P3 Bit

Triwiyanto - Jurusan Teknik
Elektromedik
Structure of Assembly language
Label Opcode Operand Comment
ORG 0H ;StartROM
Start: Mov A,#10001000b ; fill A = 10001000b
Mov P0,A ;copy A to P0
END
Desain Program
Assembly
(MIDE-51)
Org 0h
mov A,10000000b
mov P0,A
end
0001110101
0101010101
0101010101
0101010101
Compile
(MIDE-51)
PROGRAM
Download
(ATMEL Soft.)
01000101001
01111000100
01001010100
00111101010
TEST
Triwiyanto - Jurusan Teknik
Elektromedik
Percobaan 1.1. Menghidupkan/ Matikan LED
Org 0h ;Starting ROM 0h
Start: Mov P0, #00000001;Send ’1’ to P0.0
end ;LED OFF
Org 0h
Start: Mov P0, #00000000; Send ‘0’ to P0.0
end ; LED ON
Org 0h
Start: Setb P0.0 ; Send ‘1’ to P0.0
end
Org 0h
Start: Clr P0.0 ;
end
40
39
38
1
2
3
P1.0
P1.1 P0.0
P0.1
5V
P1.2
18
19
20
XTAL2
XTAL1
GND
31
/EA
VCC
22
21
P2.0
P2.1
Triwiyanto - Jurusan Teknik
Elektromedik
Percobaan 1.2. LED Blink
Rencanakan program untuk mengedipkan sebuah
LED pada P0.0.
Solusi:
Org 0h
Start: Setb P0.0
Call Delay
Clr P0.0
Call Delay
sjmp Start
;======================
;Subrutin untuk membangkitkan
;waktu tunda
;======================
Delay: Mov R0,#5d
Del1: DJNZ R0, Del1
ret
End
Triwiyanto - Jurusan Teknik
Elektromedik
3.1. LCD Karakter 2x16
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
A
g
1 4
L
0
7
1 2 3 16
16
1 2
D0-D7
R/W
EN
RS
P0.0 - P0.7
P3.6
P3.7
LCD CHARACTER 2 X 16
8951
Triwiyanto - Jurusan Teknik
Elektromedik
Pin Simbol
1 Vss
2 Vcc
3 Vee
4 RS
5 R/W
6 E
7-14 DB0
15 BPL
16 GND
Nama Signal Fungsi
DB0– DB7 Data - Untuk mengirimkan data karakter atau dan
instruksi
E Enable - Signal start untuk mulai pengiriman data
atau instruksi
R/W Read/ Write - Signal yang digunakan untuk
memilih mode baca atau tulis
‘0’ : tulis
‘1’ : baca
RS Register Select
“0”: Instruction register (Write)
“1”: Data register (Write, Read)
Vee Tegangan pengaturan kontras pada LCD
Vcc Tegangan Vcc
Vss Tegangan 0V atau Ground
3.2. Pin dan Fungsi
Triwiyanto - Jurusan Teknik
Elektromedik
3.3. Inisialisasi LCD Karakter
0 0 0 0 1 DL N F X X
Function Set
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 )
0 0 0 0 0 0 0 1 I/D S
Entry Mode Set
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
I/D : Set increment/ decrement ( 1= increment, 0= decrement )
S : Display Sift ( 1=on, 0=off )
Display On/ Off Control
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 0 0 1 D C B
B : Cursor blink ( 1= blink on,0=blink off )
C : Cursor display (1=cursor on, 0=cursor off)
D : display On ( 1= display on)
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 R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 0 1 S/C R/L X X
Note : x = Dont care
S/C R/L Note
0 0 Shift cursor position to the left
0 1 Shift cursor position to the right
1 0 Shift the entire display to the left
1 1 Shift the entire display to the right
Triwiyanto - Jurusan Teknik
Elektromedik
0 0 1 A A A A A A A
Clear Display
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
Dengan menuliskan instruksi tersebut maka semua tamplan akan reset
0 0 0 0 0 0 0 0 0 1
A : Lokasi memori display
0000000 – 1111111 ( 00 h – 7F h )
Instruksi Pemilihan RAM
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ….
40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F ….
Ram Display Address ( in Hexa )
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
Baris 1
Baris 2
Triwiyanto - Jurusan Teknik
Elektromedik
3.4. LCD ( Cetak sebuah karakter 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'
call write_data
stop: sjmp stop
A
Write_inst:
clr P3.6 ; P3.6 = RS =0
mov P0,R1 ; copy R1 ke P0
setb P3.7 ; P3.7 =EN = 1
call delay ; call delay time
clr P3.7 ; P3.7 =EN = 0
ret
;
Write_data:
setb P3.6 ; P3.6 = RS =1
mov P0,R1 ; copy R1 ke P0
setb P3.7 ; P3.7 =EN = 1
call delay ; call delay time
clr p3.7 ; P3.7 =EN = 0
ret
end
Subrutine
Write_inst
Write_data
Memanggil
subrutine
Triwiyanto - Jurusan Teknik
Elektromedik
Watch out !, you may
miss my overflow every 10s. If
you get sleep
COUNTER UP/DOWN
UP 4 BIT Co
74193
D3 D2 D1 D0
What a number to be
loaded to my register, so
I can get pulse out every
10 s on Co
My heart beat
periode is 1 Hz
now
Think smart, you must load data
: 16-10 = 6 or 0110 b on me
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
If my periode input clock is 1 u
Then I wil interupt you, after
1 uS x 65536 = 0.065536 s
Load
Triwiyanto - Jurusan Teknik
Elektromedik
COUNTER UP
Clock 16 BIT TF1
TH1(D15..D8) TL1(D7..D0)
Clock = 1 Mhz
0E0h
0B1h
Over flow, after 20000 us = 0.02 s
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
Data to be loaded to
TL1 and TH1
Register
9. Basic Timer/ Counter
Triwiyanto - Jurusan Teknik
Elektromedik
THEActionof 89s51 aS COUNTEROR TIMER
XTALL/
12
G C/T M1 M0 G C/T M1 M0
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TL1 TH1
TMOD
TCON
0 = S1 keatas
1 = S1 kebawah
S1
0 = S2 buka
1 = S2 tutup
S2
Limpahan Timer/ Counter
T1/ P3.5
INT1/ P3.3
89C51
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
Don’t get
sleep,
If you
don’t want
miss my
overflow,
test me by
Software !
Timer1 Timer 0
Triwiyanto - Jurusan Teknik
Elektromedik
M1 M0 OPERATING
0 0 Mode 0: 13 bit Timer/ Counter
0 1 Mode 1: 16 bit Timer/ Counter
1 0 Mode 2: 8 bit auto reload Timer/ Counter. THx
menerima data yang akan direload ke TLx
setiap kali TLx overflow
1 1 Mode 3: 8 bit Timer/ Counter by TL0. TH0
merupakan 8 bit Timer yang dikontrol dari
Timer 1
Gate C/T M1 M0 Gate C/T M1 M0
TIMER 1 TIMER 0
MORE ABOUTTIMER/ COUNTER 8951
TMOD REGISTER
Triwiyanto - Jurusan Teknik
Elektromedik
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TCON REGISTER ( BIT ADDRESSABLE REGISTER)
TIMER 1 TIMER 0 TIMER 1 TIMER 0
BIT SYMBOL FUNCTION
TCON.7 TF1 Timer 1 overflow flag. Set by harware on timer/counter overflow. Clear by
hardware when processor vectors to interupt routine, or clearing by
software
TCON.6 TR1 Timer 1 run control bit. Set/cleared by software to turn timer/counter
on/off
TCON.5 TF0 Idem timer 0
TCON.4 TR0 Idem timer 0
TCON.3 IE1 Interupt 1 Edge flag, set by harware when external interrupt edge
detected.
Cleared when interupt processed
TCON.2 IT1 Interrupt 1 type control bit. Set/ Cleared by software to specify falling
edge/ low level triggered external interupt
TCON.1 IE0 Idem timer 0
TCON.0 IT0 Idem timer 0
Triwiyanto - Jurusan Teknik
Elektromedik
MODE1 : TIMER16 BITON TIMER1
XTALL/
12
0 0 0 1 0 0 0 0
TF1
TR1
TL1
TMOD
TCON
0 = S1 keatas
1 = S1 kebawah
S1
0 = S2 buka
1 = S2 tutup
S2
T1/ P3.5
INT1/ P3.3
89C51
COUNTER 16 BIT
TH1 P1.0
OSCILOSCOPE
Please load a
number to my
register ( TL1, TH1)
tobe counted, by
MOV TH1,#0D8h
MOV TL1,#0EFh
If your xtall is 12MHz
Then I wil generate 1
Mhz
Don’t move
From your seat
keep spy on
me, Maybe I
get Over flow
They are trying to configure
my function as a timer in
MODE 1,by MOV TMOD,#10H
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.
2. 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.
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.
2. Idem. Ton=0.02 detik dan Toff=0.04 detik. Gunakan
Timer0 mode 2.
Triwiyanto - Jurusan Teknik
Elektromedik
XTALL/
12
0 0 1 0 0 0 0 0
TF1
TR1
TL1( 8 BIT )
TMOD
TCON
0 = S1 keatas
1 = S1 kebawah
S1
0 = S2 buka
1 = S2 tutup
S2
T1/ P3.5
INT1/ P3.3
89C51
COUNTER 16 BIT
TH1 (8 BIT)
P1.0
OSCILOSCOPE
LATCH
MODE 2 : 8 BIT AUTO-RELOAD
See, my job is
very important,
to reload the
data TH1 to TL1
register when
overflows
Always keep
On eye on
my TF1 bit.
Never Forget
Get rock and
roll by
SETB TR1
Look, my con-
fuguration as
timer mode 2,
now
Triwiyanto - Jurusan Teknik
Elektromedik
Watch out !,
don’t get sleep
till you Catch
me, if In logic
‘1’. It’s Mean I
am in Overflow
Give me a number
To configure my
mode by software:
MOV
TMOD,#01110000b
Well, to start the all system as a counter/timer
Set on me by software: SETB TR1
Look at to my pin, I am so
useless. Because
The gate on TMOD= ‘0’
When do I can get action?
XTALL/
12
0 1 1 1 0 0 0 0
TF1
TR1
TL1
TMOD
TCON
0 = S1 keatas
1 = S1 kebawah
S1
0 = S2 buka
1 = S2 tutup
S2
T1/ P3.5
INT1/ P3.3
89C51
COUNTER 8 BIT
‘1’
MODE 3 : COUNTER 8 BIT ON TIMER1
Triwiyanto - Jurusan Teknik
Elektromedik
POOLING OR INTERUPTING SYSTEMS
05.00
BANGUN PAGI
06.00
SARAPAN
07.00
KULIAH
15.00
KERJAKAN TGS.
18.00
MAIN
19.00
BELAJAR
?
RUTINITAS ATAU PRIORITAS KARENA INTERUPSI
22.00
TIDUR
INTR
Triwiyanto - Jurusan Teknik
Elektromedik
INTERUPSI MIKROKONTROLLER
Triwiyanto - Jurusan Teknik
Elektromedik
EA X X ES ET1 EX1 ET0 EX0
LSB
MSB
INTERRUPT ENABLE REGISTER ( IE )
BIT SYMBOL FUNCTION
IE.7 EA Disables all interrupts.If EA=0, no interrupt will be
acknoledged. If EA=1,each interrupt source is individually
enabled/ dis.by software
IE.6 - Reserved
IE.5 - Reserved
IE.4 ES Enables or dis. the serial port interrupt, if ES=0, disabled
IE.3 ET1 Enables or dis. the timer 1 overflow interrupt, if ET1=0,
disabled
IE.2 EX1 Enables or dis. External interrupt 1.If EX1=0, disabled
IE.1 ET0 Enables or dis. the timer 0 overflow interrupt, if ET0=0,
disabled
IE.0 EX0 Enables or dis. External interrupt 0.If EX0=0, disabled
Triwiyanto - Jurusan Teknik
Elektromedik
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TCON REGISTER ( BIT ADDRESSABLE REGISTER)
TIMER 1 TIMER 0
BIT SYMBOL FUNCTION
TCON.7 TF1 Timer 1 overflow flag. Set by harware on timer/counter overflow. Clear by
hardware when processor vectors to interupt routine, or clearing by
software
TCON.6 TR1 Timer 1 run control bit. Set/cleared by software to turn timer/counter
on/off
TCON.5 TF0 Idem timer 0
TCON.4 TR0 Idem timer 0
TCON.3 IE1 Interupt 1 Edge flag, set by harware when external interrupt edge
detected.
Cleared when interupt processed
TCON.2 IT1 Interrupt 1 type control bit. Set/ Cleared by software to specify falling
edge/ low level triggered external interupt
TCON.1 IE0 Idem Interupt 0
TCON.0 IT0 Idem Interupt 0
Triwiyanto - Jurusan Teknik
Elektromedik
ALAMAT INTERUPSI
Source Vector Address
IE0 0003H
TF0 000BH
IE1 0013H
TF1 001BH
RI + TI 0023H
Triwiyanto - Jurusan Teknik
Elektromedik
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
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.
P0.0
Org 0h
Sjmp start
Org 03h
ljmp AdaIntExt0
Start: call InitIntExt0
Forever:clr P0.0
sjmp Forever
AdaIntExt0: setb P0.0
call delay
reti
;
InitIntExt0: setb IT0
setb EX0
setb EA
ret
Delay: mov R7,#255
Del1: mov R6,#255
djnz R6,$
djnz R7,Del1
ret
Triwiyanto - Jurusan Teknik
Elektromedik
INT1
2. 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.
P0.7
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
ContohInterupsi 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
Start: mov R7,#.......
mov A,#00000001b
call InitInterupsi
call InitTimer0
;
Hang: mov P0,A
sjmp hang
;
InitTimer0:
mov TMOD,#.....
mov TL0,#......
mov TH0,#.....
setb TR0
ret
InitInterupsi: setb ET0
setb EA
ret
;
InterupsiTF0: 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
X X X PS PT1 PX1 PT0 PX0
LSB
MSB
INTERRUPT PRIORITY REGISTER ( IP )
BIT SYMBOL FUNCTION
IP.7 - Reserved
IP.6 - Reserved
IP.5 - Reserved
IP.4 PS Defines the serial port interrupt. PS=1 programs it to the
higher priority
IP.3 PT1 Defines the timer 1
IP.2 PX1 Defines the external 1
IP.1 PT0 Defines the timer 0
IP.0 PX0 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
D0 D6
D5
D4
D3
D2
D1 D7
DATA
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
Shielded Cable Length
(feet)
Unshielded Cable Length
(feet)
9600 250 100
4800 500 250
2400 2000 500
1200 3000 500
300 4000 1000
110 5000 1000
Triwiyanto - Jurusan Teknik
Elektromedik
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
Start Stop
P3.0/ RXD
P3.1/ TXD
T1OUT
R1IN
GND
R1OUT
T1IN
RS232 89C51
RD(2)
TD(3)
GND (5)
USE MY PORT SERIAL
COMM 1,COMM 2 Etc. ( DB 9 )
My job is to convert
RS232 level to TTL
Logic Level
2400 kbps
Triwiyanto - Jurusan Teknik
Elektromedik
Port Serial PC
Pin Assignment Description
1 DCD Data carrier detect
2 RXD Receive data
3 TXD Transmit data
4 DTR Data terminal ready
5 GND Signal ground
6 DSR Data set ready
7 RTS Request to send
8 CTS Clear to send
9 RI Ring indicator
Base Address:
-3F8 h
-2F8 h
Triwiyanto - Jurusan Teknik
Elektromedik
KONVERTER RS232 TO TTL/ TTL TO RS232
VCC
MAX232
13
8
11
10
1
3
4
5
2
6
12
9
14
7
R1IN
R2IN
T1IN
T2IN
C+
C1-
C2+
C2-
V+
V-
R1OUT
R2OUT
T1OUT
T2OUT
+ 10u
P3.1 ( Tx )
+
10u
+
10u
P3.0 ( Rx )
15
P1
PC DB9 Male
5
9
4
8
3
7
2
6
1
+
10u
16
VCC
Triwiyanto - Jurusan Teknik
Elektromedik
Converter RS232 to TTL
+5V
+5V
C1+
C1-
C2+
C2-
V+
V-
T1IN
T2IN
R1OUT
R2OUT
T1OUT
T2OUT
R1IN
R2IN
VCC
1
3
4
5
11
10
12
9
2
6
14
7
13
8
16
15
+5V
Triwiyanto - Jurusan Teknik
Elektromedik
WHATKINDOF SERIAL MODE DO YOULIKETO 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. Mode2 : 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. Mode3 : idem mode 2, baudrate dapat diatur
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
D7 D6 D5 D4 D3 D2 D1 D0
SBUF - SERIAL BUFFER REGISTER
Triwiyanto - Jurusan Teknik
Elektromedik
SM2
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.
REN Enables serial reception. Set by software to enable reception. Clear by software to disable reception.
TB8 The 9th data bit that will be transmitted in Modes 2 and 3. Set or clear by software as desired.
RB8
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
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.
RI
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.
SM0 SM1 SM2 REN TB8 RB8 TI RI
SCON - SERIAL PORT CONTROL REGISTER
SM0 SM1 MODE Keterangan Baudrate
0 0 0
0
0
1
1
1 1
1
2
3
Sinkron
UART 8 BIT
UART 9 BIT
UART 9 BIT
TETAP (fosc/12)
VARIABEL
TETAP (fosc/32 dan
fosc/64)
VARIABEL
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
Baudrate untuk Timer1 sebagai generator Baudrate
Baud Rate Frekuensi
Kristal
SMOD Nilai Isi
Ulang TH1
Baud rate
Aktual
9600 12 MHz 1 F9h 8923
2400 12 MHz 0 F3h 2400
1200 12 MHz 0 F6h 1202
-
SMOD -
- GF1 GF0 PD IDL
PCON - POWER CONTROL REGISTER
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
Triwiyanto - Jurusan Teknik
Elektromedik
T1OUT
R1IN
GND
R1OUT
T1IN
3 cables
MAX232 Converter S2P
RXD
TXD
‘A’
Kirim Data ke PC
Triwiyanto - Jurusan Teknik
Elektromedik
org 0h
initserial: 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
Software pada Mikrokontroller:
Triwiyanto - Jurusan Teknik
Elektromedik
Software pada PC ( Delphi/ VB dll. ):
Procedure Receive_Data_Serial;
begin
asm
mov dx,$3F8
in al,dx
mov data,al
end
end;
Procedure Initserial;
Begin
asm
{bla-bla-bla}
{baud rate, 8 bit data, no parity}
{buka catatan masalalu }
end;
End;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
{bla-bla-bla}
{buka catatan masa lalu}
Receive_Data_Serial;
edit1.text:=inttostr(data);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
initserial;
timer1.enabled:=true;
end;
Triwiyanto - Jurusan Teknik
Elektromedik
Kirim Data ke Mikrokontroller
T1OUT
R1IN
GND
R1OUT
T1IN
3 cables
MAX232
RXD
TXD
P0.0
+5V
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 Send_Data_Serial;
begin
asm
mov dx,base
mov al,data
out dx,al
end
end;
Procedure Initserial;
Begin
asm
{bla-bla-bla}
{baud rate, 8 bit data, no parity}
{buka catatan masalalu }
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
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TIMER 0
TIMER 1
EX0
ES ET1 ET0 EX1
EA
UART
HIGH PRIORITY INTERRUPT
POLLING SEQUENCE
PX0
PX1
PT0
PT1
PS
0 = SW LEFT
1 = SW RIGHT
0 = SW LEFT
1 = SW RIGHT
1 = SW LEFT
0 = SW RIGHT
0 = SW UP
1 = SW DOWN
89C51
INT0/ P3.2
INT1/ P3.3
RxD/ P3.0
TxD/ P3.1
TCON
IE
IP
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

More Related Content

PPTX
Microcontroller 8051
PPT
M I C R O C O N T R O L L E R 2009new
PDF
Tutorial at89s51
DOCX
Membuat stopwatch menggunakan mikrokontroler atmega16 by muhammad kennedy gin...
DOCX
Laporan Joystick
PDF
72681707 jam-digital-at-mega-16
PDF
Mikroprosesor &amp; mikrokomputer
PPTX
Materi Dasar Pelatihan PLC Basic (CP2E) OMRON
Microcontroller 8051
M I C R O C O N T R O L L E R 2009new
Tutorial at89s51
Membuat stopwatch menggunakan mikrokontroler atmega16 by muhammad kennedy gin...
Laporan Joystick
72681707 jam-digital-at-mega-16
Mikroprosesor &amp; mikrokomputer
Materi Dasar Pelatihan PLC Basic (CP2E) OMRON

Similar to Microcontroller-8051.ppt (20)

DOCX
Menerapkan dasar dasar mikrokontroler
PPT
Mikroprosesor sttc
PPT
arsitektur mikrokontroler oertemuan ke 7
PDF
Modul dasar-mikrokontroler-bab-i-pengantar
PDF
Bab vi mikrokontroler
DOCX
PS2 Mouse Interfacing dengan pemrograman bahasa C
PDF
Traffic Light Berbasis ATMega32A
PDF
Aplikasi plc timer counter pada produk omron
PPTX
Walking Robot Menggunakan Atmega 16
PPTX
Presentasi dan video pembelajaran Mikrokontroller
DOC
Perancangan dan pembuatan alat keamanan kendaraan
PPT
PDF
Pertemuan 14-sistem-embedded
PPTX
PERANCANGAN PUTAR BALIK MOTOR DC WALKING ROBOT MENGGUNAKAN MIKROKONTROLLER ...
DOCX
alat pencampur minuman berbasis PLC
PPTX
Ppt traffic light pada perempatan jalan
PPTX
TRAFFIC LIGHT PEREMPATAN
DOC
PROYEK1
PPTX
Pemahaman dan Pengenalan Mikrokontroler untuk pemula.pptx
Menerapkan dasar dasar mikrokontroler
Mikroprosesor sttc
arsitektur mikrokontroler oertemuan ke 7
Modul dasar-mikrokontroler-bab-i-pengantar
Bab vi mikrokontroler
PS2 Mouse Interfacing dengan pemrograman bahasa C
Traffic Light Berbasis ATMega32A
Aplikasi plc timer counter pada produk omron
Walking Robot Menggunakan Atmega 16
Presentasi dan video pembelajaran Mikrokontroller
Perancangan dan pembuatan alat keamanan kendaraan
Pertemuan 14-sistem-embedded
PERANCANGAN PUTAR BALIK MOTOR DC WALKING ROBOT MENGGUNAKAN MIKROKONTROLLER ...
alat pencampur minuman berbasis PLC
Ppt traffic light pada perempatan jalan
TRAFFIC LIGHT PEREMPATAN
PROYEK1
Pemahaman dan Pengenalan Mikrokontroler untuk pemula.pptx
Ad

Recently uploaded (20)

PPTX
1 Peraturan Perundangan terkait Keselamatan Konstruksi 18.10 - Copy.pptx
PPTX
Resistensi Perubahan Teknik Sipil Manajemen Konstruksi
PPTX
SISTEM_INFORMASI_GEOGRAFIS_unlocked.pptx
PPTX
4. Penyusunan Rancangan Kontrak _edit 2024-Parwanta1.pptx
PPT
Penanganan motor starter pada dunia otomotif
PDF
MAINKAN GAME KESUKAN KALIAN DI TANGKI 4D DI JAMIN WD DAN CLIAM BONUSNYA
PPTX
TOPOLOGI JARINGAN STAR TEKNIK INFORMATIKA
PDF
Peraturan menteri perhubungan_63_TAHUN_2019.pdf
PPTX
4. PENERAPAN PENGELOLAAN SUMBER DAYA MANUSIA.pptx
PPTX
585590334-INTAKE-AND-EXHAUST-SYSTEM-Ahmad-Aditya-S-W.pptx
PPT
electronic fuel injection for automotive sectors
PPTX
SAFETY INDUCTION untuk perusahaan konstruksi
PPTX
7-presentasi-pk-metrologi-141030010022-conversion-gate01.pptx
PPT
PCM pekerjaan pembangunan Jembatan payahe
PDF
12. Mekanisme Pelaporan Kegiatan Sanimas_rev08082024.pptx.pdf
PPTX
PEMBUATAN PANEL TRAINER DAN PROTOTYPE UNTUK PENINGKATAN KOMPETENSI TENTANG S...
PPTX
2013materistudiumgeneralkabsndiunsri-130318044328-phpapp01.pptx
PPTX
Pengenalan SPALDT_SPALDS_Karanganyar.pptx
PDF
chapter 1 Smith and Van ness thermodynamics
PPT
08_Sistem-Penilaian-Kesesuaian_BW_1708071.ppt
1 Peraturan Perundangan terkait Keselamatan Konstruksi 18.10 - Copy.pptx
Resistensi Perubahan Teknik Sipil Manajemen Konstruksi
SISTEM_INFORMASI_GEOGRAFIS_unlocked.pptx
4. Penyusunan Rancangan Kontrak _edit 2024-Parwanta1.pptx
Penanganan motor starter pada dunia otomotif
MAINKAN GAME KESUKAN KALIAN DI TANGKI 4D DI JAMIN WD DAN CLIAM BONUSNYA
TOPOLOGI JARINGAN STAR TEKNIK INFORMATIKA
Peraturan menteri perhubungan_63_TAHUN_2019.pdf
4. PENERAPAN PENGELOLAAN SUMBER DAYA MANUSIA.pptx
585590334-INTAKE-AND-EXHAUST-SYSTEM-Ahmad-Aditya-S-W.pptx
electronic fuel injection for automotive sectors
SAFETY INDUCTION untuk perusahaan konstruksi
7-presentasi-pk-metrologi-141030010022-conversion-gate01.pptx
PCM pekerjaan pembangunan Jembatan payahe
12. Mekanisme Pelaporan Kegiatan Sanimas_rev08082024.pptx.pdf
PEMBUATAN PANEL TRAINER DAN PROTOTYPE UNTUK PENINGKATAN KOMPETENSI TENTANG S...
2013materistudiumgeneralkabsndiunsri-130318044328-phpapp01.pptx
Pengenalan SPALDT_SPALDS_Karanganyar.pptx
chapter 1 Smith and Van ness thermodynamics
08_Sistem-Penilaian-Kesesuaian_BW_1708071.ppt
Ad

Microcontroller-8051.ppt

  • 1. Triwiyanto - Jurusan Teknik Elektromedik A Software Oriented Device
  • 2. Triwiyanto - Jurusan Teknik Elektromedik uC Device Programmer APAKAH MIKROKONTROLLER ? uC :sebuah komputer didalam chip untuk mengontrol peralatan elektronik Sebuah device memerlukan komponen-komponen pendukung seperti IC TTL dan CMOS dapat direduksi/diperkecil dan akhirnya terpusat serta dikendalikan oleh mikrokontroler ini
  • 3. 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
  • 4. 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.
  • 5. Triwiyanto - Jurusan Teknik Elektromedik BAGAIMANA CARANYA? Desain Program Assembly (MIDE-51) Org 0h mov A,10000000b mov P0,A end 0001110101 0101010101 0101010101 0101010101 Compile (MIDE-51) PROGRAM Download (ATMEL Soft.) 01000101001 01111000100 01001010100 00111101010 Test
  • 6. 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).
  • 7. Triwiyanto - Jurusan Teknik Elektromedik BELAJAR MIKROKONTROLLER ? • SOFTWARE, BHS. ASSEMBLY-> 60 % • HARDWARE, ARSITEKTUR -> 40 %
  • 8. 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
  • 9. Triwiyanto - Jurusan Teknik Elektromedik Introduction CPU General- Purpose Micro- processor RAM ROM I/O Port Timer Serial COM Port Data Bus Address Bus General-Purpose Microprocessor System • CPU for Computers • No RAM, ROM, I/O on CPU chip itself • Example: Intel’s x86, Motorola’s 680x0 Many chips on mother’s board General-purpose microprocessor
  • 10. Triwiyanto - Jurusan Teknik Elektromedik RAM ROM I/O PORT TIMER PORT SERIAL Microcontroller CPU • A smaller computer • On-chip RAM, ROM, I/O ports... • Example: Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X A single chip Microcontroller :
  • 11. Triwiyanto - Jurusan Teknik Elektromedik 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 Microprocessor vs. Microcontroller
  • 12. Triwiyanto - Jurusan Teknik Elektromedik 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. Tiga kriteria dalam memilih mikrokontroller
  • 13. Triwiyanto - Jurusan Teknik Elektromedik 1.1. Diagram blok CPU On-chip RAM On-chip Flash ROM 4 I/O Ports Timer 0 Serial Port OSC Interrupt Control External interrupts Timer 1 Timer/Counter Bus Control TxD RxD P0 P1 P2 P3 Address/Data Counter Inputs 1. ARSITEKTUR
  • 14. Triwiyanto - Jurusan Teknik Elektromedik 1.2 Comparison of the 8051 Family Members Feature 8031 89C51 89S51 ROM (bytes) 0K 4k 4k RAM (bytes) 128 128 128 Timers 2 2 2 I/O pins 32 32 32 Serial port 1 1 1 Interrupt sources 6 6 6 Programming ROM Ext Parallel Serial/ ISP
  • 15. Triwiyanto - Jurusan Teknik Elektromedik 1.3. Memory mapping in 8051 1.3.1. ROM memory map in 8051 family 0000H 0FFFH 0000H 1FFFH AT89C51 AT89S51 AT89C52 AT89S52 4k 8k from Atmel Corporation
  • 16. Triwiyanto - Jurusan Teknik Elektromedik DIRECT/ INDIRECT ADDRESSING 128 RAM LOWER DIRECT ADDRESSING 128 RAM SFR 0 7F H 80 H FF H INDIRECT ADDRESSING 128 RAM UPPER 80 H FF H 4 K ROM INTERNAL 0000 H 0FFF H RAM INTERNAL P0 /EA ALE P2 /RD /WR 8951 Register Nilai ( biner ) ACC 00000000 B 00000000 PSW 00000000 SP 00000111 DPTR 00000000 P0-P3 11111111 IP 00000000 IE 00000000 TMOD 00000000 TCON 00000000 THO 00000000 TL0 00000000 TH1 00000000 TL1 00000000 SCON 00000000 1.3.2. Memory map in 89s51 family
  • 17. Triwiyanto - Jurusan Teknik Elektromedik 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 1.4. Register and Port 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
  • 18. Triwiyanto - Jurusan Teknik Elektromedik BASIS BILANGAN Desimal (BASIS 10) Hexa (BASIS 16) Biner 8 BIT (BASIS 2) ASCHII (CODE) 0 00 0000 0000 30 1 01 0000 0001 31 2 02 0000 0010 32 3 03 0000 0011 33 : : : : 9 9 0000 1001 39 10 A 0000 1010 - 11 B 0000 1011 - 12 C 0000 1100 - 13 D 0000 1101 - 14 E 0000 1110 - 15 F 0000 1111 -
  • 19. Triwiyanto - Jurusan Teknik Elektromedik Lanjutan: KONVERSI BILANGAN Desimal (BASIS 10) Hexa (BASIS 16) Biner 8 BIT (BASIS 2) ASCHII (CODE) 16 10 0000 0000 - 17 11 0000 0001 - 18 12 0000 0010 - 19 13 0000 0011 - : : : 25 19 0001 1001 - 26 1A 0001 1010 - 27 1B 0001 1011 - 28 1C 0001 1100 - 29 1D 0001 1101 - 30 1E 0001 1110 - 31 1F 0001 1111 -
  • 20. Triwiyanto - Jurusan Teknik Elektromedik KUNCI BASIS BILANGAN Biner 8 bit 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 2 D7 MSB D6 D5 D4 D3 D2 D1 D0 LSB 128 64 32 16 8 4 2 1 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
  • 21. Triwiyanto - Jurusan Teknik Elektromedik CY AC FO RS1 RS0 OV - P REGISTER PSW Carry flag CY PSW.7 Auxilarry carry flag AC PSW.6 General purpose F0 PSW.5 Register Bank Select RS1 PSW.4 Register Bank Select RS0 PSW.3 Over Flow OV PSW.2 General purpose - PSW.1 Parity P PSW.0 RS1 RS0 Bank Address 0 0 0 00H-07H 0 1 1 08H-0FH 1 0 2 10H-17H 1 1 3 18H-1FH 1.5. Memori Data (RAM) RO R7 R6 R5 R4 R3 R2 R1 RO R7 R6 R5 R4 R3 R2 R1 RO R7 R6 R5 R4 R3 R2 R1 RO R7 R6 R5 R4 R3 R2 R1 OOH 12H 11H 1OH OFH O7H O8H 18H 16H 15H 14H 13H OEH ODH OCH OBH O6H O5H O4H O3H O2H O1H OAH O9H 1EH 1DH 1CH 1FH 1AH 19H 1BH 17H 20 H 2F H BIT-ADDRESSABLE SPACE 30 H 7F H RAM SERBAGUNA BANK 0 BANK 3 BANK 2 BANK 1 P0 P2 SCON SBUF P1 TH1 TH0 TL1 TL0 TMOD TCON PCON DPH DPL SP B ACC PSW IP P3 IE 80 H 87 H 88 H 90 H 98 H A0 H A8 H B0 H B8 H D0 H E0 H F0 H 8F H 8951
  • 22. Triwiyanto - Jurusan Teknik Elektromedik 60KROM EKSTERNAL 4KROM INTERNAL 0000H 0FFFH 1000H FFFFH D0S/DD7 ADDR. P0 /EA ALE PSEN OE 8951 ROMEKSTERNAL P2 Contoh Program LOC OBJ LINE SOURCE 0000 1 org 0h 0000 00 2 nop 0001 E5B0 3 start: mov a,p3 0003 F590 4 mov p1,a 0005 80FA 5 sjmp start 0006 6 end 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 1.6. Memori Program ( ROM )
  • 23. Triwiyanto - Jurusan Teknik Elektromedik Pin Description of the 8051 PDIP/Cerdip 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 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (T0)P3.4 (T1)P3.5 XTAL2 XTAL1 GND (INT0)P3.2 (INT1)P3.3 (RD)P3.7 (WR)P3.6 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 
  • 24. 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. 
  • 25. 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 
  • 26. 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.
  • 27. 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.
  • 28. Triwiyanto - Jurusan Teknik Elektromedik XTAL Connection to 8051 C2 30pF C1 30pF XTAL2 XTAL1 GND • Using a quartz crystal oscillator • We can observe the frequency on the XTAL2 pin. 
  • 29. Triwiyanto - Jurusan Teknik Elektromedik XTAL Connection to an External Clock Source N C EXTERNAL OSCILLATOR SIGNAL XTAL2 XTAL1 GND • Using a TTL oscillator • XTAL2 is unconnected. 
  • 30. Triwiyanto - Jurusan Teknik Elektromedik Power-On RESET Circuit 30 pF 30 pF 8.2 K 10 uF + Vcc 11.0592 MHz EA/VPP X1 X2 RST 31 19 18 9 
  • 31. Triwiyanto - Jurusan Teknik Elektromedik Power-On RESET with Debounce EA/VPP X1 X2 RST Vcc 10 uF 8.2 K 30 pF 9 31  C:WINDOWShinhem.scr
  • 32. Triwiyanto - Jurusan Teknik Elektromedik 2. Set Instruction 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 F9’th 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 72’th byte of RAM will replace in Accumulator. Note 2: MOV A,R3 ≡ MOV A,3 2.1. Instruksi copy data
  • 33. Triwiyanto - Jurusan Teknik Elektromedik Contoh 1 : org 0h Start: 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 org 0h Start: mov 00h,#5 mov 01h,#7 mov 02h,#8 ; mov 10h,#0Ah mov 11h,#9 mov 12h,#01Bh end OR Contoh 2: Org 0h Start: 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 Contoh 3: Org 0h Start: mov A,#00000001b mov P1,A end Org 0h Start: mov 0E0h,#00000001b mov 90h,0E0h end OR
  • 34. Triwiyanto - Jurusan Teknik Elektromedik SETB bit ; bit=1 CLR bit ; bit=0 SETB C ; CY=1 SETB P0.0 ;bit 0 from port 0 =1 SETB P3.7 ;bit 7 from port 3 =1 SETB ACC.2 ;bit 2 from ACCUMULATOR =1 SETB 05 ;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
  • 35. Triwiyanto - Jurusan Teknik Elektromedik 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. Instruksi Aritmatika 2.1.2 Instruksi Pengurangan SUBB A,source ;A=A-source-CY SETB C ;CY=1 SUBB A,R5 ;A=A-R5-1
  • 36. Triwiyanto - Jurusan Teknik Elektromedik Example: MOV A,#38H ADD A,#2FH 38 00111000 +2F +00101111 ---- -------------- 67 01100111 CY=0 AC=1 P=1 Example: MOV A,#88H ADD A,#93H 88 10001000 +93 +10010011 ---- -------------- 11B 00011011 CY=1 AC=0 P=0 Example: MOV A,#9CH ADD A,#64H 9C 10011100 +64 +01100100 ---- -------------- 100 00000000 CY=1 AC=1 P=0 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
  • 37. 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
  • 38. Triwiyanto - Jurusan Teknik Elektromedik 2.1.5 Instruksi Decrement dan Increment DEC byte ;byte=byte-1 INC byte ;byte=byte+1 Example: INC R7 ;[R7]=[R7]+1 DEC A ;[A]=[A]-1 DEC 40H ; [40]=[40]-1
  • 39. Triwiyanto - Jurusan Teknik Elektromedik Table 1.3. Instruksi Aritmatika Mnemonic Operation Addressing Mode Exect. Dir Ind Reg Imm Timer uS Add A,<byte> A=A+<byte> V V V V 1 Addc A,<byte> A=A+<byte>+C V V V V 1 Subb A,<byte> A=A-<byte>-C V V V V 1 Inc A A=A+1 Accumulator Only 1 Inc <byte> <byt>=<byt>+1 V V V 1 Inc DPTR DPTR=DPTR+1 Data Pointer Only 2 Dec A A=A-1 Accumulator Only 1 Dec <byte> <byt>=<byt>-1 V V V 1 Mul AB B:A=BxA Accumulator and B Only 4 Div AB A=Int[A/B] B=Mod[A/B] Accumulator and B only 4 DAA Dec Adjust Accumulator Only 1
  • 40. 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
  • 41. Triwiyanto - Jurusan Teknik Elektromedik Example: D7 D6 D5 D4 D3 D2 D1 D0 RL A ;rotate left A Contoh: ORG 0H START: MOV A,#00000001B PUTAR: RL A SJMP PUTAR END A = 00000010 A = 00000100 A = 00001000 : A = 10000000 A = 00000001 A = dst. CY D7 D6 D5 D4 D3 D2 D1 D0 RLC A ;rotate left and carry A Contoh: ORG 0H START: MOV A,#00000001B PUTAR: RLC A SJMP PUTAR END A = 00000010 A = 00000100 : : A = 10000000 C=1 A = 00000000 A = 00000001 dst. RR A ;rotate right A RRC A ;rotate right ;and carry A
  • 42. Triwiyanto - Jurusan Teknik Elektromedik Mnemonic Operation Addressing Mode Exect. Dir Ind Reg Imm uS Anl A,<byte> A=A and <byte> V V V V 1 Anl <byte>,A <byte>=<byte>anl A V V V V 1 Anl <byte>,#data <byte>=<byte>and #data V V V V 1 Orl A,<byte> A=A or <byte> Accumulator Only 1 Orl <byte>,A <byt>=<byt>orl A V V V 1 Orl <byte>,#data <byte>=<byte> or #data Data Pointer Only 2 Xrl A,<byte> A=A xor<byte> Accumulator Only 1 Xrl<byte>,A <byt>=<byt>xor A V V V 1 Xrl <byte>,#data <byte>=<byte>xor #data Accumulator and B Only 4 CLR A A=00h Accumulator only 1 CPL A A= not A Accumulator only 1 RL A Rotate A left 1 bit Accumulator only 1 RLC A Rotate A left trough Carry Accumulator only 1 RR A Rotate A right 1 bit Accumulator only 1 RRC Rotate A right trough carry Accumulator only 1 SWAP A Swap nibbles in A Accumulator only 1
  • 43. Triwiyanto - Jurusan Teknik Elektromedik 2.3. Addressing Modes • Immediate • Register • Direct • Register Indirect • Indexed
  • 44. Triwiyanto - Jurusan Teknik Elektromedik 2.3.1. Immediate Addressing Mode MOV A,#65H MOV A,#’A’ MOV R6,#65H MOV DPTR,#2343H MOV P1,#65H Example : Num EQU 30 … MOV R0,Num MOV DPTR,#data1 … ORG 100H data1: db “IRAN”
  • 45. Triwiyanto - Jurusan Teknik Elektromedik 2.3.2. Register Addressing Mode MOV Rn, A ;n=0,..,7 ADD A, Rn MOV DPL, R6 MOV DPTR, A MOV Rm, Rn
  • 46. 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 A, R4 MOV 6, 2 ; 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
  • 47. Triwiyanto - Jurusan Teknik Elektromedik 2.3.4. Register Indirect Addressing Mode In this mode, register is used as a pointer to the data. MOV A,@Ri ; move content of RAM loc.Where address is held by Ri into A ;( i=0 or 1 ) MOV @R1,B 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
  • 48. 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.
  • 49. 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.
  • 50. 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
  • 51. 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 DJNZR2,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
  • 52. 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.
  • 53. 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; MULAB, DIV AB
  • 54. Triwiyanto - Jurusan Teknik Elektromedik TANKS ALOT TRIMAKASIH MATUR SUWON I’LL SEE YOU SOON, IN THE NEXT SEMESTER, TO ROCK AND ROLL AND HAVING FUN A K H I R N Y A
  • 55. 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.
  • 56. Triwiyanto - Jurusan Teknik Elektromedik Other conditional jumps : JZ Jump if A=0 JNZ Jump if A/=0 DJNZ Decrement and jump if A/=0 CJNE A,byte,lompat1 Jump if A/=byte CJNE reg,#data,lompat1 Jump if byte/=#data JC Jump if CY=1 JNC Jump if CY=0 JB Jump if bit=1 JNB Jump if bit=0 JBC Jump if bit=1 and clear bit
  • 57. 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.
  • 58. 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 2K- byte range.
  • 59. Triwiyanto - Jurusan Teknik Elektromedik Port 0 with Pull-Up Resistors P0.0 P0.1 P0.2 P0.3 P0.4 P0.5 P0.6 P0.7 8751 8951 Vcc 10 K Port 0
  • 60. Triwiyanto - Jurusan Teknik Elektromedik Port 3 Alternate Functions 17 RD P3.7 16 WR P3.6 15 T1 P3.5 14 T0 P3.4 13 INT1 P3.3 12 INT0 P3.2 11 TxD P3.1 10 RxD P3.0 Pin Function P3 Bit 
  • 61. Triwiyanto - Jurusan Teknik Elektromedik Structure of Assembly language Label Opcode Operand Comment ORG 0H ;StartROM Start: Mov A,#10001000b ; fill A = 10001000b Mov P0,A ;copy A to P0 END Desain Program Assembly (MIDE-51) Org 0h mov A,10000000b mov P0,A end 0001110101 0101010101 0101010101 0101010101 Compile (MIDE-51) PROGRAM Download (ATMEL Soft.) 01000101001 01111000100 01001010100 00111101010 TEST
  • 62. Triwiyanto - Jurusan Teknik Elektromedik Percobaan 1.1. Menghidupkan/ Matikan LED Org 0h ;Starting ROM 0h Start: Mov P0, #00000001;Send ’1’ to P0.0 end ;LED OFF Org 0h Start: Mov P0, #00000000; Send ‘0’ to P0.0 end ; LED ON Org 0h Start: Setb P0.0 ; Send ‘1’ to P0.0 end Org 0h Start: Clr P0.0 ; end 40 39 38 1 2 3 P1.0 P1.1 P0.0 P0.1 5V P1.2 18 19 20 XTAL2 XTAL1 GND 31 /EA VCC 22 21 P2.0 P2.1
  • 63. Triwiyanto - Jurusan Teknik Elektromedik Percobaan 1.2. LED Blink Rencanakan program untuk mengedipkan sebuah LED pada P0.0. Solusi: Org 0h Start: Setb P0.0 Call Delay Clr P0.0 Call Delay sjmp Start ;====================== ;Subrutin untuk membangkitkan ;waktu tunda ;====================== Delay: Mov R0,#5d Del1: DJNZ R0, Del1 ret End
  • 64. Triwiyanto - Jurusan Teknik Elektromedik 3.1. LCD Karakter 2x16 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 A g 1 4 L 0 7 1 2 3 16 16 1 2 D0-D7 R/W EN RS P0.0 - P0.7 P3.6 P3.7 LCD CHARACTER 2 X 16 8951
  • 65. Triwiyanto - Jurusan Teknik Elektromedik Pin Simbol 1 Vss 2 Vcc 3 Vee 4 RS 5 R/W 6 E 7-14 DB0 15 BPL 16 GND Nama Signal Fungsi DB0– DB7 Data - Untuk mengirimkan data karakter atau dan instruksi E Enable - Signal start untuk mulai pengiriman data atau instruksi R/W Read/ Write - Signal yang digunakan untuk memilih mode baca atau tulis ‘0’ : tulis ‘1’ : baca RS Register Select “0”: Instruction register (Write) “1”: Data register (Write, Read) Vee Tegangan pengaturan kontras pada LCD Vcc Tegangan Vcc Vss Tegangan 0V atau Ground 3.2. Pin dan Fungsi
  • 66. Triwiyanto - Jurusan Teknik Elektromedik 3.3. Inisialisasi LCD Karakter 0 0 0 0 1 DL N F X X Function Set 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 ) 0 0 0 0 0 0 0 1 I/D S Entry Mode Set RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 I/D : Set increment/ decrement ( 1= increment, 0= decrement ) S : Display Sift ( 1=on, 0=off ) Display On/ Off Control RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 0 0 0 0 0 0 1 D C B B : Cursor blink ( 1= blink on,0=blink off ) C : Cursor display (1=cursor on, 0=cursor off) D : display On ( 1= display on)
  • 67. 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 R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 0 0 0 0 0 1 S/C R/L X X Note : x = Dont care S/C R/L Note 0 0 Shift cursor position to the left 0 1 Shift cursor position to the right 1 0 Shift the entire display to the left 1 1 Shift the entire display to the right
  • 68. Triwiyanto - Jurusan Teknik Elektromedik 0 0 1 A A A A A A A Clear Display RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Dengan menuliskan instruksi tersebut maka semua tamplan akan reset 0 0 0 0 0 0 0 0 0 1 A : Lokasi memori display 0000000 – 1111111 ( 00 h – 7F h ) Instruksi Pemilihan RAM 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F …. 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F …. Ram Display Address ( in Hexa ) RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Baris 1 Baris 2
  • 69. Triwiyanto - Jurusan Teknik Elektromedik 3.4. LCD ( Cetak sebuah karakter 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' call write_data stop: sjmp stop A Write_inst: clr P3.6 ; P3.6 = RS =0 mov P0,R1 ; copy R1 ke P0 setb P3.7 ; P3.7 =EN = 1 call delay ; call delay time clr P3.7 ; P3.7 =EN = 0 ret ; Write_data: setb P3.6 ; P3.6 = RS =1 mov P0,R1 ; copy R1 ke P0 setb P3.7 ; P3.7 =EN = 1 call delay ; call delay time clr p3.7 ; P3.7 =EN = 0 ret end Subrutine Write_inst Write_data Memanggil subrutine
  • 70. Triwiyanto - Jurusan Teknik Elektromedik Watch out !, you may miss my overflow every 10s. If you get sleep COUNTER UP/DOWN UP 4 BIT Co 74193 D3 D2 D1 D0 What a number to be loaded to my register, so I can get pulse out every 10 s on Co My heart beat periode is 1 Hz now Think smart, you must load data : 16-10 = 6 or 0110 b on me 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 If my periode input clock is 1 u Then I wil interupt you, after 1 uS x 65536 = 0.065536 s Load
  • 71. Triwiyanto - Jurusan Teknik Elektromedik COUNTER UP Clock 16 BIT TF1 TH1(D15..D8) TL1(D7..D0) Clock = 1 Mhz 0E0h 0B1h Over flow, after 20000 us = 0.02 s 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 Data to be loaded to TL1 and TH1 Register 9. Basic Timer/ Counter
  • 72. Triwiyanto - Jurusan Teknik Elektromedik THEActionof 89s51 aS COUNTEROR TIMER XTALL/ 12 G C/T M1 M0 G C/T M1 M0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 TL1 TH1 TMOD TCON 0 = S1 keatas 1 = S1 kebawah S1 0 = S2 buka 1 = S2 tutup S2 Limpahan Timer/ Counter T1/ P3.5 INT1/ P3.3 89C51 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 Don’t get sleep, If you don’t want miss my overflow, test me by Software ! Timer1 Timer 0
  • 73. Triwiyanto - Jurusan Teknik Elektromedik M1 M0 OPERATING 0 0 Mode 0: 13 bit Timer/ Counter 0 1 Mode 1: 16 bit Timer/ Counter 1 0 Mode 2: 8 bit auto reload Timer/ Counter. THx menerima data yang akan direload ke TLx setiap kali TLx overflow 1 1 Mode 3: 8 bit Timer/ Counter by TL0. TH0 merupakan 8 bit Timer yang dikontrol dari Timer 1 Gate C/T M1 M0 Gate C/T M1 M0 TIMER 1 TIMER 0 MORE ABOUTTIMER/ COUNTER 8951 TMOD REGISTER
  • 74. Triwiyanto - Jurusan Teknik Elektromedik TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 TCON REGISTER ( BIT ADDRESSABLE REGISTER) TIMER 1 TIMER 0 TIMER 1 TIMER 0 BIT SYMBOL FUNCTION TCON.7 TF1 Timer 1 overflow flag. Set by harware on timer/counter overflow. Clear by hardware when processor vectors to interupt routine, or clearing by software TCON.6 TR1 Timer 1 run control bit. Set/cleared by software to turn timer/counter on/off TCON.5 TF0 Idem timer 0 TCON.4 TR0 Idem timer 0 TCON.3 IE1 Interupt 1 Edge flag, set by harware when external interrupt edge detected. Cleared when interupt processed TCON.2 IT1 Interrupt 1 type control bit. Set/ Cleared by software to specify falling edge/ low level triggered external interupt TCON.1 IE0 Idem timer 0 TCON.0 IT0 Idem timer 0
  • 75. Triwiyanto - Jurusan Teknik Elektromedik MODE1 : TIMER16 BITON TIMER1 XTALL/ 12 0 0 0 1 0 0 0 0 TF1 TR1 TL1 TMOD TCON 0 = S1 keatas 1 = S1 kebawah S1 0 = S2 buka 1 = S2 tutup S2 T1/ P3.5 INT1/ P3.3 89C51 COUNTER 16 BIT TH1 P1.0 OSCILOSCOPE Please load a number to my register ( TL1, TH1) tobe counted, by MOV TH1,#0D8h MOV TL1,#0EFh If your xtall is 12MHz Then I wil generate 1 Mhz Don’t move From your seat keep spy on me, Maybe I get Over flow They are trying to configure my function as a timer in MODE 1,by MOV TMOD,#10H Lets go, to rock and roll By, SETB TR1 Read my Frequency !
  • 76. 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. 2. 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.
  • 77. 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. 2. Idem. Ton=0.02 detik dan Toff=0.04 detik. Gunakan Timer0 mode 2.
  • 78. Triwiyanto - Jurusan Teknik Elektromedik XTALL/ 12 0 0 1 0 0 0 0 0 TF1 TR1 TL1( 8 BIT ) TMOD TCON 0 = S1 keatas 1 = S1 kebawah S1 0 = S2 buka 1 = S2 tutup S2 T1/ P3.5 INT1/ P3.3 89C51 COUNTER 16 BIT TH1 (8 BIT) P1.0 OSCILOSCOPE LATCH MODE 2 : 8 BIT AUTO-RELOAD See, my job is very important, to reload the data TH1 to TL1 register when overflows Always keep On eye on my TF1 bit. Never Forget Get rock and roll by SETB TR1 Look, my con- fuguration as timer mode 2, now
  • 79. Triwiyanto - Jurusan Teknik Elektromedik Watch out !, don’t get sleep till you Catch me, if In logic ‘1’. It’s Mean I am in Overflow Give me a number To configure my mode by software: MOV TMOD,#01110000b Well, to start the all system as a counter/timer Set on me by software: SETB TR1 Look at to my pin, I am so useless. Because The gate on TMOD= ‘0’ When do I can get action? XTALL/ 12 0 1 1 1 0 0 0 0 TF1 TR1 TL1 TMOD TCON 0 = S1 keatas 1 = S1 kebawah S1 0 = S2 buka 1 = S2 tutup S2 T1/ P3.5 INT1/ P3.3 89C51 COUNTER 8 BIT ‘1’ MODE 3 : COUNTER 8 BIT ON TIMER1
  • 80. Triwiyanto - Jurusan Teknik Elektromedik POOLING OR INTERUPTING SYSTEMS 05.00 BANGUN PAGI 06.00 SARAPAN 07.00 KULIAH 15.00 KERJAKAN TGS. 18.00 MAIN 19.00 BELAJAR ? RUTINITAS ATAU PRIORITAS KARENA INTERUPSI 22.00 TIDUR INTR
  • 81. Triwiyanto - Jurusan Teknik Elektromedik INTERUPSI MIKROKONTROLLER
  • 82. Triwiyanto - Jurusan Teknik Elektromedik EA X X ES ET1 EX1 ET0 EX0 LSB MSB INTERRUPT ENABLE REGISTER ( IE ) BIT SYMBOL FUNCTION IE.7 EA Disables all interrupts.If EA=0, no interrupt will be acknoledged. If EA=1,each interrupt source is individually enabled/ dis.by software IE.6 - Reserved IE.5 - Reserved IE.4 ES Enables or dis. the serial port interrupt, if ES=0, disabled IE.3 ET1 Enables or dis. the timer 1 overflow interrupt, if ET1=0, disabled IE.2 EX1 Enables or dis. External interrupt 1.If EX1=0, disabled IE.1 ET0 Enables or dis. the timer 0 overflow interrupt, if ET0=0, disabled IE.0 EX0 Enables or dis. External interrupt 0.If EX0=0, disabled
  • 83. Triwiyanto - Jurusan Teknik Elektromedik TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 TCON REGISTER ( BIT ADDRESSABLE REGISTER) TIMER 1 TIMER 0 BIT SYMBOL FUNCTION TCON.7 TF1 Timer 1 overflow flag. Set by harware on timer/counter overflow. Clear by hardware when processor vectors to interupt routine, or clearing by software TCON.6 TR1 Timer 1 run control bit. Set/cleared by software to turn timer/counter on/off TCON.5 TF0 Idem timer 0 TCON.4 TR0 Idem timer 0 TCON.3 IE1 Interupt 1 Edge flag, set by harware when external interrupt edge detected. Cleared when interupt processed TCON.2 IT1 Interrupt 1 type control bit. Set/ Cleared by software to specify falling edge/ low level triggered external interupt TCON.1 IE0 Idem Interupt 0 TCON.0 IT0 Idem Interupt 0
  • 84. Triwiyanto - Jurusan Teknik Elektromedik ALAMAT INTERUPSI Source Vector Address IE0 0003H TF0 000BH IE1 0013H TF1 001BH RI + TI 0023H
  • 85. Triwiyanto - Jurusan Teknik Elektromedik 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
  • 86. 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.
  • 87. Triwiyanto - Jurusan Teknik Elektromedik Contoh: Interupsi Eksternal 0 INT0 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. P0.0 Org 0h Sjmp start Org 03h ljmp AdaIntExt0 Start: call InitIntExt0 Forever:clr P0.0 sjmp Forever AdaIntExt0: setb P0.0 call delay reti ; InitIntExt0: setb IT0 setb EX0 setb EA ret Delay: mov R7,#255 Del1: mov R6,#255 djnz R6,$ djnz R7,Del1 ret
  • 88. Triwiyanto - Jurusan Teknik Elektromedik INT1 2. 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. P0.7
  • 89. 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.
  • 90. Triwiyanto - Jurusan Teknik Elektromedik ContohInterupsi 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 Start: mov R7,#....... mov A,#00000001b call InitInterupsi call InitTimer0 ; Hang: mov P0,A sjmp hang ; InitTimer0: mov TMOD,#..... mov TL0,#...... mov TH0,#..... setb TR0 ret InitInterupsi: setb ET0 setb EA ret ; InterupsiTF0: call InitTimer0 djnz R7, EndInterupsiTF0 RL A mov R7,#...... EndInterupsiTF0: reti ; end
  • 91. 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.
  • 92. Triwiyanto - Jurusan Teknik Elektromedik X X X PS PT1 PX1 PT0 PX0 LSB MSB INTERRUPT PRIORITY REGISTER ( IP ) BIT SYMBOL FUNCTION IP.7 - Reserved IP.6 - Reserved IP.5 - Reserved IP.4 PS Defines the serial port interrupt. PS=1 programs it to the higher priority IP.3 PT1 Defines the timer 1 IP.2 PX1 Defines the external 1 IP.1 PT0 Defines the timer 0 IP.0 PX0 Defines the external 0
  • 93. 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.
  • 94. 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 D0 D6 D5 D4 D3 D2 D1 D7 DATA CLOCK
  • 95. 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 )
  • 96. 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 Shielded Cable Length (feet) Unshielded Cable Length (feet) 9600 250 100 4800 500 250 2400 2000 500 1200 3000 500 300 4000 1000 110 5000 1000
  • 97. Triwiyanto - Jurusan Teknik Elektromedik Standart RS232 RS232 LEVEL
  • 98. 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.
  • 99. Triwiyanto - Jurusan Teknik Elektromedik SERIAL COMMUNICATION ON THE RUN YOU MAY CONTROL YOUR DEVICE FROM REMOTE PLACE, JUST BY THREE CABLE Start Stop P3.0/ RXD P3.1/ TXD T1OUT R1IN GND R1OUT T1IN RS232 89C51 RD(2) TD(3) GND (5) USE MY PORT SERIAL COMM 1,COMM 2 Etc. ( DB 9 ) My job is to convert RS232 level to TTL Logic Level 2400 kbps
  • 100. Triwiyanto - Jurusan Teknik Elektromedik Port Serial PC Pin Assignment Description 1 DCD Data carrier detect 2 RXD Receive data 3 TXD Transmit data 4 DTR Data terminal ready 5 GND Signal ground 6 DSR Data set ready 7 RTS Request to send 8 CTS Clear to send 9 RI Ring indicator Base Address: -3F8 h -2F8 h
  • 101. Triwiyanto - Jurusan Teknik Elektromedik KONVERTER RS232 TO TTL/ TTL TO RS232 VCC MAX232 13 8 11 10 1 3 4 5 2 6 12 9 14 7 R1IN R2IN T1IN T2IN C+ C1- C2+ C2- V+ V- R1OUT R2OUT T1OUT T2OUT + 10u P3.1 ( Tx ) + 10u + 10u P3.0 ( Rx ) 15 P1 PC DB9 Male 5 9 4 8 3 7 2 6 1 + 10u 16 VCC
  • 102. Triwiyanto - Jurusan Teknik Elektromedik Converter RS232 to TTL +5V +5V C1+ C1- C2+ C2- V+ V- T1IN T2IN R1OUT R2OUT T1OUT T2OUT R1IN R2IN VCC 1 3 4 5 11 10 12 9 2 6 14 7 13 8 16 15 +5V
  • 103. Triwiyanto - Jurusan Teknik Elektromedik WHATKINDOF SERIAL MODE DO YOULIKETO 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. Mode2 : 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. Mode3 : idem mode 2, baudrate dapat diatur 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 D7 D6 D5 D4 D3 D2 D1 D0 SBUF - SERIAL BUFFER REGISTER
  • 104. Triwiyanto - Jurusan Teknik Elektromedik SM2 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. REN Enables serial reception. Set by software to enable reception. Clear by software to disable reception. TB8 The 9th data bit that will be transmitted in Modes 2 and 3. Set or clear by software as desired. RB8 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 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. RI 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. SM0 SM1 SM2 REN TB8 RB8 TI RI SCON - SERIAL PORT CONTROL REGISTER SM0 SM1 MODE Keterangan Baudrate 0 0 0 0 0 1 1 1 1 1 2 3 Sinkron UART 8 BIT UART 9 BIT UART 9 BIT TETAP (fosc/12) VARIABEL TETAP (fosc/32 dan fosc/64) VARIABEL
  • 105. 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:
  • 106. Triwiyanto - Jurusan Teknik Elektromedik Baudrate untuk Timer1 sebagai generator Baudrate Baud Rate Frekuensi Kristal SMOD Nilai Isi Ulang TH1 Baud rate Aktual 9600 12 MHz 1 F9h 8923 2400 12 MHz 0 F3h 2400 1200 12 MHz 0 F6h 1202 - SMOD - - GF1 GF0 PD IDL PCON - POWER CONTROL REGISTER 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
  • 107. Triwiyanto - Jurusan Teknik Elektromedik T1OUT R1IN GND R1OUT T1IN 3 cables MAX232 Converter S2P RXD TXD ‘A’ Kirim Data ke PC
  • 108. Triwiyanto - Jurusan Teknik Elektromedik org 0h initserial: 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 Software pada Mikrokontroller:
  • 109. Triwiyanto - Jurusan Teknik Elektromedik Software pada PC ( Delphi/ VB dll. ): Procedure Receive_Data_Serial; begin asm mov dx,$3F8 in al,dx mov data,al end end; Procedure Initserial; Begin asm {bla-bla-bla} {baud rate, 8 bit data, no parity} {buka catatan masalalu } end; End; procedure TForm1.Timer1Timer(Sender: TObject); begin {bla-bla-bla} {buka catatan masa lalu} Receive_Data_Serial; edit1.text:=inttostr(data); end; procedure TForm1.Button1Click(Sender: TObject); begin initserial; timer1.enabled:=true; end;
  • 110. Triwiyanto - Jurusan Teknik Elektromedik Kirim Data ke Mikrokontroller T1OUT R1IN GND R1OUT T1IN 3 cables MAX232 RXD TXD P0.0 +5V P0.1 P0.2 P0.3 P0.4 P0.5 P0.6 P0.7
  • 111. Triwiyanto - Jurusan Teknik Elektromedik Software pada PC ( Delphi/ VB dll. ): Procedure Send_Data_Serial; begin asm mov dx,base mov al,data out dx,al end end; Procedure Initserial; Begin asm {bla-bla-bla} {baud rate, 8 bit data, no parity} {buka catatan masalalu } 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;
  • 112. 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
  • 113. Triwiyanto - Jurusan Teknik Elektromedik OS anda WINXP Tambahkan komponen ComPort !. Download saja!
  • 114. Triwiyanto - Jurusan Teknik Elektromedik WHAT DO YOU THINK ABOUT INTERRUPTS TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 TIMER 0 TIMER 1 EX0 ES ET1 ET0 EX1 EA UART HIGH PRIORITY INTERRUPT POLLING SEQUENCE PX0 PX1 PT0 PT1 PS 0 = SW LEFT 1 = SW RIGHT 0 = SW LEFT 1 = SW RIGHT 1 = SW LEFT 0 = SW RIGHT 0 = SW UP 1 = SW DOWN 89C51 INT0/ P3.2 INT1/ P3.3 RxD/ P3.0 TxD/ P3.1 TCON IE IP 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