Lecture11 Peripheral Interface
Lecture11 Peripheral Interface
Lecture11 Peripheral Interface
8255 Programmable
Peripheral Interface
Topics
8255 Programmable Peripheral Interface
Application Examples
2
8255 Programmable Peripheral
Interface (PPI)
●
The 8255 chip is a general-purpose programmable Input/Output
interfacing device.
●
Can be used for parallel I/O interfacing with many
microprocessors such as Intel 8086, or with microcontrollers
such as Intel 8051.
●
It has 40-pins, 24 of them are I/O pins.
●
The 24 pins (I/O lines) are organized as 3 I/O ports labeled as
A, B, and C. (A port is a 8-bit register in the 8255 chip.)
●
There is also a control port.
●
Each I/O port is 8-bit, and can be separately used as input
or as output.
●
The chip can be used in 3 modes of operation.
●
The Control port is used to configure the mode of chip.
●
Port C can be divided in half, with the left-most four bits or
right-most four bits used as inputs or outputs.
Application Examples
using 8255 chip
●
LED interfacing
●
7-Segment LED Display interfacing
●
Switch / Button interfacing
●
Matrix keypad interfacing
●
Interfacing with motors
DC motor
Stepper motor
Servo motor
●
Interfacing A/D converter (Analog to Digital)
●
Interfacing D/A converter (Digital to Analog)
4
8255 Pins
Port A Port A
8255
Port C Data I/O
(upper)
Port C
(lower)
Port B
Port B
Ports in 8255
●
Chip Select Signal (CS) should be connected to the Address Decoding logic.
●
When CS (Chip Select) signal is 0, the 8255 chip is enabled.
●
Port A, Port B, Port C and Control Port have their own addresses.
●
Each port is 8-bit.
8255
D7-0
Port A PA7-0
RD
WR Port B PB7-0 I/O
Devices
A1
PC7-4
A0 Port C
PC3-0
Reset
Control
CS Port
6
8255 Functional Diagram
8
8255 Operating Modes
●
The 8255 PPI chip can be programmed to operate in
three modes:
Mode 0 : Basic Input/Output
Mode 1 : Strobed Input/Output (Handshaked)
Mode 2 : Bi-directional bus
8255
Operating
Modes
10
Mode 0 (Basic Input/Output)
●
Mode 0 gives the simplest form of I/O.
●
No handshaking is required.
●
Data is simply read from or written to the specified port.
●
Any of the ports A, B, C (upper half), and C (lower half) can be
set (configured) individually as input or output ports.
●
This is done by sending a control byte to the Control Port.
●
There are 16 possible control words.
●
Example: To configure all three ports (A,B,C) as output ports,
you need to send the control word 80h to the Control port using
the following instructions.
11
12
8255 Control Register
(Control Word Bits)
13
Hex
Value
80h
81h
82h
83h
88h
89h
8Ah
8Bh
90h
91h
92h
93h
98h
99h
9Ah
9Bh
14
Port selection
●
The CS (chip select) pin selects the entire 8255 chip.
●
Address pins A0 and A1 select the specific port within the chip.
●
The A0 and A1 are bits are part of the port addresses.
Selected
CS A1 A0
Port
0 0 0 Port A
0 0 1 Port B
0 1 0 Port C
Control
0 1 1 Register
8255 is
1 x x disabled
15
Port A1 and A0
Port Name
Address Address lines
A 60h 00
B 61h 01
C 62h 10
Control Register 63h 11
16
8255 System Bus Connection
System Bus Connections: Outputs are latched, inputs are buffered.
8086
CPU
RD
I/O Devices 17
18
Timing Diagram for Mode 0
Output Mode
●
After initialization of 8255, CPU can write data into the output port by
initiating write command with proper port address.
●
CPU sends data on the data bus and upon activation of WR signal, data
on the data bus gets latched on the selected output port.
19
20
Example1: Interfacing of 8255 in Isolated I/O
(Using Logic Gates for address decoding)
●
The 8086 CPU has special instructions IN and OUT to transfer data through the
input/output ports in Isolated I/O method.
●
M/IO signal is always low when 8086 is executing these instructions.
I/O
8086 Devices
CPU
21
Example3: Interfacing of 8255
in Memory Mapped I/O
●
The 8086 uses 20 address lines to
identify an I/O device.
●
An I/O device is connected as if it
is a memory register.
●
The 8086 uses same control
signals and instructions to access
I/O as those of memory.
●
RD and WR signals are activated
when M/IO signal is high,
indicating memory bus cycle.
●
Address lines A0& A1 are used by
8255 for internal decoding.
23
Topics
8255 Programmable Peripheral Interface
Application Examples
24
Example Question-1
●
Suppose the 8255 chip will be configured as follows.
Port A as input, B as output, and all the bits in C as output.
●
Determine the content of the Control Register.
●
Assume the port addresses of 8255 are as follows.
PortA : 60h
PortB : 61h
PortC : 62h
Control Port : 63h
25
Solution
The bit values of the Control Register should
be determined as follows.
D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 1 0 0 0 0
Control Register content = 90h
(Control word)
26
Solution
MOV AL, 90h ;The control byte is used to initialize the 8255
MOV DX, 63h ;assign the address of Control Register
OUT DX, AL ;output the Control Byte to the Control register
27
Example Question-2
●
Configure the ports of 8255 chip as follows:
Port C Lower bits : PC0-PC3 bits as switch input s
Port C Upper bits : PC4-PC7 bits as LED outputs
●
Determine the content of the Control Register.
28
Solution
Determine the Control Register bits (control word).
D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 x 0 0 x 1
29
Solution
MOV AL, 81h ;The control byte is used to initialize the 8255
MOV DX, 63h ;assign the address of Control Register
OUT DX, AL ;output the Control Byte to the Control register
30
Example Question-3
LEDs
●
The 12 switches are connected to
all 8 pins of Port B, and 4 lower
pins of Port C.
●
The 12 LEDs are connected to all
8 pins of Port A, and 4 upper pins
of Port C.
31
Solution
Determine the Control Register bits (control word).
D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 1 1 0 0 0
32
Program
Part1
;Define the port addresses
PortA EQU 00h
PortB EQU 02h
PortC EQU 04h
KontrolPort EQU 06h
.model small
.data
Cumle DB 'CIKMAK ICIN BIR TUSA BASINIZ',13,10,'$'
.code
BASLA PROC
.STARTUP
mov dx, OFFSET Cumle ;Move address of Cumle variable to DX
mov ah, 09h ;Function code for displaying string
int 21h
33
Part2
mov AL, 10011000b ;The control word
out KontrolPort, AL ;Initialize 8255 chip
MainDongu: ;Endless main loop
in AL, PortA ;Read 8 switches
out PortB, AL ;Send to 8 LEDs
in AL, PortC ;Read 4 switches
and AL, 0f0h ;Upper 4 bits filtered (taken)
mov CL, 04 ;Counter for rotate operation
ror AL, CL ;Rotate AL left 4 times
out PortC, AL ;Send to 4 LEDs
;--------------------------------------------------
MOV AH, 01 ;Check for keyboard press
INT 16H ;Using INT 16H
JE MainDongu ;Jump if ZF equal 1
;--------------------------------------------------
.EXIT
BASLA ENDP
END BASLA ;End of file
34
Example Question-4
●
3 LEDs with different colors are connected to Port C as shown below.
●
In order to light an LED, the connected port pin output should be made
active low (logic 0).
●
Write an Assembly program to continuously light one LED after another
at regular time intervals.
●
When user presses a key on computer keyboard, program exits.
●
Assume the following port address will be used for 8255 chip.
Port C address: 04h, Control Register address: 06h
Port C pins
PC0
8255
chip PC1
PC2
LEDs 5V
35
Solution
D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 x x 0 x 0
x : Assumed as zeros.
36
Sequence of walking LED patterns
●
In an endless loop, the following LED patterns will be send to the
Port C for the walking LED application.
●
A subroutine for waiting keyboard press will be called,
at each loop iteration.
37
Program
Part1
; Program for Walking 3 LEDs.
; (With 8255 PPI chip)
.model small
.stack ;Default stack size is 1 KB.
;CALL instruction uses the stack.
.data
Cumle DB 'CIKMAK ICIN BIR TUSA BASINIZ',13,10,'$'
.code
BASLA PROC
.STARTUP
mov dx, OFFSET Cumle ;Move address of Cumle variable to DX
mov ah, 09h ;Function code for displaying string
int 21h
38
Part2
mov AL, 10000000b ;The control word
out KontrolPort, AL ;Initialize 8255 chip
MainDongu: ;Endless main loop
;Implement the walking LED application:
mov AL, 00000000b ;Turn off all 3 LEDs
out PortC, AL
call Bekle
mov AL, 00000001b ;Turn on only PC0 LED
out PortC, AL
call Bekle
mov AL, 00000010b ;Turn on only PC1 LED
out PortC, AL
call Bekle
mov AL, 00000100b ;Turn on only PC2 LED
out PortC, AL
call Bekle
jmp MainDongu
.EXIT
BASLA ENDP
39
Part3
; Procedure (Assembly subroutine)
;Loop for making the program slower
Bekle PROC
MOV CX, 0FFh ;CX is counter for local loop
LokalDongu:
MOV AH, 01 ;Check for keyboard press
INT 16H ;Using INT 16H
JNE Dur ;If a key pressed, then stop
LOOP LokalDongu ;Decrements CX implicitly
RET ; Return from procedure
Dur:
;Stop program and return to operating system
MOV AH, 4CH ;Function code for stop
INT 21H ;DOS interrupt
Bekle ENDP
END BASLA ;End of file
40
Using Emu8086 Emulator for
testing the walking LED program
●
Run the program in Emu8086 emulator.
●
Open the Virtual Devices --> Traffic_Lights.exe program.
41
Example Question-5
●
4 rows of keyboard are connected to Port A, and
4 columns are connected to Port B of 8255 chip.
●
Write an Assembly program to continuously read the keyboard,
and detect a button press.
●
Base Address for 8255 chip ports is C0h.
8086
CPU
4x4 Keyboard
Matrix
3:8 Address
42
Decoder
Solution
Determine the Control Register bits (control word).
●
Port A is used as input.
●
Port B is used as output.
●
Port C is not used.
D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 1 x 0 0 x
x : Assumed as zeros.
43
Keypad Layout
Keypad matrix (4x4)
0 1 2 3
4 5 6 7
8 9 A B
C D E F
5V
Row1
5V
Row2
INPUTS
5V
Row3
5V
Row4
OUTPUTS
46
Initial status:
No buttons are pressed
5V
5V
1
INPUTS
5V
5V
1 1 1 1
OUTPUTS
47
5V
5V
1
INPUTS
5V
5V
1 0 1 1
OUTPUTS
48
S9 button is pressed by user
during Column2 selection
5V
5V
1
INPUTS
Pressed
5V
5V
1 0 1 1
OUTPUTS
49
5V
5V
1 Shorted
INPUTS
5V
5V
1 0 1 1
OUTPUTS
50
Selecting the Columns and
Reading the Rows of Keypad Matrix
The way to read a button matrix is to select (drive) one column output at a
time to the active low-level, and read (scan) all the input rows, looking to see
if any row is at the active low-level.
If a row input is at active low-level, then programs has detected a key press.
In that case, program calculates the key value with a formula.
The formula below is used to calculate the Key value (between 0 and 15).
51
4 5 6 7
C D E F
Key value = (Row-1)*(Number of Columns) + Column - 1
Solution:
Row = 3 Column = 4
Inputs Outputs
(keypad rows) (keypad columns)
1 1 1 1 1 1 1 0
1 1 1 1 1 1 0 1
1 1 1 1 1 0 1 1
1 1 1 1 0 1 1 1
53
Inputs Outputs
(keypad rows) (keypad columns)
1 1 0 1 0 0 0 0
1 0 1 1 0 0 0 0
0 1 1 1 0 0 0 0
54
Subroutines in Program
●
Write an Assembly program to continuously read the keypad matrix.
●
Program should detect any key press and store its value
(between 0 and 15) to a memory variable named KEY.
●
If user presses a key on computer keyboard, program exits.
Ma n
Program
SELECT_COLUMNS
Sub Routine
READ_ROWS CALCULATE_KEY
Sub Routine Sub Routine
55
Program
Part1
; Program for reading 4x4 Matrix Keypad.
; ( With 8255 PPI chip )
;----------------------------------
; MAIN PROGRAM
;----------------------------------
;Define the port addresses
PortA EQU 0C0h
PortB EQU 0C2h
Kontrol EQU 0C6h
.model small
.data
Cumle DB 'CIKMAK ICIN BIR TUSA BASINIZ',13,10,'$'
56
Part2
.code
BASLA PROC
.STARTUP
mov dx, OFFSET Cumle ;Move address of Cumle variable to DX
mov ah, 09h ;Function code for displaying string
int 21h
;----------------------------------
mov AL, 10010000b ;The control word
out Kontrol, AL ;Initialize 8255 chip
Son:
.EXIT
BASLA ENDP
57
Part3
;----------------------------------
; SUB ROUTINES
;----------------------------------
Kolon4:
MOV AL, 11110111b ;Select Column4
OUT PortB, AL
MOV COLUMN, 4
call READ_ROWS
MOV AL, ROW
CMP AL, 0
JE Donus1
Call CALCULATE_KEY
Donus1:
RET
SELECT_COLUMNS ENDP
59
Part5
;Subroutine reads (scans) the rows one-by-one.
;Determines which row is current if any key pressed.
READ_ROWS PROC
IN AL, PortA ;Read from port A
AND AL, 11110000b ;Filter leftmost 4 bits
Satir1:
CMP AL, 11111110b ;Check Row1 bit
JNE Satir2 ;If not equal, then check next row
MOV ROW, 1 ;Row detected, save it to variable
JMP Donus2 ;Return from subroutine
Satir2:
CMP AL, 11111101b ;Check Row2 bit
JNE Satir3
MOV ROW, 2
JMP Donus2
Satir3:
CMP AL, 11111011b ;Check Row3 bit
JNE Satir4
MOV ROW, 3
JMP Donus2
Satir4:
CMP AL, 11110111b ;Check Row4 bit
JNE Donus2
MOV ROW, 4
Donus2:
RET
READ_ROWS ENDP 60
Part6
;Subroutine calculates the KEY value with
;the formula by using ROW and COLUMN.
CALCULATE_KEY PROC
MOV AX, 0 ;Clear accumulator
MOV AL, ROW ;Get row value
DEC AL ;Decrement row value by 1
MOV BL, 4 ;Number of columns
MUL BL ;AL*BL executed, result is in AX
ADD AL, COLUMN ;Add column value to result
MOV KEY, BL ;Store calculated key to variable
RET
CALCULATE_KEY ENDP
61
Design Example1 :
8255 interface with 4x3 Keypad and 7-Segment LEDs
Common Cathode
7-Segment LEDs
Outputs
8086
CPU
Outputs
8255
Intputs
Pull-down 4x3
resistors Keypad
62
Address Decoder for
8255 Chip Selection
0 1 0 1 0 1 0 0 1 A9h
63
Design Example2 :
8255 interface with Two Multiplexed 7-Segment LEDs
Common
Anode
8255 PortA
(8 lines used for
LED segments)
8255 PortB
(2 lines used for
multiplexing)
64