Section 15 1: Programming The 8255: It in
Section 15 1: Programming The 8255: It in
Section 15 1: Programming The 8255: It in
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
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.
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
{;,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
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
Figure 15-3 shows the control word for mode selection of the 8255.
Group A GroupB
p·
(~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
-
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
'
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
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 -
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
l •
Example 15-4
;;---
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
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
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?
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
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.
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
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
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