Professional Documents
Culture Documents
8051 has limited number of I/O ports. One solution is to add parallel interface chip(s).
8255 is a Programmable Peripheral Interface PPI. Add it to 8051 to expand number of
parallel ports. 8051 I/O port does not have handshaking capability. 8255 can add
handshaking capability to 8051.
PIN configuration
NagRaz 1
8255 Operating Modes
Mode 0
Provides simple input and output operations for each of the three ports. No
“handshaking” is required, data is simply written to or read from a specified port. Two 8-
bit ports and two 4-bit ports. Any port can be input or output. Outputs are latched.
Inputs are not latched
NagRaz 2
NagRaz 3
8051 Microcontroller Architecture
NagRaz 4
Instructions by opcode
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x00 NOP AJMP LJMP RR INC INC INC INC INC INC INC INC INC INC INC INC
0x10 JBC ACALL LCALL RRC DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC
0x20 JB AJMP RET RL ADD ADD ADD ADD ADD ADD ADD ADD ADD ADD ADD ADD
0x30 JNB ACALL RETI RLC ADDC ADDC ADDC ADDC ADDC ADDC ADDC ADDC ADDC ADDC ADDC ADDC
0x40 JC AJMP ORL ORL ORL ORL ORL ORL ORL ORL ORL ORL ORL ORL ORL ORL
0x50 JNC ACALL ANL ANL ANL ANL ANL ANL ANL ANL ANL ANL ANL ANL ANL ANL
0x60 JZ AJMP XRL XRL XRL XRL XRL XRL XRL XRL XRL XRL XRL XRL XRL XRL
0x70 JNZ ACALL ORL JMP MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV
0x80 SJMP AJMP ANL MOVC DIV MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV
0x90 MOV ACALL MOV MOVC SUBB SUBB SUBB SUBB SUBB SUBB SUBB SUBB SUBB SUBB SUBB SUBB
0xa0 ORL AJMP MOV INC MUL ? MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV
0xb0 ANL ACALL CPL CPL CJNE CJNE CJNE CJNE CJNE CJNE CJNE CJNE CJNE CJNE CJNE CJNE
0xc0 PUSH AJMP CLR CLR SWAP XCH XCH XCH XCH XCH XCH XCH XCH XCH XCH XCH
0xd0 POP ACALL SETB SETB DA DJNZ XCHD XCHD DJNZ DJNZ DJNZ DJNZ DJNZ DJNZ DJNZ DJNZ
0xe0 MOVX AJMP MOVX MOVX CLR MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV
0xf0 MOVX ACALL MOVX MOVX CPL MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV MOV
NagRaz 5
Alphabetical List of Instructions
ACALL - Absolute Call MOV - Move Memory
ADD, ADDC - Add Accumulator (With Carry) MOVC - Move Code Memory
DJNZ - Decrement Register and Jump if Not Zero RL - Rotate Accumulator Left
JBC - Jump if Bit Set and Clear Bit RRC - Rotate Accumulator Right Through Carry
JNB - Jump if Bit Not Set SUBB - Subtract From Accumulator With Borrow
NagRaz 6
Interfacing 8255 with 8051
P2.7(A15) A2
P2.6(A14) A1
P2.5(A13) A0
74138
3×8 decoder
8051 /CS
8255
ALE O0 A0
74373 O1 A1
P0.7-P0.0 D7-D0
O7
(AD7-AD0)
D7-D0
/RD /RD
/WR /WR
NagRaz 7
Program No: 01
Problem: Write a ALP to Display Characters on Multiplexed LED display.
NagRaz 8
Solution:
org 4100h
mov dptr,#0ff0fh
mov a,#80h
movx @dptr,a
mov r0,#0eeh
start: mov dptr,#look
mov r1,dpl
mov r2,dph
mov r3,#04h
cont: movx a,@dptr
mov dptr,#0ff0ch
movx @dptr,a
mov a,r0
rl a
mov r0,a
mov dptr,#0ff0dh
movx @dptr,a
lcall delay
inc r1
mov dpl,r1
mov dph,r2
djnz r3,cont
sjmp start
delay: mov r5,#01h
l3: mov r6,#0ffh
l2: djnz r6,l2
djnz r5,l3
ret
look: db 08h,00h,46h,40h
NagRaz 9
Program No: 02
Problem: Write ALP to generate Square wave using Oscilloscope
Solution:
org 4100
mov dptr,#0ff0fh
mov a,#80h
movx @dptr,a
mov a,#0ffh
return: mov dptr,#0ff0ch
movx @dptr,a
cpl a
mov r4,#0ffh
loop: djnz r4,loop
sjmp return
NagRaz 10
Program No: 03
Problem: Write ALP to Control Traffic at 4 road junction.
NagRaz 11
Solution:
org 4100h
mov a,#80h
mov dptr,#0ff0fh
movx @dptr,a
start: mov r4,#04h
mov dptr,#look
mov r0,dph
mov r1,dpl
go: movx a,@dptr
mov dptr,#0ff0ch
movx @dptr,a
inc r1
mov dph,r0
mov dpl,r1
movx a,@dptr
mov dptr,#0ff0dh
movx @dptr,a
inc r1
mov dph,r0
mov dpl,r1
movx a,@dptr
mov dptr,#0ff0eh
movx @dptr,a
acall delay1
inc r1
mov dph,r0
mov dpl,r1
movx a,@dptr
mov dptr,#0ff0ch
movx @dptr,a
inc r1
mov dph,r0
mov dpl,r1
movx a,@dptr
mov dptr,#0ff0dh
movx @dptr,a
inc r1
mov dph,r0
mov dpl,r1
movx a,@dptr
mov dptr,#0ff0eh
movx @dptr,a
inc r1
mov dph,r0
mov dpl,r1
NagRaz 12
acall delay2
djnz r4,go
ljmp start
delay2: mov r5,#6fh
l1: mov r6,#0ffh
l2: mov r7,#0ffh
l3: djnz r7,l3
djnz r6,l2
djnz r5,l1
ret
delay1: mov r5,#0afh
l4: mov r6,#0ffh
l5: mov r7,#0ffh
l6: djnz r7,l6
djnz r6,l5
djnz r5,l4
ret
look: db 90h,20h,10h
db 88h,20h,10h
db 04h,20h,12h
db 04h,20h,11h
db 84h,20h,40h
db 84h,20h,20h
db 84h,80h,10h
db 84h,40h,10h
NagRaz 13
Program No: 04
Problem: Write ALP to Display String Using LCD Display.
The LCD has 14 pins. The function of each pin is given in table.
While Vcc and Vss provide +5V and ground, respectively, VEE is used for controlling
LCD contrast.
RS - register select:
There are two very important registers inside the LCD. The RS pin is used for their
selection as follows. If RS = 0, the instruction command code register is selected, allowing the
user to send a command such as clear display, cursor at home, etc. If RS = 1 the data register is
selected, allowing the user to send data to be displayed on the LCD.
R/W - read/write:
R/W input allows the user to write information to the LCD or read information from it.
R/W = 1 when reading; R/W =0 when writing.
E - enable:
The enable pin is used by the LCD to latch information presented to its data pins. When
data is supplied to data pins, a high to low pulse must be applied to this pin in order for the LCD
to latch in the data present at the data pins. This pulse must be a minimum of 450 ns wide.
D0 – D7:
The 8 bit data pins, D0 – D7, are used to send information to the LCD or read the contents
of the LCD’s internal registers.
NagRaz 14
To display letters and numbers, we send ASCII codes for the letters A – Z, a – z, and
numbers 0 – 9 to these pins while making RS = 1.
There are also instructions command codes that can be sent to the LCD to clear the
display or force the cursor to the home position or blink the cursor. Table below lists the
instruction command codes.
We also use RS = 0 to check the busy flag bit to see if the LCD is ready to receive
information. The busy flag is D7 and can be read when R/W =1 and RS = 0, as follows: if R/W
=1, RS =0. When D7 = 1(busy flag = 1), the LCD busy taking care of internal operations and will
not accept any new information. When D7 = 0, the LCD is ready to receive new information.
Note: It is recommended to check the busy flag before writing any data to the LCD.
NagRaz 15
We can write program for LCD in two ways.
NagRaz 18
ACALL DATA
MOV A,#'D' ; display letter D
ACALL DATA
MOV A,#'S' ; display letter S
ACALL DATA
NagRaz 19
Solution: Blinking “GITAM” String
org 5000h
start: clr a
lcall bsychk
mov dptr,#0ff04h
mov a,#38h
movx @dptr,a
lcall bsychk
mov a,#01h
movx @dptr,a
lcall bsychk
mov a,#06h
movx @dptr,a
lcall bsychk
mov a,#0fh
movx @dptr,a
lcall bsychk
mov a,#80h
movx @dptr,a
mov dptr,#lookup
mov r2,#10h
lcall delay
more: lcall bsychk
mov a,#01h
mov p2,#0ffh
movx @r0,a
movx a,@dptr
mov p2,#0ffh
movx @r1,a
inc dptr
djnz r2,more
lcall delay
ljmp start
bsychk: mov r1,#04h
mov r0,#08h
mov p2,#0ffh
mov a,#02h
movx @r0,a
bsy: mov p2,#0ffh
movx a,@r1
anl a,#80h
jnz bsy
mov p2,#0ffh
mov a,#00h
movx @r0,a
ret
delay: mov r3,#0ffh
l2: mov r4,#0ffh
l1: djnz r4,l1
djnz r3,l2
ret
lookup: db
47h,49h,54h,41h,4dh,20h,55h,4eh,49h,56h,45h,52h,53h,49h,54h,59h
NagRaz 20
Program No: 05
Problem: Write ALP to rotate stepper motor.
Solution:
org 4100h
mov a,#80h
mov dptr,#0ff0fh
movx @dptr,a
start:
clock: mov r0,#03h
mov dptr,#look1
mov r1,dpl
mov r2,dph
acall run
djnz r0,clock
acall delay
NagRaz 21
run: mov r3,#0ch
go: movx a,@dptr
mov dptr,#0ff0ch
movx @dptr,a
mov r4,#01h
l3: mov r5,#11h
l2: mov r6,#0ffh
l1: djnz r6,l1
djnz r5,l2
djnz r4,l3
inc r1
mov dpl,r1
mov dph,r2
djnz r3,go
ret
look1: db 09h,05h,06h,0ah,09h,05h,06h,0ah,09h,05h,06h,0ah
look2: db 0ah,06h,05h,09h,0ah,06h,05h,09h,0ah,06h,05h,09h
NagRaz 22
Program No: 06
Problem: Write ALP to Print Characters using dot matrix printer.
Solution:
org 5000h
mov r0,#0eh
mov dptr,#look
rep: mov r1,dpl
mov r2,dph
mov a,#05h
mov dptr,#0ffd0h
movx @dptr,a
mov dpl,r1
mov dph,r2
movx a,@dptr
mov dptr,#0ffc8h
movx @dptr,a
mov a,#01h
mov dptr,#0ffd0h
movx @dptr,a
mov a,#0ah
mov dptr,#0ffc8h
movx @dptr,a
mov a,#01h
mov dptr,#0ffd0h
movx @dptr,a
mov a,#05h
mov dptr,#0ffd0h
movx @dptr,a
mov dpl,r1
mov dph,r2
inc dptr
mov r4,0fh
k1: djnz r4,k1
djnz r0,rep
look: db 47h,49h,54h,41h,4dh,20h,45h,53h,20h,4ch,41h,42h,0dh,0ah
NagRaz 23
Program No: 07
Problem: Write ALP to interfacing to HEX keyboard.
Solution:
org 4100h
keystart: mov dptr,#0ff0fh
mov a,#82h
movx @dptr,a
start: mov a,#00h
mov dptr,#0ff0ch
movx @dptr,a
mov dptr,#0ff0dh
movx a,@dptr
anl a,#0fh
cjne a,#0fh,cont
ljmp start
cont: lcall getdat
ljmp start
getdat: mov r2,#51h
mov r3,#00h
mov r4,#52h
mov r5,#00h
mov a,#0eh
lcall find1
mov a,#0dh
lcall find1
mov a,#0bh
lcall find1
mov a,#07h
lcall find1
ret
find1: mov dptr,#0ff0ch
movx @dptr,a
mov dptr,#0ff0dh
movx a,@dptr
anl a,#0fh
mov r6,a
mov r7,#04h
ffn2: mov dph,r4
mov dpl,r5
movx a,@dptr
mov dpl,r6
cjne a,dpl,ffn3
lcall storedat
ret
ffn3: inc r3
inc r5
djnz r7,ffn2
mov r4,#52h
mov r5,#00h
ret
NagRaz 24
storedat: lcall bsychk
mov dptr,#0ff04h
mov a,#38h
movx @dptr,a
lcall bsychk
mov a,#01h
movx @dptr,a
lcall bsychk
mov a,#06h
movx @dptr,a
lcall bsychk
mov a,#0fh
movx @dptr,a
lcall bsychk
mov a,#80h
movx @dptr,a
mov dph,r2
mov dpl,r3
lcall bsychk
mov a,#01h
mov p2,#0ffh
movx @r0,a
movx a,@dptr
mov p2,#0ffh
movx @r1,a
ret
org 5100h
db 30h,31h,32h,33h,34h,35h,36h,37h,38h,39h,41h,42h,43h,44h,45h,46h
org 5200h
db 0eh,0dh,0bh,07h
NagRaz 25
8051 Kit Programs
Program No: 01
Problem: Write ALP to Add two 16 bit numbers.
Solution:
org 4100h
clr c
mov a,#34h
addc a,#78h
mov dptr,#4200h
movx @dptr,a
inc dptr
mov a,#12h
addc a,#56h
movx @dptr,a
hlt: sjmp hlt
Program No: 02
Problem: Write ALP to add two multi byte numbers.
Solution:
org 4100h
mov r0,#45h
mov r1,#46h
mov r2,#47h
mov dpl,#00h
clr c
next: mov a,r0
mov dph, a
movx a,@dptr
mov r3,a
mov a,r1
mov dph, a
movx a,@dptr
addc a,r3
mov r4,a
mov a,r2
mov dph, a
mov a,r4
movx @dptr,a
inc dpl
djnz r5,next
jnc hlt
mov a,#01h
movx @dptr,a
hlt: sjmp hlt
NagRaz 26
Program No: 03
Problem: Write ALP to Subtract two 8 bit numbers.
Solution:
org 4100h
clr c
mov a,#59h
sub a,#50h
mov dptr,#4200h
movx @dptr,a
hlt: sjmp hlt
Program No: 04
Problem: Write ALP to multiply two 8 bit numbers with out using MUL instruction.
Solution:
org 4100h
mov r1,#04h
mov r2,#06h
mov r3,#00h
mov a,#00h
next: add a,r1
jnc pass
inc r3
pass: djnz r2,next
mov dptr,#4200h
movx @dptr,a
inc dptr
mov a,r3
movx @dptr,a
hlt: sjmp hlt
Program No: 05
Problem: Write ALP to divide two 8 bit numbers with out using DIV instruction.
Solution:
org 4100h
mov r0,#08h
mov r1,#04h
mov r2,#00h
mov a,r0
loop: cjne a,01h,loop1
inc r2
mov r3,#00h
jmp loop2
loop1: jc loop3
inc r2
subb a,r1
jmp loop
loop3: mov r3,a
loop2: sjmp loop2
NagRaz 27
Program No: 06
Problem: Write ALP to Split 8 bit numbers in to two Nibbles.
Solution:
org 4100h
mov dptr,#4500h
movx a,@dptr
mov b,a
anl a,#0fh
inc dptr
movx @dptr,a
mov a,b
swap a
anl a,#0fh
inc dptr
movx @dptr,a
hlt: sjmp hlt
Program No: 07
Problem: Write ALP to convert Packed BCD number in to two ASCII numbers.
Solution:
org 4100h
mov dptr,#4500h
movx a,@dptr
mov b,a
anl a,#0fh
add a,#30h
inc dptr
movx @dptr,a
mov a,b
swap a
anl a,#0fh
add a,#30h
inc dptr
movx @dptr,a
hlt: sjmp hlt
Program No: 08
Problem: Write ALP to convert BCD number in to HEX number.
Solution:
org 4100h
mov dptr,#4200
movx a,@dptr
mov b,#0ah
mul ab
mov b,a
inc dptr
movx a,@dptr
add a,b
inc dptr
movx @dptr,a
hlt: sjmp hlt
NagRaz 28
Program No: 09
Problem: Write ALP to convert HEX number into Decimal(BCD) number.
Solution:
org 4100h
mov dptr,#4200
movx a,@dptr
mov b,#64h
div ab
mov dptr,#4201
movx @dptr,a
mov a,b
mov b,#0ah
div ab
inc dptr
movx @dptr,a
inc dptr
mov a,b
movx @dptr,a
hlt: sjmp hlt
Program No: 10
Problem: Write ALP to sum of an array of HEX numbers.
Solution:
org 4100h
mov dptr,#4200h
movx a,@dptr
mov r0,a
mov b,#00h
mov r1,#00h
sum: clr c
inc dptr
movx a,@dptr
add a,b
mov b,a
jnc pass
inc r1
pass: djnz r0,sum
mov dptr,#4500h
mov a,r1
movx @dptr,a
inc dptr
mov a,b
movx @dptr,a
hlt: sjmp hlt
Program No: 11
Problem: Write ALP to sum of an array of BCD numbers.
Solution:
org 4100h
mov dptr,#4200h
movx a,@dptr
mov r0,a
mov b,#00h
mov r1,#00h
NagRaz 29
sum: clr c
inc dptr
movx a,@dptr
add a,b
da a
mov b,a
jnc pass
inc r1
pass: djnz r0,sum
mov dptr,#4500h
mov a,r1
movx @dptr,a
inc dptr
mov a,b
movx @dptr,a
hlt: sjmp hlt
Program No: 12
Problem: Write ALP to Sort an array of numbers.
Solution: org 4200h
mov r3,#04h
mov r4,#04h
mov dptr,#4200h
ptri: mov r5,dpl
mov r6,dph
movx a,@dptr
mov b,a
ptrj: inc dptr
movx a,@dptr
mov r0,a
clr c
subb a,b
check: jnc chknxt
exch: push dpl
push dph
mov dpl,r5
mov dph,r6
mov a,r0
movx @dptr,a
pop dph
pop dpl
mov a,b
movx @dptr,a
mov b,r0
chknxt: djnz r3,ptrj
dec r4
mov a,r4
mov r3,a
inc r4
mov dpl,r5
mov dph,r6
inc dptr
djnz r4,ptri
hlt: sjmp hlt
NagRaz 30