09 Io
09 Io
Part 9
Basic Input & Output Interface
(Hall’s Ch 9; Brey’s Ch 10)
I/O
Q: In Isolated I/O, an I/O port has the
same address as one of the memory
byte. How does it know it is being
addressed? 00000
(b)
a Parallel data transfer involves using at least 8 separate lines for the
8 data bits in a byte. Normally, other lines are needed for the
communications protocol (eg. STB [data strobe] line to indicate
when data is valid, ACK line to acknowledge data has been read).
STB is sent by sender ACK is sent by receiver
a Parallel data transfer is usually faster. Each data bit typically needs
its own ground return line to reduce noise. A popular parallel data
transfer interface standard is the CENTRONICS type interface
which uses a 36-pin connector. The Centronics interface is
commonly used in printers.
ELEG 3230 - Part 9 9
Parallel and Serial Data Transfer
(cont.)
a Serial data transfer involves sending the data on a single line, bit
by bit. The I/O interface converts the data from parallel to serial or
vice-versa using shift registers.
Q: Why serial data transfer is more commonly used in long distance transmission?
one frame
MSB LSB
simple
data
(no handshake)
strobe
Tx
Rx ACK Double
Handshake
Note:
Mode 2 is a bidirectional transmission mode; not necessary using double
handshaking. ELEG 3230 - Part 9 15
8255A Programmable Parallel
Port Device
Power +5V
GROUP A I/O
Supply GND GROUP PORTA
PA7-PA0
A (8)
CONTROL
RD
WR READ/ GROUP GROU B
WRITE B PORT B
I/O
A1 CONTROL CONTROL PB7-PB0
LOGIC (8)
A0
RESET
CS 8255
ELEG 3230 - Part 9 16
8255A Mode 0 Operation
a In Mode 0 operation, no handshaking will be used.
a The two halves of port C are independent and can be set as input
or output port independently.
CONTROL BUS
DATA BUS
R D, W R D7-D0 A0-A1
CS
8255A
MODE 0 C
B A
NO
HANDSHAKE 8 I/O 4 I/O 4 I/O 8 I/O
SIGNALS
PB7-PB0 PC3-PC0 PC7-PC4 PA7-PA0
ELEG 3230 - Part 9 18
8255A Mode 1 Operation
a In Mode 1 operation, single handshaking (strobed) is used.
a In this mode, some of the port C pins are used for handshaking
purpose.
`If Port A is set to mode 1 & input port: PC3, PC4, and PC5 pins are used.
`If Port A is set to mode 1 & output port: PC3, PC6, and PC7 pins are used.
`If Port B is set to mode 1 & input port: PC0, PC1, and PC2 pins are used.
`If Port B is set to mode 1 & output port: PC0, PC1, and PC2 pins are used.
MODE 1
B PC0 PC1 PC2 PC3 PC4 PC5 PC6 PC7 A
8 I/O 8 I/O
PB7-PB0 PA7-PA0
OUTPUT
PORT A, PORT B CONTROL HANDSHAKING
SIGNAL
ELEG 3230 - Part 9 20
8255A Mode 2 Operation
a Only port A can be initialized in mode 2.
a In mode 2, port A can be used as bi-directional handshake data
transfer.
a In this mode, PC3-PC7 are used for handshake lines.
a PC0-PC2 can be used as I/O pins if port B is set to mode 0.
a PC0-PC2 are used as handshake lines if port B is set to mode 1.
a Note that Mode 2 does not mean that it uses double handshake
I/O. It basically specify the bi-directional transmission of data flow.
Both Mode 1 and Mode 2 require handshaking, but no specification
which type is used.
8 I/O 8 I/O
PB7-PB0 PA7-PA0
PORT A CONTROL
PORT B may be MODE 0
(or MODE 1)
ELEG 3230 - Part 9 22
8255A Control Words
a How to set 8255A? – by control words
a Two control word formats are used:
(1) mode-set control word format : to set the modes of each port
(2) port C bit set/rest control word format : to set the particular bit in
port C. (such that 8255 can send a “1” or “0” via a particular bit to
other devices)
a These two formats can be differentiated by the MSB of the control
word.
D7 D6 D5 D4 D3 D2 D1 D0 GROUP B
PORT C (LOWER)
1 = INPUT
0 = OUTPUT
PORT B
1 = INPUT
0 = OUTPUT
MODE SELECTION
0 = MODE 0
1 = MODE 1
GROUP A
PORT C (UPPER)
1 = INPUT
0 = OUTPUT
PORT A
1 = INPUT
0 = OUTPUT
MODE SELECTION
00 = MODE 0
01 = MODE 1
1X = MODE 2
X X X BIT SELECT
DON’T CARES 0 1 2 3 4 5 6 7
0 1 0 1 0 1 0 1 B0
0 0 1 1 0 0 1 1 B1
0 0 0 0 1 1 1 1 B2
BIT SET/RESET
FLAG 0=FLAG
D7 D6 D5 D4 D3 D2 D1 D0
0 0 0 0 0 1 1 1
SET BIT
Select BIT # 3
MUST BE 0*
BIT SET/ RESET WORD
*:D6-D4 are set to 0 for simplicity and compatibility with future product.
Port A
CONTROL WORD
INTE A
PC6 ACK
(PC6) D7 D6 D5 D4 D3 D2 D1 D0
PC7 OBF 1 0 1 0 1/0
2 1
PC4+5
1 = INPUT
3
PC3 INTR 0 = OUTPUT
PC4+5 I/O
Internal structure
ELEG 3230 - Part 9 28
Strobed Output Operation
of 8255A (Mode 1 Port B)
Mode 1 Port B
Port B
INTE B
PC2 ACK
(PC2) CONTROL WORD
5 4
PC1 OBF D7 D6 D5 D4 D3 D2 D1 D0
1 1 0
6
PC0 INTR
Internal structure
ELEG 3230 - Part 9 29
Signal Definition of Mode 1
Strobed Output
a Whenever data are written to a port programmed as a strobed output
port, the OBF signal becomes a logic 0 to indicate that data are present
in the port latch (buffer) and is ready for external device to access. The
external device strobes the ACK signal to indicate it has received the
data. The ACK returns the OBF to logic 1.
a OBF an output that goes low whenever data are output (using OUT)
from CPU to the port A or port B latch; a strobe signal.
a ACK an Acknowledge signal that causes OBF pin to return to a logic
1. This signal is a response from external device to indicate it
has received data from 8255 port.
a INTR a signal that is often used to interrupts the processor when the
external device receives the data and sends back the ACK signal.
a INTE an internal bit programmed to enable or disable the INTR pin.
INTE A is programmed as PC6 (for output mode) or PC4 (for
input mode) and INTEB is PC2 (for both input/output mode).
(Also check 8255 data sheet in course web site http://course.ie.cuhk.edu.hk/~ieg3810/msm82c55a-2rs-gs-vjs.pdf )
ELEG 3230 - Part 9 30
Strobed Output Operation
(Mode 1) of 8255A
WR
ACK
Port
8255A printer
PB0 D0
ASCII
PB7 D7
ACK
PC2 ACK
BIT1 EQU 2
PRINT PROC NEAR
Jump if ;check for printer ready
Z=1 or Check if printer has
IN AL,PORTC ;get OBF
AL AND BIT1=0 or accessed the previous
TEST AL,BIT1 ;test OBF
OBF=0 data in the port buffer
JZ PRINT ;jump if OBF = 0
;send character to printer via port B
MOV AL,AH ;get character
OUT PORTB,AL ;send it
;send DS to printer
MOV AL,8 ;clear DS (data strobe pin on printer)
OUT COMMAND,AL ; 8=00001000B -> reset PC4 Check mode set
MOV AL,9 ;set DS control word format
OUT COMMAND,AL ; 9=00001001B -> set PC4
RET
PRINT ENDP
ELEG 3230 - Part 9 33
Strobed Input Operation
of 8255A (Mode 1 Port A)
Mode 1 Port A For example when inputting data from keyboard.
Port A
INTE A CONTROL WORD
PC4 STB
(PC4)
D7 D6 D5 D4 D3 D2 D1 D0
PC5 IBF
2 1 1 0 1 1 1/0
For PC6+7
3 1 = INPUT
PC3 INTR
0 = OUTPUT
PC6+7 I/O
Internal Structure
ELEG 3230 - Part 9 34
Strobed Input Operation
of 8255A (Mode 1 Port B)
Mode 1 Port B
Port B
INTE B
CONTROL WORD
PC2 STB
(PC2)
PC1 D7 D6 D5 D4 D3 D2 D1 D0
5 4 IBF
1 1 1
6
PC0 INTR
Internal Structure
ELEG 3230 - Part 9 35
Strobed Input Operation
(Mode 1) of 8255A
Level 0 trigger Port Latch
STB
IBF
(buffer full)
INTR
(Interrupt requested)
RD
Port
Timing diagram
Data flow: external device → port buffer → CPU
ELEG 3230 - Part 9 36
Example Procedure of
keyboard encoder reading
;procedure that reads the keyboard encoder and returns with the ASCII character in AL
=0020 BIT5 EQU 20H
=0022 PORTC EQU 22H
=0020 PORTA EQU 20H
Junp if Z=1 or
0000 E4 22 AL AND BIT5=0 IN AL,PORTC ;read port C
0002 A8 20 or IBF=0 TEST AL,BIT5 ;test IBF (PC5)
0004 74 FA JZ READ ;jump if IBF = 0
1 2 OBF
PC7
3 INTE
1 PC6 ACK
1 2 INTE
PC4 STB
2
PC5 IBF
Internal Structure
ELEG 3230 - Part 9 38
Mode 2 Operation of 8255A
WR
OBF
INTR
ACK
STB
IBF
Port A
RD
Data stored in port A
Data read from port A
Data output (OUT) to port A
Data input (IN) from port A
Timing diagram
ELEG 3230 - Part 9 39
Example Procedure of output
through bidirectional bus data
;procedure that transmits AH through the bi-directional bus of port A
=0080 BIT7 EQU 80H
=0062 PORTC EQU 62H
=0060 PORTA EQU 60H
D7 D6 D5 D4 D3 D2 D1 D0
I/O I/O IBFA INTEA INTRA INTEB IBFB INTRB
STB STB
OUTPUT PORT OUTPUT PORT
Keyswitch Types
(a) Membrane
Q:
Assume key “9” is pressed.
+5V
For the following cases,
OUTPUT C D E F
PORT 01 what are the values of D3-
Y D0 D0 at Port 2?
8 9 A B
X
(1). Port 1, D3-D0=0000
D1
4 5 6 7
(2). Port 1, D3-D0=1110
O
row
D5
O: Output D4
D3
D2 Q: How to detect if no key is pressed?
D1
D0 Q: How to detect which (one) key is
Port connections pressed?
ELEG 3230 - Part 9 44
Detecting a Matrix Keyboard
Keypress
Y
ENCODE
N Key Found?
Read Wait
Columns 20 ms DEBOUNCE
Y
Convert
Read
to HEX
N All Keys Y Columns
Open?
N Key Y RETURN
Pressed?
(D. Hall)
Flowchart for procedure
ELEG 3230 - Part 9 45
Assembly Program for Keybrd
Detect, Debounce and Encode
1 ;8086 Program F9-20.ASM
2 ;Abstract : Program scans and detect a 16 - switch keypad
3 ; It initializes the ports below and then calls a procedure
4 ; to input an 8 - bit value from a 16 - switch keypad and encode it.
5 ; Ports : SDK-86 board Port P1A (FFF9H) - output, P1B(FFFBH) - input
6 ; Procedures : Calls KEYBRD to scan and decode 16 - switch keypad
7 ; Registers : Uses CS, DS, SS, SP, AX, DX
8 Q: Why, e.g., 7Eh corresponds to “3”?
9 0000 DATA SEGMENT WORD PUBLIC
10 ; 0 1 2 3 4 5 6 7
11 0000 77 7B 7D 7E B7 BB BD BE TABLE DB 77H, 7BH, 7DH, 7EH, 0B7H, 0BBH, 0BDH, 0BEH
12
13 ; 8 9 A B C D E F
14 0008 D7 DB DD DE E7 EB ED + DB 0D7H, 0DBH, 0DDH, 0DEH, 0E7H, 0EBH, 0EDH, 0EEH
15 EE
16 0010 DATA ENDS
17 0000 STACK_SEG SEGMENT
18 0000 1E*(0000) DW 30 DUP(0) ;set up stack of 30 words with value 0
19 TOP_STACK LABEL WORD ;pointer to top of stack
20 003C STACK_SEG ENDS
+5V
MAN 7
160 Ω EACH
13 12 11 10 9 15 14 +6V
a b c d e f g
16 5
+5V Vcc BI
4
8 RBI
GND 7447 3
LT
A B C D
7 1 2 6
BCD INPUTS
Q: Only one 7447 for all 7 digits. → display the same values for all?
A: time-multiplexing
ELEG 3230 - Part 9 54
8254 Software-Programmable
Timer/Counter
a 8254 is very versatile and can be used in many applications.
a There are several modes of operation for different applications.
a Intel 8253 and 8254 are almost pin-to-pin compatible except
` The maximum input clock frequency for 8253 and 8254 is 2.6 MHz
and 8 MHz, respectively. (10 MHz for 8254-2)
` 8254 has a read-back feature which allows you to latch the count in all
the counters and the status of the counter at any point. 8253 does not
have this feature.
a 8254 contains three 16-bit counters. The counter can be
programmed to load the initial count, start and stop the count.
a 8254 has an 8-bit interface to data bus, and two address input A0
and A1 to address each of the three counters.
RD
INTERNAL BUS
WR READ/ CLK 1
WRITE COUNTER GATE 1
A0 1
A1 LOGIC OUT 1
CS
CLK 2
CONTROL COUNTER
WORD GATE 2
2
REGISTER OUT 2
D7 1 24 Vcc
D6 2 23 WR
D5 3 22 RD
D4 4 21 CS
D3 5 20 A1
D2 6 19 A0
8254
D1 7 18 CLK2
D0 8 17 OUT2
CLK 0 9 16 GATE2
OUT 0 10 15 CLK1
GATE 0 11 14 GATE1
GND 12 13 OUT1
CONTROL STATUS
WORD LATCH
REGISTER
CRM CRL
STATUS
REGISTER
OL M OL L
GATE n
CLK n OUT n
A1 A0 SELECTS
0 0 COUNTER 0
Internal 0 1 COUNTER 1
1 0 COUNRER 2
1 1 CONTROL WORD REGISTER
*Note : This system address is for the example in D. Hall Fig 8-14, and may vary for
different circuit. In this example, 8254’s A1 and A0 are connected to CPU’s address pin
A2 and A1. Also check 784LS138 connections.
ELEG 3230 - Part 9 60
Example of 8254 Circuit
74LS138
Q: Find the system port address for the three counters and control register.
ELEG 3230 - Part 9 61
Address Decoding via LS138
a To activate the output Y4 of LS138 in the previous example, G1 shall be high
and G2(A&B) shall be low.
a From the previous figure, we note that
G1=~A7*~A6*~A5
~G2A=~(A15*…*A8)
~G2B=M/~IO
D7 D6 D5 D4 D3 D2 D1 D0
SC1 SC0 RW1 RW0 M2 M1 M0 BCD
SC1 SC0
0 0 Select Counter 0
0 1 Select Counter 1
1 0 Select Counter 2
1 1 Read-Back Command
(see Read Operations)
(4) BCD
0 Binary Counter 16-bits
1 Binary Coded Decimal (BCD) Counter
(4 Decades)
M2 M1 M0
0 0 0 Mode 0 - Interrupt on Terminal Count
0 0 1 Mode 1 - Hardware One-Shot
× 1 0 Mode 2 - Pulse Generator
× 1 1 Mode 3 - Square Wave Generator
1 0 0 Mode 4 - Software Triggered Strobe
1 0 1 Mode 5 - Hardware Triggered Strobe
NOTE:
Don’t Care bits (×) should be 0 to insure
compatibility with future Intel products
Example 3 A1 A0 Example 4 A1 A0
Control Word - Counter 0 1 1 Control Word - Counter 1 1 1
Control Word - Counter 1 1 1 Control Word - Counter 0 1 1
Control Word - Counter 2 1 1 LSB of Count - Counter 1 0 1
LSB of Count - Counter 2 1 0 Control Word - Counter 2 1 1
LSB of Count - Counter 1 0 1 LSB of Count - Counter 0 0 0
LSB of Count - Counter 0 0 0 MSB of Count - Counter 1 0 1
MSB of Count - Counter 0 0 0 LSB of Count - Counter 2 1 0
MSB of Count - Counter 1 0 1 MSB of Count - Counter 0 0 0
MSB of Count - Counter 2 1 0 MSB of Count - Counter 2 1 0
NOTE: (1) In all four examples, all Counters are programmed to read/write two-byte counts.
(2) When programmed in 2-byte, the first ELEG
byte (LSB)
3230will- stop
Partthe
9 count. The second byte (MSB) will 66
start the counter with the new count.
Example of a control word
Task: Use counter 0 of the 8254 to divide a clock signal at 2.45 MHz to
78.6 kHz (→1/32).
(Brey Fig10-35)
MODE 0
ELEG 3230 - Part 9 MODE 1 71
8254 Example Timing Diagrams
MODE 4 MODE 5
(software-triggered strobe) (hardware-triggered strobe)
ELEG 3230 - Part 9 73
Gate Pin Operations Summary
Signal
Low or Going
Status Rising High
Low
Modes
0 Disables Counting ----------- Enables Counting
1 ----------- 1) Initializes Counting -----------
2) Resets Output after
Next Clock
2 1) Disables Counting Initializes Counting Enables Counting
2) Sets Output
Immediately High
3 1) Disables Counting Initializes Counting Enables Counting
2) Sets Output
Immediately High
4 Disables Counting ----------- Enables Counting
5 ----------- Initializes Counting -----------
ELEG 3230 - Part 9 74
Minimum and Maximum Initial Counts
NOTE:
0 is equivalent to 216 (0FFFFh+1) for binary counting and 104 (9999+1)
for BCD counting
A1A0 = 11 CS = 0 RD = 1 WR = 0
D7 D6 D5 D4 D3 D2 D1 D0
1 1 COUNT STATUS CNT 2 CNT 1 CNT 0 0
D7 D6 D5 D4 D3 D2 D1 D0
OUTPUT NULL COUNT RW1 RW0 M2 M1 M0 BCD
D7 1 = OUT Pin is 1
0 = OUT Pin is 0
D6 1 = NULL Count
0 = Count available for reading
a After the counter register or status register is latched, the content can be
read by instruction
IN AL, counter_n_address
a If both the count and status of a counter are latched, the first read
operation of that counter will return the latched status, regardless of
what was latched first. The next one or two read will return the latched
count.
a Subsequent read will return unlatched count.