You are on page 1of 13

Asthis

In demonstran_>d
diapte l'. .; Chaptcr 14, in the process of connecting the 8031/51 to external memory, two ports, PO and P2
nection of ther;; s iow how t-o e~v:ind the 1/O ports of the 8031 /51 by connecting it to~ 8255 chip. In Section 15_1' th
are l0s1.
devices s _ L/
1 51 to An 8255 1s shown. bi Section 15.2, the 8255 is progmmmed as a simple l/0 port for conne ~- e co11.
uc11as Ds, stepper motors, and ADCs. In &.'C lion 15.3, the 8051 C programming of the 8255 is discusse~ Ion With

SECTION 15·1: PROGRAMMING THE 8255


In
, tithis section. we stu l i)' ti1e c82"5
J cI1Ip,
· one of the most widely used 1/0 chips.
· We f"irst d escri"be Its
· fea tures
s110\~ ,e connection between the 8031/ 51 and 8255 chips. and the11

8255 features
ratel~,e 82~~ is a 40-pin DIP chip (see Figure 15-1). It has three sepa-
, acce~sible_po1is. 171c ports are each 8-bit, and are named A, B,
and C. The md1v1d ual ports of the 8255 b .
or output an :l b can e programmed to be input PA3 1 40 0 PA4
' h l _can e changed dynamically. In addition 8255 ports
have J,an d s akmg capabTty 1 1 ' tI,erebYallowmg. interface with
' devices PA2o 2 39 0 PAS
tl,at a lso I,ave handshakin . I PA1 3 38 0 PA6
in<> ca bir f tJ, . g sign~ s, such as printers. The handshak- 37 0 PA7
pa ity O e 8255 is not discussed since it is no longer used. 4
.::i
5 36 o WR
8255A 35 o RESET
6
PA0-PA7
GND 7 34 o DO
Al o .8 33 0 01
The S-bit port _A_ can be programmed as all input, or as all out-
9 32 0 02
put, or all bits as bidirectional input/ output.
PC7 10 31 0 03
PC6o 11 30 0 04
PB0-PB7 12 29 0 05
PC4 13 28 0 06
The 8-bit port B can be programmed as all input or as all output. 27 D D7
Port B cannot be used as a bidirectional port. PCO 14
PC1 o 15 26 O Vee
PC2 16 25 D PB?
PC0-PC7 PC3 17 24 0 PB6
PB0 o 18 23 D PBS
This 8-bit port C can be all input or all output. It can also be split 22 D PB4
PBl 19
into two parts, CU (upper bits PC4 - PC7) and CL (lower bits PCO - 21 PB3
PB2·o 20
PC3). Each can be used for input or output.

Figure 15-1. 8255 Chip


RDandWR (Reprinted by _permission of Intel Corporation, Copyright Intel
Corp. 1983)
_ These two active-low control signals are inputs to the 8255. The
RD and WR signals from the 8031 /51 are connected to tllese inputs.

DO - D7 data pin
PA
The data pins of the 8255 are connected to the data pins of the D7-DO 8255

.
microcontroller allowing it to send data back and forth between the PB
RD
controller and the 8255 ch ip.
WR
PC
AO -
RESET Al
This is an active-h igh signa l input into the 8255 used to clear the
control register. When RESET is activated, all ports are initialized as
input ports. In many de~igns th is pin is connected to the RESET output · cs RESET
of the system bus or gro und ed to make it inactive. Like all IC input
pins, it should not be left unconnected. ' Figure 15-2. 8255 Block Diagram

THE 8051 MICROCONTROLLER AND EMBEDDED SYSTEMS


390
.'.

{;,5 d_emonSrrated in Chapter 14, in the process of connecting the 8031/51 to external memory, two ports, PO and P2
tl~s chapter we show how to expand the I/0 potis of tl1e 8031/51 by connecting it to an 8255 chip. In Section 1s.1' :e lost,
nec~on of the 8031 / 51 to 1111 8255 is shown . In Section 15.2, the 8255 is programmed as a simple 1/0 port for connecti econ.
devices such as LCDs, stepper motors, and /\DCs. In Section 15.3, the 8051 C programming of the 8255 is discussed. on \.vith

SECTION 15.1: PROGRAMMING THE 8255


In this section we stud y the 8255 chip, one of the most widely used 1/0 chips. We first describe its features and th
show the connection between t·he 8031/ 51 and 8255 chips. en

8255 features
n,e 8255 is a 40-pin DIP chip (see Figure 15-1). It has three sepa-
rately aceessible ports. n,e ports are ead1 8-bit and are named A B
and C. TI1e individual ports of the 8255 can be pr~grammed to be in~u; 40 0 PA4
PA3 1
or output, and _can be changed dynamically. In addition, 8255 ports 39 O PAS
have handshaking capa b 1·1·1ty, th ereby allowing interface with devices PA2 2
PAI 3 3B PA6
t11at also l_,~ve handshaking signals, sud, as printers. The handshak-
4 37 0 PA?
mg capability of the 8255 is not discussed since it is no longer used.
RDoS
CS 6 8255A 35 0 RESET
PA0-PA7 GNDo7
.8 33 0 Dl
The 8-b!t port _A_ can _be programmed as all input, or as all out-
9 32 0 D2
put, or all bits as b1d1rectional input/output.
PC7 10 31 0 D3
PC6oll 300D4
PB0-PB7 PCS 12 29 OS
PC4 13 28 D6
The 8-bit port B can be programmed as all input or as all output.
PCO 14 27 D D7
Port B cannot be used as a bidirectional port.
PClo 15 26
PC2 16 25 D PB7
PC0-PC7 PC3 17 24 PB6
PBO 18 23 D PBS
This 8-bit port C can be all input or all output. It can also be split PBl 19 22 D PB4
into two parts, CU (upper bits PC4 - PC7) and CL (lower bits PC0 - 20 21 D PB3
PC3). Each can be used for input or output. L....---------'
Figure 15-1. 8255 Chip
RDandWR (Reprinted by permission of Intel Corporation, Copyright Intel
Corp. 1983) .
_ These two active-low control signals are inputs to the 8255._The
RD and WR signals from the 8031/51 are connected to these inputs.

DO - D7 data pin
)I
The data pins of the 8255 are connected to the data pins of the 07-00 8255
microcontroller allo wing it to send data back and forth between the
RD PB
controller and the 8255 chip.
WR
PC
AO-
RESET Al
This is an active-high signa l input into the 825~ used to clear the
control register. When RESET is activa ted, aJJ ports are initialized as
input ports. ln many designs this pin is connected to the RESET output cs RESET
of the system bus or gro unded to make it inactive. Like all IC input
pins, it should not be left unconnected. ' Figure 15-2. 8255 Block Diagram

390 THE 8051 MICROCONTROLLER AND EMBEDDED SYSTEMS


'
1 snd~ Table 15-1: 8255 Port Selection '
flO,ft '. e ES (chip select) selects the entire chip, it is AO and Al
specific ports. ~ese three pins are used to access ports
cs Al AO Selection
the control register as shown in Table 15-1 • Note th a t 1s
,1,el . 0 Port A
1i c or
0
0 0
B ' 1 PortB
.~,.;e-JoW• 0 0
1cU'
0 1 0 PortC
selection Of the 8255 o 1 1 Control register
,1od 8 · d
11' ,,v11ile ports A, B, and C are used to input or output data, it is 1 x x 8255 1s not selecte
trol register that must be programmed to select the opera-
the co:de of the three ports. The ports of the 8255 can be programmed in any of the following modes.
"onf11
r-,1ode 0, simple I/~ mode. In this mo~e, any of the ports A, B, CL, and CU can be programmed as input or output.
111 this mode, all bits ar~ out or all are m : other words, there is no such thing as single-bit control as we have se~
. po- P3 of the 8051. Smee the vast ma1onty of applications involving the 8255 use this simple 1/0 mode, we will
: ncentrate on this mode in this chapter.
, Mode 1. In this ~ode, ports A ~nd B can be used as input oroutput ports with handshaking capabilities. Handshaking
signals are provided by the bits of port C. This mode will not be explored further in this book.
Mode 2. In this mode, port A can be used as a bidirectional 1/0 port with handshaking capabilities whose signals
-'· are provided by port C. Port B can be used either in simple 1/0 mode or handshaking mode 1. This mode will not
be explored further in this book.
SSR (bit set/reset) mode. In this mode, only the individual bits of port C can be programmed. This mode will not
be e,._-pJored further in this book. .

Figure 15-3 shows the control word for mode selection of the 8255.

Simple 1/0 programming


Intel calls mode 0 the basic input/output mode. The more commonly used term is simple I/O. In this mode, any of ports
A, 8, or C can be programmed as input or output. It must be noted that in this mode, a given port cannot be both an
input and output port at the same time.

Group A GroupB

~ Lod:IL_o--,-6__ o7s__ JL_ _ ...Jll_P_o_,~7~r--"---D73r-~•M-o-d-:7:1e-le_c_ti~.o~n- -D


- 1 _ __._\____
oo_Pr o_ rt
_ C_.

o B 1 = Input O = Mode 0 I (Lower


___SR Mode 1 = Mode 1 \ '
o = Output , PC3 - PC0) '\
I 1 = input
I I

f" ·M ode Selection PortC -~~;;{~ : :;· · ( Output ,


00 =Mode0 (Upper
01 = Mode 1 PC7- PC4)
1 = Input \ --~ =Output .\
lx = Mode 2
0 = Output
•n• •• • n • - - • n , . . ,., ,


(~ure 15-3. 8255 Control Word Format U/? Mode) . . .
Pnnted by permission of Intel Corporation, Copynght Intel Corp., 198.3)

8051
lNTERFAqNG WITH THE.8255 391
Example 15-1
Find the control word of the 8255 for the following configurations:
(a) All the ports of A, B, and Care output ports (mode 0).
(b) PA= iri, PB= out, PCL = out, and PCH = out.

Solution:
From Figure 15-3 we have:
(a) 1000 0000 = 80H (b) 1001 0000 = 90H

Connecting the 8031/51 to an 8255


The 8255 chip is programmed in any of the 4 modes mentioned earlier by sending a byte (Intel calls it a
word) to the control register of the 8255. We must first find the port addresses assigned to each of ports A, B, c, aco~troJ
1
control register. Tilis is called mapping the 1/0 port. n he
As cai_1 be seen ~om Figure 15-4, the _8255 is connected to an 8031/51 as if it is RAM memory: No~c~ the use ofRJ5
and WR signals. This method of connecting an 1/0 chip to a CPU is called memory-mapped l/O, smce It 1s mapped .
. F h' . . mto
memory space. In o th er word s, we use memory space to access I / O devices. or t IS reason we use mstruchons such
MOVX to access the 8255. In Chapter 14 we used MOVX to access RAM and ROM. For an 8255 connected to the 8031/;s1

-
we must also use the MOVX instruction to communicate with it. This is shown in Example 15-2.

Example 15-2
Refering to Figure 15-4.
(a) Find the addresses for the ports A, B, and C and the control register.
(b) Four switches are connected to the lower 4-bit lines (PBO - PB3).
Write a program to transfer the status of these switches to LEDs connected to the upper 4-bits of port A
(PA3-PA7).

Solution:
(a) The address of the ports is such that only if Al 4 is 'l' the chip will be selected. Also the condition of Al and AO
for the ports and control register are as follows:
Al AO
Port A 0 0
PortB 0 1
PortC 1 0
Control Register 1 1
The rest of the address lines can be 1 or 0. For example, if we take all these lines as 0, the addresses will be 4000H
for port A, 4001H for port B, 4002H for port C, and 4003H for the control register.
(b) Since the switch information has to be read into the A register, port B has to be an input port. Port A is an output
port because it is connected to LEDs which display data fed into it. The control word is 10000010, i.e., 82H.
MOV A, #82H ;move control word to A
MOV DPTR,#4003H ;load control register address
MOVX @DPTR,A ;issue control word to control register
MOV DPTR ,#4001H ;move port B address to DPTR
MOVX A, @DPTR ; move port B data to A
SWAP A -;switch nibbles of A to bring switch data
;to the upper nibble
MOV DPTR,#4000H ;move port A address to DPTR
MOVX @D PTR , A ;move the content of A to por t A
END

392 THE 8051 MICROCONTROLLER AND EMBEDDED SYSTEMS


\ .
i

'

RD
p3.6 WR
I I
p2.7 A14 - { ) o - cs WR RD

P2.0 PA i---
ALE G 8255 PB
P0.7 AD7
,__ Al PC i---
Al
74LS373 1 - - AO
po.o A Du
AO
oc D7 DO RES 1
L---- __L
D7
I\\ +
DO I

154 . 8051 Connection to the 8255 for Example 15-2


fiSurc

E,:aJI\Ple 15-3
for Figure 15-5'
(a) Find the 1/0 port addresses assigned to ports A, B, c, and the control register.
(b) Find the control byte for PA = in, PB = out, PC = out.
(c) Write a program to get data from PA and send it to both ports B and C.

Solution:
(a) Assuming all the unused bits are Os, the base port address for 8255 is 1000H.
Therefore we have: lOOOH PA
1001H PB
1002H PC
1003H Control register
(b) The control word is 10010000, or 90H.
(c)
MOV A,#90H ; (PA=IN,PB=OUT,PC=OUT)
MOV DPTR,#1003H; ;load control reg pdr_t addr
MOVX @DPTR,A ;issue control word
MOV DPTR,#l000H ;PA address
MOVX A,@DPTR ;get data from PA
INC DPTR ;PB address
MOVX @DPTR,A ;send the data to PB
INC DPTR ;PC address
MOVX @DPTR,A ;send it also to PC

For the program in Example 15-3, it is recommended that you use the EQU directive for port addresses as shown
next.
APORT EQU 1000H
BPORT EQU 1001H
CPORT EQU 1002H
CNTPORT EQU 1003H

8051
INTERFACING WITH THE 8255
393
- -- - - - - -- ~
8051
P3.7 RD
P3.6 WR

P2.7
Al"
Al4
Al3- l} CS
WR R]---

P2.0 PA.
ALE
P0.7
AD7
-~
IG

Al
8255
PB
PC
''
PO.O 74LS373 .__AO AO
ADO
oc D7 DO Res

-==- D7 1 1-t
-
DO -

Figure 15-5. 8051 Connection to the 8255 for Example 15-3

MOV A, #90H ; (PA=IN,PB=OUT,PC=OUT)


MOV DPTR,#CNTPORT ;load cntr reg port addr
MOVX @DPTR,A ;issue control word
MOV DPTR,#APORT ;PA address
MOVX A,@DPTR ;get data from PA
INC DPTR ;PB address
MOVX @DPTR,A ;send the data to PB
INC DPTR ;PC address
MOVX @DPTR,A ;send it also to PC
or the following, also using EQU:
CONTRBYT EQU 90H ; (PA=IN,PB=OUT,PC=OUT)
BAS8255P EQU 1000H ;base address for 8255 chip
MOV A,#CONTRBYT
MOV DPTR,#BAS8255P+3 ;load c port addr
MOVX @DPTR, A ;issue control word
MOV DPTR,#BAS8255P ;PA address

Notice in both Examples 15-2 and 15-3 that we used the DPTR register since the base address assigned to 8255 111
16-bit. If the base address for the 8255 is 8-bit, we can use the instructions "MOVX A, @RO" and "MOVX @RO, A" where RO((
Rl) holds the 8-bit port address of the port. See Example 15-4. Notice in Example 15-4 that we used a simple logic gate tod
the address decoding for the 8255. For multiple 8255s in a system, we can use a 74LS138, as shown in Example 15-5.

Example 15-3a
Referring to Figure 15-5,
(a) Find the addresses of the ports and control register.
(b) Write a program to generate a square wave at bit Oof port C.

Solution: )

(a) Assuming all the unused address lines are 0, and noting that A12 has to be 1, the addresses are

ysrEM1
394 THE 8051 MICROCONTROLLER AND EMBijVDED S
1000H I
1P. 1001H
por 8 1002H
0
P ~c ·ster 1003H
po !fOl regt
cofl enerate a sq~are wave of any time period with a 33% duty eye 1e, 1t . necessary to have an OFF time which
. 1s

1n,) foS ·ce the ON time. nf din any way


have the square
" is !'\Tl . . wave
00 at PCO, PCL must be an output port. All the other ports can be co tgure ·
fo ntr0l word 1s 1 00000, i.e., 80H
.
MOV A,#SOH ;control word to A
MOV DPTR,#1003H ;DPTR po i nts to control r egi s t e r
MOVX @DPTR,A ;transfer control word
MOV DPTR,#1002H ;DPTR points to port C
MOV A,#OlH ; A=OlH
sr,1;,1<T : MOVX @DPTR' A ; PCO = l
ACALL DELAY ;call a delay for the ON time
MOV A,#OOH
;A=O
MOVX @DPTR, A
ACALL DELAY ;PCO =O
ACALL DELAY ;call delay for OFF time
SJMP START ;one more delay time for OFF t ime
END ; repeat to generate a continuous waveform·

l •
Example 15-4

For Figure 15-6, a switch SW is connected to PCO.


(a) If SW = 1/ the data received from port A is to be.transferred to port B. _
(b) If SW = 0, the data received from port B is to be transferred to port A. Write a program for this to be done
continuously.
Solution:
We have two cases
(1) When SW = 1, port A is an input porh ind port B is an output port. Port C lower is an input. The control word
is thus 10010001 = 91H '
(2) When SW= O, port B is an input port arid port A is an output port. Port C lower is an input port. The control
word then is 10000011 = 8~H. From Figure,15-6, }Ve see that the addresses of port A, B, C and Control register are-
resp.ectjvely, 20H, 21H, 22H _<!fld 23H.
CWDl EQU ·" 91H
CWD2 EQU 83H
PORTA EQU 20H
Pa°RTB EQU ":nH
PORTC EQU 22H
CONREG EQU 23H
MOV A,#CWDl ;move one of the c ont r ol wo r ds into A - t h is
;ensures that por t C ~~wer i s . a n inpu t port
MOV RO , #CONREG ; move the c ont r ol ~eg ister ad9r~ss t9 RO
MOVX @RO,A ;move the da t a in A to the adqress pointed
; by RO, i .e ., t h e co~trol r egister
~PT ,
MOV RO,#PORTC ·move por t C address to RO
MOVX A, @RO ;ge t ·port ' C data into A · ·

;;---
51
- - - - ------:--
INTERFACING WITH THE 8255 ' ' ..
- - - - - -- - -- - -
395
;check LSB of A by rotating A into carry
RRC A ;if PCO=l, jump to THERE
JC THERE ;if PCO=O, change the control word
HERE: MOV A, #CWD2 ;
MOV RO,#CONREG
MOVX @RO , A
RO,#PORTB ;let RO point to input por t B
MOV
MOVX A, @RO ;get port B data into A
RO ,# PORTA ;let RO point to output port A
MOV ;move the data in A to port A
MOVX @RO,A
RPT ;repeat the pr ocess
SJMP
RO, #PORTA ;let RO point to input port A
THERE: MOV
MOVX A, @RO ;move data from input port A to A reg.
MOV RO,#PORTB ;let RO point to output port B
MOVX @RO,A ;move data in A to ·port B
SJMP RPT ;repeat
END

l
J
P3.7 RD
WR ,_ I
P3.6
WR RD
8 A.AS A6!0c.: cs
,~ >
..--A3 A2 - PA
0
5 8255 PB
1
ALE ,c -
PCL
AD7
PQ,7 . = Al Al -
PCU
AO AO
74LS373
P0.0 D7 DO RES
ADO
oc _L
L-

- D7

DO

Figure 15-6. 8051 Connection to the 8255 for Example 15-4

74LS138
Address aliases
In Examples 15-4 and 15-5 we decode the AO - A7 address bits; A2 A AO
however, in Examples 15-3 and 15-2 we decoded only a portion of the A3 B Al
upper addresses of A8 - A15. This partial address decoding leads to A4 C
what is called address aliases. In other words, the same physical port has 8255
AS G2A
different addresses; thus, the same port is known by different names.
A6 G2B cs
In Examples 15-2 and 15-3 we could have changed-all x's to various
combinations of ls and Os to come up with different addresses, yet A7 Gl
they would all refer to the same physical port. In your hardware
reference documentation, make sure that all address aliases are doc- ·
umented, so that the user knows what addresses clre c\Vailable if he/ 138
Figure 15-7. 8255 Decoding Using 74LS
she wants to expand the system .

- - -- -----------....--- ------------;;;
396 sYS THE 8051 MICROCONTROl,LER AND EMBJiIJ.DED
1 15-5 - - - -:-:--:::--:-=:--:::-;:; - - - - - - - - - - - - - - - - - - - - 7
~,JIIP e ddress for the 8255 in Figure 15-7.
atJie t,ase a
fill
1utiOJl:

GI
GZB G2A
AS
C
A4
B
A3
A
A2 Al AO
Address

0 0 1 0 0 0 BBH
i7 0

8031
..-:.:.'--;::=::-;~ - - - - - - - - - - - RD - - - - - - - - - - - - - - - - 7
EA P3 .7 WR
f PSPE3N.61-- - - - - - - - - - - - -T --;:~==~......-~V~c-,
L_j CE
c
OE .Jppl
J
WR RD I I
1
P2.7 - A12 - A12 CS PA
2864
P2.O AB - AB (2764) 8255
A L E i - - - - - - -- ---,G 8Kx8
PB
P0.7 AD7 r- : _ ~7 ~
=D±=Q=:-,:::-J A7 - A7 p"togram
ROM PC
P0.0,-- 74LS373 Al
~ADO .....--- AO- AO D7 DO \AO
oc RES \
-l
_ D7 I\\\
IUJ
DO

figure 15-8. 8031 Connection to External Program ROM and the 8255

8031 system with 8255


In the 8031-based system where external progra_m ROM is an absolute must, the use of an 8255 is most welcome. This
t1 due to the fact that in the process of connecting the 8031 to .external program ROM, we lose the two ports PO and P2,
leaving only PL Therefore, connecting an 8255 is the best way to gain some extra ports. This is shown in Figure 15-8.

Review Questions
l. Find the control byte if all ports are inputs.
rmd the control byte for PC = in, PB = out, and PA = out.
'· True or false. To avoid aliases, we must decode addresses AO - A15.
Can 86H be the base address for port A of the 8255?
i. Why do we use the MOVX instruction to access the ports of the 8255?

SECTION 15.2: 8255 INTERFACING


.IO . Chapters 12 and 13 detailed real-world interfacing of LCDs, sensors, and ADC devices In this t· sh
IJ\lerface the 8255 to LCDs, stepper motors, and ADC d evices,
• th en program it using 8051
- instructions.
sec 10:n we ow how
Step
Per motor connection to the 8255 ,

I~
~ 5Chapter 17 will detail the interface of a stepper motor to the 8051. Here we show steppel· mot0t· c
¾d progr,..,..,-,_ Se p·
-m,uug. e 1gure 15 _9
.
·
,
o:nnection to the

L:'AQING WITH THE 8255


8255 ULN2003 Stepper Motor
DO
DO
D7 PAO
07
fron1
8051
WR
RD
AO
Al
WR
RD
AO
Al PA3
PAl
PA2 0
A2 cs RESET
Decoding
Circuitry
A7
1..--------. COM
ULN2003 C01mection for Stepper Motor COM
Pin8=GND +5V
Pin 9 = +5V
Use a separate power supply for the motor

Figure 15-9. 8255 Connection to Stepper Motor

LCD
MOV A,#S0H ; cont·r ol- word 'for PA= out 8255
MOV Rl,#CRPORT ;control reg port address PAO DO V
cc +SV
MOVX @Rl,A ; configure PA= out
MOV Rl,#APORT ; i'oad PA a:ddress · VEE I~
D7
MOV A,#66H ;A=66H,stepper motor sequence Pl)
AGAIN: MOVX @Rl,A ;issue motor, sequences to PA RS R/WE Vss
RR A ;rotate ·sequence for clockwise PBO
ACALL DELAY ;wait PIH
SJMP AGAIN PB2

RESET
LCD connection to the 8255 -
Program 15-1 shows how to issue commands and data to an LCD Figure 15-10. LCD Connection
connected to an 8255. See Figure 15-10. In Program 15-1, we must put a
long delay before issuing any information (command or data).to .the LCD. A-better way is to check the busy flag befu
issuing anything to the LCD. lhls was discussed in Chapter 12. Program 15-2 is a repeat of Program 15-1 with the che:
ing of the busy flag. Notice that no DELAY is used the main program in Program 15-2.

; Writing commands and data to LCD without checking busy flag


•Assume PA of 8255 connected to D0-D7 of LCD and - -
:PBO=RS, PBl=R/W, PB2=E for LCD's control pins c~nnecti~n
MOV A, #B0H ;all 8255 ports as output
MOV R0,#CNTPORT ;load control reg address
MOVX @R0,A ;issue , control word
MOV A,#38H ;LCD:2 lines, 5x7 matrix
ACALL CMDWRT ;write command to LCD
ACALL DELAY ;wait before next issue(2 ms)
MOV A,#0EH ;LCD co~mand for cursor on
ACALL CMDWRT ;write command to LCD
MOV A, # 0l H r, , ; clear 1 LCD
ACALL CMDWRT ;write command to LCD
ACALL DELAY ;wait before next issue

Program 15-1. (con ti11ued on fol/owinJ? page)

398
.• v11vs"s
THE 8051 MICROCONTROJ..LER AND EMB~P
;shift cursor right
;,.,#06 CMDWRT command
;,.Cl'r.,I., DELAY ,·wr ite
· command to LCD
i
I•
;,.Cl'r.,I., ;wait before next i ssue
;•d etc
' . for a l l LCD commands
A,#'N '
r,1OV DATAWRT ' isplay data (let t e r N)
;,.Cl'r.,r., ;send
;wait dab t a to LCD di s play
DELAY
p,.C1'r.,r.,
A,#'0' ·dis efor e next i s sue
r,1OV ' play data (letter O)
DATAWRT ;se~d data to LCD d i splay
p,.CJ>.r.,L
DELAY ;wait before next issue
p,.CAr.,L
subroutine, · . ; etc · for other data
d write RO,#APORT writes inS t ruction commands to LCD
11 ;load port A a dd ress
.co 111af ·
111
r,1OV @RO,A
.
J!011Jl. • MOVX '· issue in
· f o to LCD data pins
RO,#BPORT ;load port B address
MOV
A,#00000100B ;RS=O,R/W=O,E=l for H-to - L
MOV
MOVX
@RO,A ;activate LCD pins RS,R/W,E
NOP ;make E pin pulse wide enough
NOP
MOV A,#00000000B ;RS=O,R/W=O , E=O for H-to-L
MOVX @RO,A ;latch in data pin info
RET
a write subroutine, write data to be displayed to LCD
.pat ;load •port A address
\ TAWRT : MOV RO, #APORT
p, MOVX @RO I A ;issue info to LCD data pins
MOV RO,#BPORT ;load port B address
MOV A,#00000101B ;RS=l,R/W=O,E=l for H-to-L
@RO,A ;activate LCD pins RS,R/W,E
MOVX ;make E pin pulse wide enough
NOP
NOP ; RS=l,R/W=O,E=O for H-to-L
MOV A,#00000001B
;latch in LCD's data pin info
MOVX @RO,A
RET

Prograill 15-1. (continued from previous page)

;wri ting commands to the LCD with checking busy flag


;PA of 8255 connected to DO-D7 of LCD ·and
;PB0 =RS, PBl=R/W, PB2=E for 8255 to LCD' s control pins connect.
1
MOV A,#80H ;all 8255 ports as output
MOV RO,#CNTPORT ;load control reg address
;issue , control word
MOVX @RO,A ;LCD: 2 LINES, 5X7 matrix
MOV A, #38H ; write command to LCD
ACALL NCMDWRT ;LCD command for cursor on
MOV A, #OEH ;write command to LCD
ACALL NCMDWRT ;clear LCD
MOV A,#OlH ;write command to LCD
ACALL NCMDWRT ;shift cursor r i ght command
MOV A,#06 ;write 1 command to LCD
ACALL NCMDWRT
Pr0
&ram lS-2 . -
· (contin ued on following page)

l 80s1lNtERFAC--ING WITH THE 8255


llllli.....
399
•etc. for all LCD commands
;display data (letter N)
MOV A,#'N' , send data to LCD display
ACALL NDATAWRT '.display data (letter O)
MOV A,#'0'
'.send data to LCD display
CALL NDATAWRT
'.etc. for other data
;New command write subroutine with checking busy flag
NCMDWRT: MOV R2 , A •save A value
MOV '.PA=IN to read LCD status
A,#90H
MOV '.1oad control reg address
RO, #CNTPORT
MOVX @R0,A ; configure PA=IN, PB=OUT
MOV A,#00000ll0B ;RS=O,R/W=l,E=l read command
MOV R0,#BPORT •load port B address
MOVX @R0,A ;RS=O,R/W=l for RD and RS pins
MOV R0,#APORT ;load port A address
READY: MOVX A,@R0 ·read command reg
RLC A
-~ove D?(busy flag) into carry
JC READY '.wait until LCD is ready
MOV A,#B0H '.make PA and PB output again
MOV R0,#CNTPORT ;load control port address
MOVX @R0,A ·issue control word to 8255
MOV A,R2 ;get back value to LCD
MOV R0,#APORT ;load port ,A address
MOVX @R0,A ;issue info to LCD' ~ data pins
MOV R0,#BPORT ;load port B address
MOV A,#00000l00B ;RS=0,R/W=0,E=l for H-to-L
MOVX @R0,A ;activate RS,R/W,E pins of LCD
NOP ;make E pin pulse wide enough
NOP
MOV A,#00000000B ;RS=0,R/W=0,E=0 for H-to-L
MOVX @R0.,'A ;latch in LCD'S data pin info
RET
;New data write subroutine with checking busy flag
NDATAWRT : MOV R2,A ;save A value
MOV A,#90H ;PA=IN to read LCD status, PB=OUT
MOV R0,#CNTPORT ;load control port address
MOVX @R0,A ; configure PA 7 IN, PB=OUT
MOV A,#00000ll0B ;RS=0,R/W=l,E=l to read command reg
MOV R0,#BPORT ;load port B address
MOVX @R0,A ;RS=O,R/W=l for RD and RS pins
MOV R0,#APORT ;load port , A address
READY: MOVX A,@R0 ;read command reg
RLC A ;move D?,(busy flag) into carry
JC READY ;wait until LCD is ready
MOV A,#80H ;make PA and PB output again
MOV R0,#CNTPORT ;load control port address
MOVX @R0,A ;issue control word to 8255
MOV A,R2 ;get back value to be sent to LCD
MOV R0,#APORT ;load port A address
MOVX @R0,A ;issue info to LCD'S data pins
MOV R0,#BPORT ;load port B address
MOV A,#00000101B ;RS=l,R/W=0,E=l for H-to - L
MOVX @R0,A ;activate RS , R/W,E pins of LCD

Program 15-2. (continued on following page)

srs1~1l'
400 THE 8051 MICROCONTROU.ER AND EMBEDDED
NOP ;make E pin pulse wide enough
NOP
r,!OV A , #00000001B ;RS =l ,R/W= O,E= O for H- to - L
r,1OVX @RO,A ;latch i n LCD'S data pin info
RET

( contin11ed fro m previous page)


15·2·
Irt"5raf11
ADC8O4
RD Vee
WR CLKR
CLKIN
PAO DO
Vin(+)
WR D7 Vin(-) 10k
from
WR POT
8051 RD RD
AGND
AO
Vref/2
A2
A7
==t Decoding
Circuitry
Al
o cs
PA7
PCO
D7 GND -=-
INTR
RESET cs
-

figure 15-11. 8255 Connection to ADC804

ADC connection to the 8255


ADC devices were covered in Chapter 13. The following is a program for the ADC connected to the 8255 as shown
in Figure 15-11.
MOV A,#BOH ;control word for PA=OUT,PC=IN
MOV Rl,#CRPORT ;control reg port address
MOVX @Rl,A ; configure PA=OUT AND PC= IN
BACK : MOV Rl,#CPORT ;load port C address
MOVX A,@Rl ;read port C to see if ADC is ready
ANL A,#00000001B ;mask all except PCO
JNZ BACK ;ke.e p monitoring PCO for EOC
;end of conversion, now get ADC data
MOV Rl,#APORT ;load PA address
MOVX A,@Rl ;A=analog data input
Ian So far we have discussed the simple f/0 mode of the 8255 and showed many applications for it using Assembly
guage. Next, we discuss how to program the 8255 using 8051 C language.

Review Ouestions
1
· Moct·fy k .
2. T I the stepper motor program to turn countercloc _wise. .
rue or false . In programming the LCD via an 8255 (without checking the busy flag), poxt A is always an output
Port. · ·
3. True or false. In the LCD connection to the 8255, we must h ave a long delay before issuing the next data if we are
notch kin
ec g the b usy flag.

8osi INT
ERFAlliNG WITH THE 8255 401

You might also like