Академический Документы
Профессиональный Документы
Культура Документы
INTRODUCTION:
A microprocessor is great at solving problems, but if it cant communicate with the outside world, it is a little worth. This module outlines some of basic methods of communication both serial and parallel, between human and microprocessor. The following topics are covered here :
1. Serial and parallel port 2. Application of serial and parallel ports(RS232 etc)
LEARNING OBJECTIVES:
The objectives of this topic are to:
1. Introduce the operation of the basic input output device. 2. Define handshaking and explain how to it with many I/O devices. 3. Interface and program the 8255 programmable parallel interface component.
LEARNING OUTCOMES:
After completed this module trainees should be able to :
1. Explain the operation input/output device. 2. Interface and program the 8255 programmable parallel interface component. 3. Interface and program 8251 UART 4. Know the interfacing port in microcomputer 5. Describe connectors and port in computer system
KKTMPJ
174
INTRODUCTION TO INTERFACING
Introduction
The 8255 programmable peripheral interface (PPI) is a very popular low-cost interfacing component found in many applications. The PPI has 24 pins for I/O, programmable in groups of 12 pins, that are used in three separate modes of operation. The 8255 is used for interface to the keyboard and the parallel printer port in many of these personal computers. It also controls the timer and reads data from the keyboard interface.
KKTMPJ
175
INTRODUCTION TO INTERFACING
A1 0 0 1 1
A0 0 1 0 1
The 8255 is fairly simple device to interface to the microprocessor and program. For the 8255 to be read or written, the CS input must be a logic 0 and the correct I/O address must be applied to the A1 and A0 pins. The remaining port address pins are dont cares as far as the 8255 is concerned, and are externally decoded to select the 8255.
The nicest feature of the 8255 is that different hardware circuits can be connected to ports A, B and C, with the direction (input or output) of each port configured with initial programming. This allows an 8088-based system with an 8255 in it to be used for many different purposes.
In this mode, port A, B and C can be individually programmed as input or output ports. Port C is divided into two 4-bit halves, directionally independent from each other So, there are 16 combinations of input and output configurations available with this mode. A RESET automatically causes the 8255 to enter mode 0 with all ports programmed for input.
To program the 8255 for mode 0 operation and set the direction of each port, a mode word must be output to control port. The definition of the mode word is shown in Figure 5.2. The MSB is the mode-set flag, which must be a 1 to program the 8255. Bits 5 and 6 are used to select the 8255s mode. 00 selects mode 0, 01 selects mode 1, and mode 2 is selected when bit 6 is high. Bit 2 is also used as a select bit for modes 0 and 1. The other 4 bits set the direction of ports A and B and both halves of C. A0 indicates an output port and a 1 indicates an input port.
KKTMPJ 176
INTRODUCTION TO INTERFACING
To configure the 8255 for mode 0, all ports programmed for input, the mode word must be 10011011 (9BH). This byte must be output to the control port to configure the 8255. The following two instructions will initialize the 8255 after a reset:
Remember that the 8255 of Figure 5.0 has its control port at address A 3 . Assume that the 8255 has a DIP switch wired to port A and a set of LEDs wired to port B. The following code can be used to repeatedly read the switches and send their states to the LEDs.
READ :
; configure 8255 for A in , B out , mode 0 ; read switches ;send data to lights
GET :
KKTMPJ
177
INTRODUCTION TO INTERFACING
In this mode, the 8255 uses port C as a handshaking port. Handshaking signals are commonly used in printers to sense the status of the paper-out sensor and the printers readiness to accept new data. Ports A and B can be programmed for input and output. Data are latched in both directions. If port A is programmed for input, a strobe signal is needed on PC 4 to write data into port A. The 8255 will acknowledge the new input data by outputting a 1 on PC 5 . These two signals on port C are defined as shown in Figure 5.3(a). PC5 is IBF a , input buffer A full. IBF is cleared when the processor reads port A. Port B operates in the same way, using PC 2 and PC1 as handshaking signals. Both ports have the capability of causing an interrupt when data are strobed into them. The INTR output will go high when IBF goes high and the internal interrupt-enable bit is set. PC4 will cause INTR a to go high when data are strobed into port A. Reading the input port will clear the interrupt request. This interrupt mechanism is a useful alternative to using software to constantly poll the input port. Polling wastes a lot of time waiting for input data that may not be there. Interrupting the processor only when new data have arrived results in more efficient program execution. This is one of the advantages of modes 1. As Figure 5.3(a) shows, PC 6 and PC 7 are available for general purpose I/O when port A is programmed for input. The mode word needed to program the 8255 for mode 1, Ain, Bin, and PC 6 and PC 7 out is B6. Figure 5.3(b) shows how the 8255 is configured for output in mode 1. It has output buffer full (OBF) and a acknowledge (ACK) signals used to handshake with the output circuitry. OBF will go low when the processor writes to port A or B. This signal will remain low until a low pulse arrives on ACK. ACK is used to indicate that the new output data were received. ACK, together with interrupt-enable, can be used to generate an interrupt with INTR. This would interrupt the processor when the new output data have been read, and avoid the need to poll the ACK signal. To program the 8255 for mode 1, Aout, and Bout, use mode word A 4 .
KKTMPJ
178
INTRODUCTION TO INTERFACING
This mode allows port A to operate as an 8-bit bidirectional bus. This is needed to allow the 8255 to be interfaced with 8-bit peripherals such as UARTS, which require a bidirectional data bus. Bits in port C are again used for handshaking and general purpose I/O, as indicated by Figure 4.4. Port B can operate as an input port or output port in mode 0 or mode 1. When operating port B in mode 0 ( with port A in mode 2), PC 0 through PC 2 are available for general purpose I/O. The definitions for PC0 through PC 2 in mode 1 apply when port B is operated in mode 1 with port A in mode 2. As before, the INTR output can be used to interrupt the processor for input or output operations. When the CPU writes data to port A, OBF will go low. To enable the output buffer on port A and read the data, ACK must be pulled low. Data are written into port A by pulling STB low. This will cause IBF to go high until the data are read by the processor.
KKTMPJ
179
INTRODUCTION TO INTERFACING
KKTMPJ
180
INTRODUCTION TO INTERFACING
The control functions are selected when A 0 is high (port 79H). Reading form port 79H gets 1 byte of status information; writing to it selects functions such as data size and used when the 8251 is connected to a modem. They are grounded to keep them enable. TxC and RxC are the transmitter and receiver clocks. The frequency of the TTL signal at these inputs determines the bit rate and time of the transmitter and receiver. It is common to run the UART at a clock speed 16 times greater than the baud rate. So, a 2400 baud transmission rate requires a 38.4 kHz clock (multiply 2400 by 16). This frequency and other standard baud rate frequencies are generated automatically by the 14,411 baud-rate generator. All that is needed is a 1.8432 MHz crystal. Attempting to transmit a digital (0-5 V) signal over a long length of wire causes distortion in the signal shape due to the line capacitance. It was discovered that making
KKTMPJ 181
INTRODUCTION TO INTERFACING
the signal switch form a positive voltage to a negative voltage helps to eliminate the distortion. Higher baud rates are possible using the +/-swigging signal. A standard was developed for this type for signal, called the RS232C standard. Take another look at the waveform in Figure 5.4. This is the TTL waveform that comes out of UARTs transmitter. The RS232 waveform that gets transmitted over the wires is inverted and swings plus and minus. So, a high level on the TTL waveform creates a low (negative) level on the An integrated circuit capable of performing the RS232C-to-TTL
RS232Cwaveform.
conversions is the MAX23CPE. This chips, such as the 1488 line driver and 1489 line receiver, required additional external power supplies. The MAX232 has two separate RS232C drivers/receivers for systems requiring two serial data channels.
KKTMPJ
182
INTRODUCTION TO INTERFACING
Figure 5.6 : 8251 mode instruction format The number of data bits used in a transmission is selected by bits 2 and 3. To enable generation of a parity bit, bit 4 must be set. Odd or even parity is chosen by the setting of bit 5. Finally, the number of stop bits is chosen by the upper 2 bits in the mode instruction. The waveform of Figure 5.4 contained 7 data bits, an even parity bit, and two stop bits. The required mode instruction byte is FA. To program the 8251, use:
Because an X16 clock was selected, the 8251 will operate in asynchronous mode. Synchronous mode is used for high-speed data transmission (not usually needed for communication with a serial display terminal). Synchronous mode is selected by making the lower two mode instruction bits. In this case, the upper two mode instruction bits do
KKTMPJ
183
INTRODUCTION TO INTERFACING
not set the number of stop bits, but rather the number of sync characters transmitted and the function is the SYNDET pin.
A second byte must be output to the control port to complete the initialization of the 8251. This byte is called the command instruction. The bits are assigned as shown in Figure 5.7; they have the following meanings.
Bit 0 :
Bit 1 :
transmit enable. Enable transmitter when this bit is set. data terminal ready. Setting this bit will will force the DTR output low. receive enable. Enable receiver when this bit is set send break character. Setting this bit forces TxD low error reset. Setting this bit clears the PE, OE, and FE error flags. request to send. Setting this bit forces the CTS output low. internal reset. To reset the 8251 and prepare for a new mode instruction this bit must be set.
: : : : :
Bit 6
internal reset. To reset the 8251 and prepare for a new mode instruction, this bit must be set.
Bit 7
enter hunt mode. Setting this bit enables a search for SYNC characters (in synchronous mode only).
The command instruction needed to enable the transmitter and receiver and ignore all other functions is 05H. This byte must be output to the control port after the mode
instruction. So, to totally initialize the 8251 for operation in the circuit of Figure 5.5, we need these instructions:
; mode instruction
; command instruction
184
INTRODUCTION TO INTERFACING
Once the UART has been programmed we have no need for the control port. Instead, we use the 82512 status port to help control the way data are transmitted and received. Figure 5.8 shows the bit assignments in the 8251s status port. Particularly important are the TxRDY (transmitter ready) and RxRDY (receiver ready) flags. They tell us when the transmitter is ready to transmit a new character and when the receiver has received a complete character. A number of error bits are included to show what may have given wrong with the last reception. PE is parity error, and will go high fi the parity of the received character is wrong. OE is overrun error, and will be set if a new characterize received before the processor read the last one. FE stands for framing error, and goes high when stop bits are not received in synchronous mode. DSR (data set ready) will go high whenever DSR is low.
The programmer must use the 8251s status bits to ensure proper serial data communication. Figure 5.9 shows how the first two bits are used to implement a simple serial input/output procedure. Both flowcharts indicate that repeated testing of the RxRDY/TxRDY bits may be necessary. For example, to show the importance of this repeated testing, consider the following case. Suppose that an 8251 is configured to transmit and receive data at 1200 baud, with 7 data bits, odd parity, and 1 stop bit. How long does it take to fully transmit or receive a character? At 1200 baud, the bit time is just over 833 us, and the selected word length of 10 bits makes the total time to receive or transmit a single character roughly 8.3 ms. It is not difficult to imagine how many instructions the 8088 might be able to execute in 8.3 us. Would a few thousand be unreasonable? Probably not. Therefore, we use the status bits to actually slow down the 8088, so that it does not try to send or receive data from the 8251 faster than the 8251 can handle. The two short routine is used to read a character from the receiver, returning it in AL. It is necessary to check the state of RxRDY before reading the receiver.
KKTMPJ 185
INTRODUCTION TO INTERFACING
CHARACTER INPUT
CHARIN RSTAT:
PROC IN
FAR AL, 79H ; ; ; ; ; read UART status examine RxRDY wait until receiver is ready read receiver ensure 7 bit ASCII code
KKTMPJ
186
INTRODUCTION TO INTERFACING
The data byte received is ANDed with 7F to clear the MSB. Because the UART is being used for character transmission, only a 7-bit ASCII code is required.
CHARACTER OUTPUT
This routine checks to see if the transmitter is ready for a new character. If it is, the ASCII character stored in AL is output to the transmitter.
CHAROUT TSTAT:
CHAROUT
; ; ; ; ;
save character read UART status wait until transmitter is ready get character back output to transmitter
KKTMPJ
187
INTRODUCTION TO INTERFACING
Essentially, serial ports provide a standard connector and protocol to let you attach devices, such as modems, to your computer., you will learn about the difference between a parallel port and a serial port, what each pin does and what flow control is.
The external connector for a serial port can be either 9 pins or 25 pins. Originally, the primary use of a serial port was to connect a modem to your computer. The pin assignments reflect that. Let's take a closer look at what happens at each pin when a modem is connected.
KKTMPJ
188
INTRODUCTION TO INTERFACING
Figure 5.11
9-pin connector:
1. Carrier Detect - Determines if the modem is connected to a working phone line. 2. Receive Data - Computer receives information sent from the modem. 3. Transmit Data - Computer sends information to the modem. 4. Data Terminal Ready - Computer tells the modem that it is ready to talk. 5. Signal Ground - Pin is grounded. 6. Data Set Ready - Modem tells the computer that it is ready to talk. 7. Request To Send - Computer asks the modem if it can send information. 8. Clear To Send - Modem tells the computer that it can send information. 9. Ring Indicator - Once a call has been placed, computer acknowledges signal (sent from modem) that a ring is detected.
KKTMPJ
189
INTRODUCTION TO INTERFACING
25-pin connector:
1. Not Used 2. Transmit Data - Computer sends information to the modem. 3. Receive Data - Computer receives information sent from the modem. 4. Request To Send - Computer asks the modem if it can send information. 5. Clear To Send - Modem tells the computer that it can send information. 6. Data Set Ready - Modem tells the computer that it is ready to talk. 7. Signal Ground - Pin is grounded. 8. Received Line Signal Detector - Determines if the modem is connected to a working phone line. 9. Not Used: Transmit Current Loop Return (+) 10. Not Used 11. Not Used: Transmit Current Loop Data (-) 12. Not Used 13. Not Used 14. Not Used 15. Not Used 16. Not Used 17. Not Used 18. Not Used: Receive Current Loop Data (+) 19. Not Used 20. Data Terminal Ready - Computer tells the modem that it is ready to talk. 21. Not Used 22. Ring Indicator - Once a call has been placed, computer acknowledges signal (sent from modem) that a ring is detected. 23. Not Used 24. Not Used 25. Not Used: Receive Current Loop Return (-) Voltage sent over the pins can be in one of two states, On or Off. On (binary value "1") means that the pin is transmitting a signal between -3 and -25 volts, while Off (binary value "0") means that it is transmitting a signal between +3 and +25 volts...
KKTMPJ
190
INTRODUCTION TO INTERFACING
5.2.2
uses the parallel port. While USB is becoming increasingly popular, the parallel port is still a commonly used interface for printers.
Figure 5.12 A typical parallel port on the back of your computer Parallel ports can be used to connect a host of popular computer peripherals:
Printers Scanners CD burners External hard drives Iomega Zip removable drives Network adapters Tape backup drives
Parallel port is a very commonly known port, widely used to connect the printer to the PC. If you see backside of your computer, there will be a port having 25 pins with a small symbol like this:. That port is known as LPT port or printer port. We can program this port for device control and data transfer. In this article, we will learn basics of parallel port and programming the parallel port.
KKTMPJ
191
INTRODUCTION TO INTERFACING
KKTMPJ
192
INTRODUCTION TO INTERFACING
Figure 5.13
In the PC there will be D-25 type of female connector having 25 pins and in the printer, there will be a 36-pin Centronics connector. Connecting cable will combine these connecter using following convention. Pin structure of D-25 and Centronics connecters are explained bellow.
KKTMPJ
193
Function Strobe Data Lines Acknowledgement Busy Out of Paper Select Auto feed Error Init Select In GND N/C
2 to 9
10 11 12 13 14 15, 32 16, 31 17, 36 18 to 30, 33 34, 35
Now let us know how communication between PC and printer takes place. Computer places the data in the data pins, then it makes the strobe low. When strobe goes low, printer understands that there is a valid data in data pins. Other pins are used to send controls to the printer and get status of the printer, you can understand them by the names assigned to the pins. To use the printer port for applications other than printing, We need to know how ports are organized. There are three registers associated with LPT port: Data register, Control register and Status register. Data register will hold the data of the data pins of the port. That means, if we store a byte of data to the data register, that data will be sent to the data pins of the port. Similarly control and status registers. The following table explains how these registers are associated with ports.
KKTMPJ
194
INTRODUCTION TO INTERFACING
nStrobe Data 0 Data 1 Data 2 Data 3 Data 4 Data 5 Data 6 Data 7 nAck Busy Paper-Out / Paper-End Select nAuto-Linefeed nError / nFault nInitialize nSelect-Printer/ nSelectIn/Out In Ground Gnd
Direction In/out In/Out In/Out In/Out In/Out In/Out In/Out In/Out In/Out In/Out In In In In In/Out In In/Out
Register.bit Control.0 Data.0 Data.1 Data.2 Data.3 Data.4 Data.5 Data.6 Data.7 Status.6 Status.7 Status.5 Status.4 Control.1 Status.3 Control.2 Control.3
Table 5.1: Pin directions and associated registers. * Pins with * symbol in this table are hardware inverted. Than means, If a pin has a 'low' ie. 0V, Corresponding bit in the register will have value 1. Signals with prefix 'n' are active low. That means, Normally these pins will have low value. When it needs to send some indication, it will become high. For example: Normally nStrobe will be high, when the data is placed in the port, computer makes that pin low. Normally, data, control and status registers will have following addresses. We need these addresses in programming later.
Register Data register (Base Address + 0) Status register (Base Address + 1) Control register (Base Address + 2) LPT1 0x378 0x379 0x37a LPT2 0x278 0x279 0x27a
KKTMPJ
195
INTRODUCTION TO INTERFACING
Note: All the parallel ports do not have bidirectional capability. Earlier parallel ports had only output enabled in data pins since printers only inputs data. But latter, to make parallel port capable of communicating with other devises, bidirectional ports are introduced. By default, data port is output port. To enable the bidirectional property of the port, we need to set the bit 5 of control register. To know the details of parallel ports available in your computer, follow this procedure: 1. Right click on My Computer, go to "Properties". 2. Select the tab Hardware, Click Device manager. 3. You will get a tree structure of devices; In that Expand "Ports(Com1 & LPT)". 4. Double Click on the ECP Printer Port(LPT1) or any other LPT port if available. 5. You will get details of LPT port. Make sure that "Use this Port (enable)" is selected. 6. Select tab recourses. In that you will get the address range of port. To start programming, you will need a D-25 type Male connector. Its pin structures can be found in the connector as follows:
KKTMPJ
196
INTRODUCTION TO INTERFACING
When we want to find out whether particular pin of the port is high or low, we need to input the value of corresponding register as a byte. In that, we have to find out whether the corresponding bit is high or low using bitwise operators. We can't access the pins individually. So, you need to know basic bitwise operations. Main bitwise operators that we need are bitwise AND '&' and bitwise OR '|'. To make a particular bit in a byte high without affecting other bits, write a byte with corresponding bit 1 and all other bits 0; OR it with original byte. Similarly, to make particular bit low, write a byte with corresponding bit 0 and all other bits 1; AND it with original byte. In Turbo C, there are following functions used for accessing the port:
outportb( PORTID, data); data = inportb( PORTID); outport( PORTID, data); data = inport( PORTID);
Outport() function sends a word to port, inport() reads a word from the port. outportb() sends a byte to port and inportb() reads a byte from the port. If you include DOS.H header, these functions will be considured as macro, otherwise as functions. Function inport() will return a word having lower byte as data at PORTID and higher byte as data at PORTID+2. So, we can use this function to read status and control registers together. inportb() function returns byte at PORTID. outport() writes the lower byte to PORTID and higher byte to PORTID+1. So this can be used to write data and control together. outportb() function write the data to PORTID. outport() and outportb() returns nothing. excessive draw of current from the port. Connect an LED in series with a resister of 1KW or 2.2KW between any of the data pins(2 to 9) and ground. With that, if you run the program given below, you should see the LED blinking with app. 1 sec frequency.
KKTMPJ
197
INTRODUCTION TO INTERFACING
3. How many mode is 8255 have ?. State the purpose each of them.
6. Determine the function of each no of 9 pins connector when a serial port was connect a modem to your computer .
KKTMPJ
198
INTRODUCTION TO INTERFACING
SUMMARY
In this unit we have studied : 1. The 82C55A is an LSI peripheral design to permits easy implementation of parallel I/O in the 8088 and 8086 microprocessor system .
2.
Two example of interfaces that require a synchronized data transfer are serial communication interface and a parallel printer interface.
3.
Two types of serial data communication are widely used in microprocessor system are asynchronous communications and synchronous communication.
2.
First the CS input must be a logic 0 and correct I/O address must be applied to the A1 and A0 pins. The remaining port address pins are dont cares as far as the 8255 is concerned , and are externally decoded to select the 8255.
3.
Mode 2 this mode allow port A to operate as an 8 bit bidirectional bus. This is needed to allow the 8255 to be interfaced with 8 bit peripheral such as UARTS , which require a bidirectional data bus.
KKTMPJ
199
INTRODUCTION TO INTERFACING
4.
RS 232C interface is a standard interface for implementing asynchronous serial data communication port on devices such as printers , CRT terminals , keyboards and modems.
5.
By synchronous , mean that the receiver and transmitter sections of the two pieces of equipment communicating with each other must ran synchronously. For this reason , the interface includes a clock line as well as transmit data , receive data and signal common lines. It is a clock signal that synchronizes both the transmission and reception of data. Meanwhile by asynchronous method of communications eliminates the need for the clock signal. The simplest form of an asynchronous communications
interface could consist of a Receive data , Transmit data , and Signal common communication lines. In this case , the data to be transmitted are sent out one character at a time, and the receiver examining synchronization bits that are included at the beginning and end of each character performs end of the communication line synchronization.
6.
9 pin connector :
1. Carrier Detect - Determines if the modem is connected to a working phone line. 2. Receive Data - Computer receives information sent from the modem. 3. Transmit Data - Computer sends information to the modem. 4. Data Terminal Ready - Computer tells the modem that it is ready to talk. 5. Signal Ground - Pin is grounded. 6. Data Set Ready - Modem tells the computer that it is ready to talk. 7. Request To Send - Computer asks the modem if it can send information. 8. Clear To Send - Modem tells the computer that it can send information. 9. Ring Indicator - Once a call has been placed, computer acknowledges signal (sent from modem) that a ring is detected.
KKTMPJ
200
INTRODUCTION TO INTERFACING
KKTMPJ
201