8086 I/O Interfacing & Programming - Part I: Vtu - Edusat Programme
8086 I/O Interfacing & Programming - Part I: Vtu - Edusat Programme
8086 I/O Interfacing & Programming - Part I: Vtu - Edusat Programme
Example: Write an ALP to toggle the bits of port address 300H continuously.
MOV DX, 300H
again: MOV AL, 55H
OUT DX, AL
MOV AL, 0AAH
OUT DX, AL
JMP BACK
The patterns 55 => 0101 0101 and AA=> 1010 1010 toggle the pins.
1
VTU – EDUSAT PROGRAMME
Example : Write an ALP to bring in 8-bit data from port address 302H; add contents of BL to it
and send the 16-bit sum to port address 45H.
MOV DX, 302H ;16-bit port address – use DX
IN AL, DX ;8-bit data into AL
MOV AH,00 ;initialize AH to hold carry
ADD AL, BL
JNC SKIP ;skip if no carry
INC AH
SKIP: OUT 45H, AX
; The last OUT instruction makes 16-bit sum (data) in AX –AHAL to be sent to port with 8-bit
address – 45H using direct addressing (no DX)
Overview of 8088 Input/Output Instructions & how to use the I/O Instructions are shown
in Fig. 1 below.
Summary :
In x86 system with 8-bit address bus A7-A0 for port addresses; the maximum number of
input ports = 256 (00 - FF) & the maximum number of output ports = 256
x86 can have maximum of 216 = 64K = 65,536 I/O ports
Instruction OUT 24H, AL – sends the data in AL register to an output port at 24H I/O
address
ALP to accept data from port 300H & send it out to port 304H
MOV DX, 300H
IN AL, DX
MOV DX, 304H
2
VTU – EDUSAT PROGRAMME
OUT DX, AL
Assembly language instructions to place status of port 60H in CH:
IN AL, 60H ; MOV CH, AL
Fig: Design for „OUT 99H, AL‟ with A7 – A0 : 1001 1001; IOW =0 for G
3
VTU – EDUSAT PROGRAMME
4
VTU – EDUSAT PROGRAMME
Fig: Input Port Design for ‘IN AL, 9FH’ 9F => A7 to A0 = 1001 1111
Fig: Input Port Design for ‘IN AL, 5FH’ => A7 to A0 at input to NAND gate is 5F – 0101 1111
5
VTU – EDUSAT PROGRAMME
Use instructions that access memory locations: Use IN & OUT instructions
MOV AL,[2000] – for i/p port IN AL, 30H
MOV [2010], AL – for accessing o/p port with memory address 2010 OUT DX, AL
Entire 20-bit address A19 –A0 is to be decoded. Requires DS to be Decode only 16/8 bit – A15- A0
loaded before
Number of ports can go upto as high as 220 = 1M ports Limited to 216 – 65,536 input &
65,536 output ports
6
VTU – EDUSAT PROGRAMME
7
VTU – EDUSAT PROGRAMME
Absolute Decoding
• Disadv : Aliases – same port with multiple addresses. Document the port addresses in the I/O
map thoroughly. Large gap in the I/O address map of the x86 PC, due to address aliases.
• Can be data acquisition boards – used to monitor analog signals such as temperature, pressure,
etc
• 62-pin section of the ISA expansion slot uses the following signals:
• Fig below shows latch connected to port address 300H of an x86 PC via an ISA expansion slot.
8
VTU – EDUSAT PROGRAMME
74LS138 is a group of NAND gates in a single chip. Each Y can control a single device. Much more
efficient than simple logic gates as shown under memory interfacing section.
A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
1 1 0 0 0 0 0 1 0 0
3 0 4
Introduction
To have more pins for I/O, the 8086 is interfaced to 8255 - a PPI (Parallel Peripheral Interface) –
pin diagram is shown in Fig.1. 8255 provides 3ports- Port A, B and C-each of 8 pins-totally 24
I/O pins. Each port can be dynamically changed as input or output in contrast to hardwired
74LS244 or 74LS373. But still the flexibility is limited. In 8255, the complete port A should be
configured as either input port or output port. Similarly port B and port C can be configured. A
little flexibility is available with port C, which is divided into 2 halves - upper and lower, ie.,
PCu and PCL which can be individually configured as either input or output.Also using the BSR
(Bit-Set and Reset Mode), individual port C pins can be set or reset irrespective of the other pins
(but this is only output).
9
VTU – EDUSAT PROGRAMME
10
VTU – EDUSAT PROGRAMME
11
VTU – EDUSAT PROGRAMME
Interfacing 8255 to 8086 Processor is as shown in Fig. 5 below. The interfacing circuit consists
of an address decoding circuitry along with A1A0 for selecting the THREE ports and Control
word Register.
12
VTU – EDUSAT PROGRAMME
Example 1: Develop a solution to Interface 8255 with 8086 µP for a base address of B800H.
Example 2: Find the control word if PA = OUT;PB = IN; PC0-PC3 = IN; PC4 – PC7 = OUT
1 0 0 0 0 0 1 1
8 3
13
VTU – EDUSAT PROGRAMME
Example 3: Program the 8255 to get data from Port A and send it to Port B. In addition, data
from PCL is sent to PCU. Use port addresses 300H to 303h for 8255 chip
Answer:CW : Control Word = 83h (worked out in example 2)
Port addresses: PA- 300H, PB = 301H; PC = 302H & CWR = 303H.
The Program is developed below.
PA EQU 300H ;send it to PB
PB EQU 301H MOV DX,PB
PC EQU 302H OUT DX, AL
CWR EQU 303H
CW EQU 83H ; get data from PCL
MOV DX, PC
;initialize CW IN AL, DX
MOV DX, CWR
MOV AL, CW ;lower nibble -PCL
OUT DX, AL AND AL,0FH
14
VTU – EDUSAT PROGRAMME
Example 4: 8255 shown in Fig. 7 is configured as:PA –I/P; PB & PC as O/P. Find control word,
find port addresses of PA, PB, PC & CWR. Also Program the ports to input data from PA &
send it to PB & PC.
Solution:The control word is developed in Table 3a & the I/O addresses of the port computed
from Table 3b.
1 0 0 1 0 0 0 0
9 0
Program the ports to input data from PA & send it to PB & PC
PA EQU 310H ; get data from PA
PB EQU 311H MOV DX, PA
PC EQU 312H IN AL, DX
CWR EQU 313H ;send it to PB & PC
CW EQU 90H MOV DX,PB
;initialize CW OUT DX, AL
MOV DX, CWR MOV DX,PC
MOV AL, CW OUT DX, AL
OUT DX, AL
15
VTU – EDUSAT PROGRAMME
Example 5: Write a program to toggle all the bits of PA continuously. Use INT 16H to exit if
there is a key press.
Solution: control word = 80H for all ports as output ports. Assume PA address is 300H; PB –
301H; PC-302H; CWR – 303H. Sending patterns 55h & AAh alternately on the port A, toggles
the bits. Delay program is to insert delay of 0.25s in between toggling the pins.
;initialize CW ;check for key press DELAY PROC NEAR
MOV DX, 303H MOV AH, 01 PUSH AX
MOV AL, 80H INT 16H ;INT 61H waits for 15.085µS
OUT DX, AL JZ AGAIN ;0.25s = 16592 x 15.085µS
Again: MOV CX, 16592
MOV DX,300H ; exit from pgm W1: IN AL,61H
MOV AL, 55H MOV AH, 4CH AND AL, 00010000B
OUT DX,AL INT 21H CMP AL, AH
CALL DELAY JE W1
MOV AL, 0AAH MOV AH, AL
OUT DX, AL POP AX
CALL DELAY RET
DELAY ENDP
16
VTU – EDUSAT PROGRAMME
To access I/O & hardware features in XPenvironment, one has to use the Windows Platform SDK
provided by Microsoft.In Windows 9x (95 & 98) environment,direct I/O addressing is available,
whileINT 21H & other system interrupt instructions are blocked.
To access I/O directly in Windows 9x, Visual C++ must be programmed in Console mode with a
different instruction syntax : has _ underscore
ALP has a distinction between 8 & 16 bit addresses; whereas in Visual C++ no such distinction.
Port# - any value between 0000 to FFFFH.
Example 6:Write a Visual C++ program for Windows 98 to toggle all bits of PA & PB of the 8255
chip. Use the kbhit function to exit if there is a key press
#include<conio.h> _outp(0x303,0x80); //80 to CWR
#include<stdio.h> do
#include<iostream.h> {
#include<iomanip.h> _outp(0x300,0x55); //PA=55
#include<windows.h> _outp(0x301, 0x55); //PB=55
void main() _sleep(500); //500ms delay
{ _outp(0x300,0xAA); //toggle bits
cout<<setiosflags(ios::unitbuf); _outp(0x301, 0xAA);
//When the unitbuf flag is set, the associated _sleep(500);
buffer is flushed after each insertion }
operation while(!kbhit());
cout<<“Pgm for toggling PA,PB” }
17
VTU – EDUSAT PROGRAMME
Example 7:Write a Visual C++ program for Windows 98 to get a byte of data from PA & send it
to both PB &PC of the 8255 chip in PC trainer
To clear the screen in Visual C++, utilize the code: system("CLS"); The standard library header file
<stdlib.h> is needed
18
VTU – EDUSAT PROGRAMME
Example 8: Write a C/C++ program for a PC with Linux OS to toggle all bits on PA & PB.
500ms delay. Key press to exit.
//main toggle loop n= getch();
do //if no key press in 1ms, n=0 due to halfdelay()
{ //display status on screen }
printf(“0x55\n\r”); while(n<=0);
refresh(); //update console //test for key press. If key press, exit program
outb(0x55, 0x300); endwin();
outb(0x55, 0x301); //close program console for ncurses
usleep(delay); //500ms=5e5µS return 0; //exit program
printf(“0xaa\n\r”); refresh(); }
outb(0xaa, 0x300);
outb(0xaa, 0x301);
usleep(delay); //500ms
Example 9:Write a C/C++ program for a PC with Linux OS to get abyte from PA & send it to
PB & PC.
#include<stdio.h> halfdelay(1);
#include<unistd.h> do { i = inb(0x300);
#include<sys/io.h> usleep(1e5); //100ms
#include<ncurses.h> outb(i,0x301);
int main() { outb(i,0x302);
int n=0; n=getch();
int i=0; } while(n<=0);
ioperm(0x300,4,0x300); endwin();
outb(0x90,0x303); //CW return(0);
initscr(); cbreak(); noecho(); }
19