Computer Interfacing InputOutput Interface ACOE 243 1 Introduction

  • Slides: 27
Download presentation
Computer Interfacing Input/Output Interface ACOE 243 1

Computer Interfacing Input/Output Interface ACOE 243 1

Introduction • In a typical computer system, the user communicates with the computer via

Introduction • In a typical computer system, the user communicates with the computer via standard peripheral devices such as the keyboard, mouse, display, printer e. t. c. • Furthermore computers and microprocessor based systems are used in instrumentation or automatic control applications. In such cases it is necessary that the microprocessor reads the state of input devices (switches, sensors) and activate some output devices (motors, heaters, lights). • The I/O interface is required to enable the interface between the microprocessor and the peripheral devices. • The peripheral devices are connected on a microprocessor system through the Input/Output ports. • The I/O interface provides the following: – Isolation between the buses and the peripheral devices. – Address decoding. – Synchronization/Latching. ACOE 243 2

Input/Output Instructions • The 8088 and 80 X 86 processors use the 16 lower

Input/Output Instructions • The 8088 and 80 X 86 processors use the 16 lower address lines (A 0 to A 15) to address I/O devices. This limits the maximum number of I/O ports to 64 K. • The IN instruction copies the content of an input port to register AL, AX or EAX. • The OUT instruction copies the content of register AL, AX, or EAX to an output port. • Register AL is used for 8 -bit ports, AX for 16 -bit ports and EAX for 32 -bit ports. • The 8088 and 80 X 86 processors support two I/O addressing modes. – Fixed Address. The address is directly specified in the instruction as an 8 -bit number. That is loaded on the address lines A 0 to A 7. Address lines A 8 to A 15 are set to 00. • IN AL, 30 H ; Read input port 0030 H into register AL • OUT 30 H, AL ; Copy register AL to output port 0030 H. – Variable Address. The address is specified through register DX as a 16 -bit address. • MOV DX, 3 A 0 H ; Set I/O address • IN AL, DX ; Read input port [DX] or 3 A 0 H into register AL. • OUT DX, AL ; Copy register AL to output port [DX] or 3 A 0 H ACOE 243 3

High Level Language Input/Output Instructions (C/C++) • C/C++ does not allow direct access to

High Level Language Input/Output Instructions (C/C++) • C/C++ does not allow direct access to I/O ports. This can be done by inserting assembly language code into the C/C++ code as follows: __asm ; code equivalent to ‘value: =port[portaddress]; { mov dx, portaddress ; specify the port address in al, dx ; input from the port specified by “dx” into “al” mov value, al } ; copy input data into variable ‘value’ __asm ; code equivalent to ‘port[portaddress] : = value; { mov dx, portaddress ; specify the port address mov al, value ; move output data into register “al” out dx, al ; output data to the port specified by “dx” } ACOE 243 4

Input/Output Using DLLs (C/C++) • For protection purposes, modern operating systems such as Windows

Input/Output Using DLLs (C/C++) • For protection purposes, modern operating systems such as Windows (after XP) do not allow the use of the IN and the OUT instructions in the users programs. • A way to have access to I/O ports is through libraries (DLL files) developed for this purpose. Such a library is the inout 32. dll • To use this library in Visual Studio (C++) we must: – Add the inout 32. lib file in the Linker properties • Project ->Properties->Linker->Input->Additional Dependencies – Define the two functions for input and output and use them accordingly short _stdcall Inp 32(short Port. Address); void _stdcall Out 32(short Port. Address, short data); main() { short Inval = Inp 32(889); /* input from port 889 into variable “Inval”*/ Out 32(888, 0 x 2 f); /* Output the hex value 2 f to port 888 */ } ACOE 243 5

Simple Input Port • An input port can be implemented using a simple octal

Simple Input Port • An input port can be implemented using a simple octal buffer such as the 74 LS 244. • The address decoding circuit enables the three-state buffers of the 74 LS 244 whenever the port address is selected by the processor. In such a case the state of the input devices appears on the data bus. • The address decoding circuit is enabled only if the IO/M signal is set High by the 8088 (Low for the x 86 processors) and the RD signal is Low. ACOE 243 Switch Open => Logic 1 Switch Closed => Logic 0 6

Simple Output Port (Using the 74 LS 373) • An output port can be

Simple Output Port (Using the 74 LS 373) • An output port can be implemented using a simple octal latch such as the 74 LS 373. • The address decoding circuit enables the D-latches of the 74 LS 373 whenever the port address is selected by the processor. In such a case the state of the data bus is latched to the output devices. • The address decoding circuit is enabled only if the IO/M signal is set High by the 8088 (Low for the x 86 processors) and the WR signal is Low. ACOE 243 Logic 1 => LED is OFF Logic 0 => LED is ON 7

Simple Output Port (Using the 74 HCT 573) • The 74 HCT 573 is

Simple Output Port (Using the 74 HCT 573) • The 74 HCT 573 is pin compatible with the 74 LS 373. There are three significant differences: Logic 1 => LED is OFF Logic 0 => LED is ON – The ‘ 573 has all inputs on the left side and all outputs on the right side of the IC, while the ‘ 373 has the even numbered inputs on the left side and the odd numbered inputs on the right side. Thus the ‘ 573 is easier to by handled on PCB boards. – The HCT 573 has a 20 m. A current sink and a 20 m. A current source, thus it can drive a LED connected either to the ground or to +5 V. The LS 373 has a 16 m. A current sink and a 400 u. A current source, thus it can only drive a LED connected to +5 V. Logic 1 => LED is ON Logic 0 => LED is OFF – The HCT family is faster than the LS family. ACOE 243 8

Simple I/O Example 1 • Three switches are connected on the input port 3

Simple I/O Example 1 • Three switches are connected on the input port 3 A 0 H and 8 LEDs on the output port 3 A 1 H as shown below. • Write a program to keep reading the state of the switches and switch ON the LED at the position corresponding to the binary number formed by the switches. (i. e. 3 X 8 decoder). ACOE 243 9

Simple I/O Example 1 (Software) Pseudo Code: Repeat Read Input port in In. Val

Simple I/O Example 1 (Software) Pseudo Code: Repeat Read Input port in In. Val Mask out D 2, D 1, D 0 from In. Val if In. Val = 0 then Out. Val = 1 if if In. Val = 1 then Out. Val = 2 if In. Val = 2 then Out. Val = 4 if In. Val = 3 then Out. Val = 8 if In. Val = 4 then Out. Val = 16 if if In. Val = 5 then Out. Val = 32 if In. Val = 6 then Out. Val = 64 if In. Val = 7 then Out. Val = 128 Write Out. Val to Output port Until keypressed ACOE 243 C/C++ Code main() { short In. Val, Out. Val = 0; do { In. Val = Inp 32(0 x 3 a 0); In. Val = In. Val & 0 x 7; if (In. Val == 0) Out. Val = 0 x 1; if (In. Val == 1) Out. Val = 0 x 2; if (In. Val == 2) Out. Val = 0 x 4; if (In. Val == 3) Out. Val = 0 x 8; if (In. Val == 4) Out. Val = 0 x 10; if (In. Val == 5) Out. Val = 0 x 20; if (In. Val == 6) Out. Val = 0 x 40; if (In. Val == 7) Out. Val = 0 x 80; Out 32(0 x 3 a 1, Out. Val); } while (!_kbhit()); } 10

Simple I/O Example 1 (Software using array as a lookup table) Pseudo Code: Table[0]

Simple I/O Example 1 (Software using array as a lookup table) Pseudo Code: Table[0] Table[1] Table[2] Table[3] Table[4] Table[5] Table[6] Table[7] = 1 /* = 2 /* = 4 /* = 8 /* = 16 /* = 32 /* = 64 /* = 128 /* C/C++ Code main() { short In. Val, Out. Val = 0; short Table[8] = {0 x 1, 0 x 2, 0 x 4, 0 x 8, 0 x 10, 0 x 20, 0 x 40, 0 x 80}; do { In. Val = Inp 32(0 x 3 a 0); Repeat Read Input port in In. Val Mask out D 2, D 1, D 0 from In. Val Get Out. Val from Table[In. Val] Write Out. Val to Output port Until keypressed ACOE 243 In. Val = In. Val & 0 x 7; Out. Val = Table[In. Val]; Out 32(0 x 460, Out. Val); } while (!_kbhit()); } 11

Simple I/O Example 1 b • Three switches are connected on the input port

Simple I/O Example 1 b • Three switches are connected on the input port 3 A 0 H and 8 LEDs on the output port 3 A 1 H as shown below. • Write a program to keep reading the state of the switches as a 3 -bit binary number, and switch ON the LEDs at the output port in such a way to form the binary value of the square of the binary number formed by the input switches. ACOE 243 12

Simple I/O Example 1 b (Software) Pseudo Code: Repeat Read Input port in In.

Simple I/O Example 1 b (Software) Pseudo Code: Repeat Read Input port in In. Val Mask out D 2, D 1, D 0 from In. Val if In. Val = 0 then Out. Val = 0 if if In. Val = 1 then Out. Val = 1 if In. Val = 2 then Out. Val = 4 if In. Val = 3 then Out. Val = 9 if In. Val = 4 then Out. Val = 16 if if In. Val = 5 then Out. Val = 25 if In. Val = 6 then Out. Val = 36 if In. Val = 7 then Out. Val = 49 Write Out. Val to Output port Until keypressed ACOE 243 C/C++ Code main() { short In. Val, Out. Val = 0; do { In. Val = Inp 32(0 x 3 a 0); In. Val = In. Val & 0 x 7; if (In. Val == 0) Out. Val = 0 x 0; if (In. Val == 1) Out. Val = 0 x 1; if (In. Val == 2) Out. Val = 0 x 4; if (In. Val == 3) Out. Val = 0 x 9; if (In. Val == 4) Out. Val = 0 x 10; if (In. Val == 5) Out. Val = 0 x 19; if (In. Val == 6) Out. Val = 0 x 24; if (In. Val == 7) Out. Val = 0 x 31; Out 32(0 x 3 a 1, Out. Val); } while (!_kbhit()); } 13

Simple I/O Example 1 b (Software using array as a lookup table) Pseudo Code:

Simple I/O Example 1 b (Software using array as a lookup table) Pseudo Code: Table[0] Table[1] Table[2] Table[3] Table[4] Table[5] Table[6] Table[7] = 0 /* = 1 /* = 4 /* = 9 /* = 16 /* = 26 /* = 36 /* = 49 /* C/C++ Code main() { short In. Val, Out. Val = 0; short Table[8] = {0 x 0, 0 x 1, 0 x 4, 0 x 9, 0 x 10, 0 x 19, 0 x 24, 0 x 31}; do { In. Val = Inp 32(0 x 3 a 0); Repeat Read Input port in In. Val Mask out D 2, D 1, D 0 from In. Val Get Out. Val from Table[In. Val] Write Out. Val to Output port Until keypressed ACOE 243 In. Val = In. Val & 0 x 7; Out. Val = Table[In. Val]; Out 32(0 x 460, Out. Val); } while (!_kbhit()); } 14

Simple I/O Example 2 Four float switches and four LEDs are used to indicate

Simple I/O Example 2 Four float switches and four LEDs are used to indicate the level of a liquid in the tank shown below. The switches and the LEDs are connected on a computer through an interface board with an input and an output port occupying the address 460 H to 46 FH. • Write a program to keep reading the state of the float switches and display the liquid level on the LEDs. If an error is detected then switch ON all LEDs. ACOE 243 15

Simple I/O Example 2 (Software using Case/Switch Statement) Pseudo Code: C/C++ Code Set Out.

Simple I/O Example 2 (Software using Case/Switch Statement) Pseudo Code: C/C++ Code Set Out. Val to 0, i. e switch off all leds main() { short In. Val, Out. Val = 0; Repeat do { Read Input port in In. Val = Inp 32(0 x 460); Mask out D 3, D 2, D 1, D 0 from In. Val = In. Val & 0 xf; Case of In. Val switch (In. Val) { 0: Out. Mask = 1 case 0: Out. Mask = 1; break; 1: Out. Mask = 2 case 1: Out. Mask = 2; break; 3: Out. Mask = 4 case 3: Out. Mask = 4; break; 7: Out. Mask = 8 case 7: Out. Mask = 8; break; FH: Out. Mask = 10 H case 0 xf: Out. Mask = 0 x 10; break; else Out. Mask = 1 FH default Out. Mask = 0 x 1 f; } Mask in D 4 to D 0 of Out. Mask in Out. Val Write Out. Val to Output port Until keypressed ACOE 243 Out. Val = (Out. Val & 0 xe 0) | Out. Mask; Out 32(0 x 460, Out. Val); } while (!_kbhit()); } 16

Simple I/O Example 2 (Software using array as a lookup table) C/C++ Code Pseudo

Simple I/O Example 2 (Software using array as a lookup table) C/C++ Code Pseudo Code: Table[0] = 00001 /* Table[1] = 00010 /* Table[3] = 00100 /* Table[7] = 01000 /* main() { short In. Val, Out. Mask, Out. Val = 0; short Table[16] = {0 x 1, 0 x 2, 0 x 1 f, 0 x 4, 0 x 1 f, 0 x 8, Table[15] = 10000 /* 0 x 1 f, Table[rest] = 111111 /* Repeat Read Input port in In. Val Mask out D 3, D 2, D 1, D 0 from In. Val Mask in D 4 to D 0 of Table[In. Val] to Out. Val Write Out. Val to Output port Until keypressed ACOE 243 0 x 1 f, 0 x 10}; do { In. Val = Inp 32(0 x 460); In. Val = In. Val & 0 xf; Out. Mask = Table[In. Val]; Out. Val = (Out. Val & 0 xe 0) | Out. Mask; Out 32(0 x 460, Out. Val); } while (!_kbhit()); } 17

7 -Segment Displays • Used to display the digits from 0 to 9, as

7 -Segment Displays • Used to display the digits from 0 to 9, as well as many letters (A, b, C, d, E, F, L, H) • Consists of seven leds connected to a common point • Can be common-anode or common-cathode Can be connected directly on an O/P port ACOE 243 OR Through a BCD/7 segment decoder 18

7 -segment Displays – Example 1 • A common cathode 7 -segment display is

7 -segment Displays – Example 1 • A common cathode 7 -segment display is connected on an o/p port occupying the address 3 A 1 h and four switches on an i/p port occupying the address 3 A 0 h. • Write a program to read the state of the switches and display the binary number formed by the switches as a hexadecimal digit. ACOE 243 19

7 -segment Displays – Example 1: Program D 7 D 6 D 5 D

7 -segment Displays – Example 1: Program D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 - g f e d c b a 0 X 0 1 1 1 3 F 1 X 0 0 0 1 1 0 0 0 C 2 X 1 1 1 0 76 3 X 1 0 1 1 0 5 E main() { short In. Val, Out. Val = 0; 4 X 1 0 0 1 1 0 1 4 D short Table[16] = {0 x 3 f, 0 x 0 c, 0 x 76, 0 x 5 e, 5 X 1 0 1 1 5 B 0 x 4 d, 0 x 5 b, 0 x 7 b, 0 x 0 e, 6 X 1 1 0 1 1 7 B 7 X 0 0 0 1 1 1 0 0 E 0 x 7 f, 0 x 4 f, 0 x 6 f, 0 x 79, 8 X 1 1 1 1 7 F 9 X 1 0 0 1 1 4 F A X 1 1 0 1 1 6 F In. Val = Inp 32(0 x 3 a 0); b X 1 1 0 0 1 79 In. Val = In. Val & 0 xf; C X 0 1 1 0 0 1 1 33 Out. Val = Table[In. Val]; d X 1 1 1 0 0 7 C E X 1 1 1 0 0 1 1 73 F X 1 1 0 0 0 1 1 63 ACOE 243 C/C++ Code 0 x 33, 0 x 7 c, 0 x 73, 0 x 63}; do { Out 32(0 x 3 a 1, Out. Val); } while (!_kbhit()); } 20

7 -Segment Displays: Example 2 Four float switches and a 7 -segment display are

7 -Segment Displays: Example 2 Four float switches and a 7 -segment display are used to indicate the level of a liquid in the tank shown below. The switches and the display are connected on a computer through an interface board with an input and an output port occupying the address 460 H to 46 FH. Write a program to keep reading the state of the float switches and display the liquid level on the display using the digits F(Full), H(High), A (Half), L(Low) and E(Empty). If an error is detected then switch ON the decimal point. ACOE 243 21

7 -Segment Example 2 (Software using array as a lookup table) D 7 D

7 -Segment Example 2 (Software using array as a lookup table) D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 - g f e d c b a F 0 1 1 0 0 0 1 1 63 H 0 1 1 5 B A 0 1 1 1 1 6 F L 0 0 1 1 0 0 0 1 31 E 0 1 1 1 0 0 1 1 73 dp 1 0 0 0 0 80 C/C++ Code main() { short In. Val, Out. Val = 0; short Table[16] = {0 x 73, 0 x 31, 0 x 80, 0 x 6 f, 0 x 80, 0 x 5 b, 0 x 80, 0 x 80, 0 x 63}; Inputs do { Output In. Val = Inp 32(0 x 460); N D 3 D 2 D 1 D 0 State Code 0 0 0 Empty (E) 73 In. Val = In. Val & 0 xf; 1 0 0 0 1 Low (L) 31 Out. Val = Table[In. Val]; 3 0 0 1 1 Half (A) 6 F 7 0 1 1 1 High (H) 5 B 15 1 1 Full (F) 63 Error (dp) 80 Otherwise ACOE 243 Out 32(0 x 460, Out. Val); } while (!_kbhit()); } 22

7 -segment Displays – Example 3 • A common cathode 7 -segment display is

7 -segment Displays – Example 3 • A common cathode 7 -segment display is connected on an o/p port occupying the address 3 A 1 h and four switches on an i/p port occupying the address 3 A 0 h. • Write a program to read the state of the switches and display on the 7 -segment display the letter: • • • H, if there are more switches closed than open, L, if there are less switches closed than open, E, if the number of switches closed is equal to the number of switches open. ACOE 243 D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 - f g b d e a c H 0 1 1 6 B L 0 1 0 0 0 1 1 0 46 E 0 1 1 1 0 76 23

7 -Segment Example 3 (Software using array as a lookup table) D 3 D

7 -Segment Example 3 (Software using array as a lookup table) D 3 D 2 D 1 D 0 0 0 L 46 1 0 0 0 1 L 46 2 0 0 1 0 L 46 3 0 0 1 1 E 76 4 0 1 0 0 L 46 5 0 1 E 76 6 0 1 1 0 E 76 7 0 1 1 1 H 6 B 8 1 0 0 0 L 46 9 1 0 0 1 E 76 A 1 0 E 76 b 1 0 1 1 H 6 B C 1 1 0 0 E 76 d 1 1 0 1 H 6 B E 1 1 1 0 H 6 B F 1 1 H 6 B ACOE 243 C/C++ Code O/P main() { short In. Val, Out. Val = 0; short Table[16] = {0 x 46, 0 x 76, 0 x 6 b, 0 x 46, 0 x 76, 0 x 6 b, 0 x 6 b}; do { In. Val = Inp 32(0 x 3 A 0); In. Val = In. Val & 0 xf; Out. Val = Table[In. Val]; Out 32(0 x 3 A 1, Out. Val); } while (!_kbhit()); } 24

Homework- Question 1 • Four switches are connected on the input port 3 A

Homework- Question 1 • Four switches are connected on the input port 3 A 0 H and 8 LEDs on the output port 3 A 1 H as shown below. • Write a program to read the binary number formed by the six switches and display the equivalent Binary Coded Decimal number on the Leds. For example if the switches form the number 100101, the displays should display the number 00110111, since (100101)2 = (00110111)BCD. ACOE 243 25

Homework- Question 2 • Four switches are connected on pins D 2, D 1

Homework- Question 2 • Four switches are connected on pins D 2, D 1 and D 0 of the input port 3 A 0 H and 8 LEDs on the output port 3 A 1 H. • Write a program to i. ii. read the code formed by the three switches, convert the input code into a binary value according to the table below and store it in an array called BINV, and iii. display it as a bar graph on the eight Leds as shown in figure below For example if the switches form the number 101 then the number 110 must be stored in the array BINV and the Leds should display 11111110. ACOE 243 26

Homework- Question 3 • Eight LEDs on the output port 3 A 1 H.

Homework- Question 3 • Eight LEDs on the output port 3 A 1 H. Write a program to generate the visual effect shown below, with a 500 ms delay between each step. Assume that a time delay function ‘msdelay(int x)’ is available that returns after x milliseconds. ACOE 243 27