Instrumentation Lab: Lab 3: The Parallel Port

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Instrumentation Lab

Lab 3: The Parallel Port

Purpose of the experiment


To become acquainted with the parallel port. Learn how to interface circuitry to the computer through this port.

The Parallel Port - 1

Background Information
In this lab, you will learn how to use the parallel port. This port acts as an interface between the external control and measurement circuitry and the internal software of the computer.

Bits and Bytes In science and engineering, the SI system is frequently used to describe the sizes of various quantities such as length, time, or mass. Each of these quantities has an associated fundamental SI unit, like the meter for length, or the second for time. While it is not an SI unit, the bit is the fundamental unit of information. The bit is the smallest possible unit of information, from which all other larger units of information can be built. It is a single true or false. In contrast, the byte is one of these larger units of information. It is a series of 8 bits. The computer processes information and therefore uses both bits and bytes in doing so. In a computer, bits are represented by voltages. Frequently, a voltage of approximately 0 V to about 2.5 V represents a bit whose value is false (logic 0). Conversely, voltages from about 2.5 V to 5 V represent a value of true (logic 1). This voltage standard is referred to as TTL logic levels.

The Parallel Port - 2

Memory Registers and Addresses

One basic circuit in modern digital computers is the memory register. A memory register is an electric circuit capable of storing a byte of information by maintaining voltages in the circuits conductors at 0 V (logic 0) or 5 V (logic 1). In this manner, a single memory register is capable of storing a binary number from 0 (0000 0000) to 255 (1111 1111). While it is possible to construct registers capable of storing decimal numbers (for example by making 10 V correspond to 2, 15 V to 3, etc), it is sufficient and easier just to deal in binary. The byte contained in a register can be manipulated in many ways. However, there are two data operations in which we are going to be interested: reading from a register and writing to a register. Reading the content of a register copies the byte that is stored there to the CPU, usually without altering those contents. Writing to a register places a new byte in that register, overwriting the previous byte. Most computer languages contain commands that allow reading from and writing to registers. In LabVIEW, these commands are the Advanced>>Memory>>In Port VI for reading and the Advanced>>Memory>>Out Port VI for writing. Reading and writing involve the transfer of a decimal number that represents the bit pattern associated with that register. For example, 151 decimal is 1001 0111 binary. So, if I wanted to store the byte 1001 0111 to a register, I would use the Out Port VI to write the decimal number 151 to that register. Reading works in an analogous way. Computers are made up of many such registers. Consequently, there is a system in place to specify which register will be read from or written to. This system is called addressing. Every register in the computer is given an address that distinguishes it from all other registers. This address is simply a number. In most textbooks or references, addresses are specified as a hexadecimal number, like 3BCh or 201h. However, most computer languages use ordinary decimal numbers to specify addresses. Of course, computers interpret the address into binary. Thus, reading from a register requires only the address of the register from which a byte is to be read. However, writing to a port requires two pieces of information: the address of the register and the byte to be stored at that address. Also, notice that registers

The Parallel Port - 3

have many different functions in a computer. Some registers are data registers (read and/or write capability) that contain information to be manipulated. Others are status registers (read-only capability) that only indicate the state of a particular circuit. Still others are control registers (write-only capability) that allow a user to change the state of a circuit. Consequently, either reading or writing to a particular register may or may not be allowed. In status and control registers, individual bits frequently have an agreed upon meaning. For example, in the printer status register at 379h, everybody agrees that when bit 5 is a logical 1 it means that the printer is out of paper. Thus, reading a value of 32 decimal (0001 0000, bit 5 is the 5th from the right) from address 379h means the printer is out of paper. In light of this, bits in a status register are indicators of some state of the computer, and bits in a control register are switches to toggle on and off certain features of the computer.

The Parallel Port - 4

Parallel Port The parallel port is most often used as an interface between the computer and a printer. As a result, the parallel port must be able to send information to the printer to be printed, as well as receive information from the printer such as printer status information or paper out errors. Parallel ports are universally supplied with PCs. This fact, coupled with the ports versatility, makes the parallel port indispensable for building instrumentation. While you can purchase additional parallel ports for a computer, there is usually at least one built into the motherboard. The built-in port is usually assigned to be LPT1 (first Line PrinTer device) and other parallel ports are assigned LPT2 and LPT3. Take note that on some computers, BIOS may assign these addresses to different LPTs. The resources usually used by each LPT port are listed in Table 3e.

Port Addresses LPT1 0378h* 037Fh (888d 895d) LPT2 0278h* 027Fh (632d 639d) LPT3 03BCh* 03C3h (956d - 963d) *Base Address

IRQ 7 5 7

DMA 3 3 3

Table 3a: Parallel Port Resources

On some computers, BIOS allows the port to operate in one of several different modes. While these modes use several registers to expand the capability of the port, we will only be interested in modes that use the lowest three addresses (base + 0 to base + 2) and allow for bi-directionality (i.e. data can flow both ways on the data pins). For the purposes of this lab, the computer will be preset to use one of these modes. The registers that are located at the addresses in Table 3e are listed below:

Address Base + 0 Base + 1 Base + 2

Description LPT Data Register LPT Status Register LPT Control Register

Register Type Data Status Control

Read/Write R/W R W

Table 3b: Parallel port registers.

The Parallel Port - 5

Notice that the registers in the table above are all of a different type. Since data can flow both ways on the parallel port, the LPT Data Register is both R/W. The LPT Status Register says whether or not voltages have been applied to the status pins (pins 10, 11, 12, 13 and 15, see below). Therefore, the LPT Status Register is R-only. The LPT Control Register allows the computer to set voltages on the control pins (pins 1, 14, 16 and 17, see below). While technically you can read from the LPT Control Register, it will only tell you the last number that the computer wrote there. It will not tell you whether or not voltages have been applied to the control pins. In fact, you should never impose external voltages to the control pins as this could damage the port. Table 3f summarizes the operations that can be performed with each of the registers of the parallel port. Notice that reading and writing are communications between the game port and the computer, and input and output are communications between the port and an external device or circuit. For a more complete listing of the registers, see the sections on the LPT Registers below.

Operation Write to Base + 0 Read from Base + 0 Write to Base + 1 Read from Base + 1 Write to Base + 2 Read from Base + 2 Input to Port Output from Port

Result Sets voltages on pins 2 to 9. Reads voltages imposed on pins 2 to 9. Not defined. Reads voltages imposed on pins 10 to 13, and 15. Sets voltages on pins 1, 14, 16, 17. Enables IRQ and bi-directionality. Not defined. (Does not tell you the voltages imposed on pins 1, 14, 16, 17.) Voltages can be applied on pins 2 to 13 and 15 to be read by the computer. Uses voltages that have been written to pins 2 to 9 to control an external circuit. Table 3c: Parallel port operations.

Notes Might only work when not in bi-directional mode. Works in bi-directional Mode only. Data written here is Ignored. Used to send status info from the printer to the CPU. Used to send control info from the CPU to printer. Voltages should not be imposed on pins 1, 14, 16, 17. Works in bi-directional mode only for pins 2 to 9. Used to send data to the printer.

The Parallel Port - 6

The parallel port connector is located at the back of the computer. The parallel port looks similar to the game port except that it has 25 holes instead of 15. Notice that the parallel port will be female. A male 25 pin port will be a serial port (see Lab 5).

Figure 3a: Female DB25 parallel port (left) and male DB25 cable (right) with pin numbers. Notice that the pins on the connecting cable are numbered as a mirror image of the holes. Each pin or hole has a particular function, and a list of these functions with the corresponding pin or hole numbers is called the pinouts of the port. The pinouts for the parallel port are listed in Table 3g.

Description Address/Bit Strobe. Inform printer that data on D0-7 is valid. Base + 2, Bit 0 Data Bit 0. Data bus. Base + 0, Bit 0 Data Bit 1. Data bus. Base + 0, Bit 1 Data Bit 2. Data bus. Base + 0, Bit 2 Data Bit 3. Data bus. Base + 0, Bit 3 Data Bit 4. Data bus. Base + 0, Bit 4 Data Bit 5. Data bus. Base + 0, Bit 5 Data Bit 6. Data bus. Base + 0, Bit 6 Data Bit 7. Data bus. Base + 0, Bit 7 Acknowledge. Notify computer that the printer Base + 1, Bit 6 is ready to receive the next set of data. 11 BUSY** Busy. Printer buffer full or printer busy. The Base + 1, Bit 7 computer needs to wait until this line is set high before transmitting again. 12 PO Paper Out. Printer is out of paper. Base + 1, Bit 5 13 SLCT OUT Select Out. Printer is on-line and ready. Base + 1, Bit 4 t 14 Auto Feed. Printer line feed. Base + 2, Bit 1 AUTO FEED 15 ERROR* Error. The printer has an error. Base + 1, Bit 3 16 INIT* Reset. Initialize the printer. Base + 2, Bit 2 17 Select In. Send a request to the printer. Base + 2, Bit 3 SLCT INt 18-25 GND Ground -*This pin is active when low. Other pins are active when high. **This pin is hardware inverted. +5 V applied to this pin is inverted by the port to be read as a logical 0 by the computer. t Both active when low and hardware inverted. Table 3d: Parallel port pinouts. The Parallel Port - 7

Pin 1 2 3 4 5 6 7 8 9 10

Name STROBEt D0 D1 D2 D3 D4 D5 D6 D7 ACK*

I/O O I/O I/O I/O I/O I/O I/O I/O I/O I I

I I O I O O --

There are three types of pins listed in the above table: data pins (input in bidirectional mode/output, I/O), status pins (input only, I) and control pins (output only, O). Status pins are used to allow the printer or other external circuit to signal the computer. Control pins are used to allow the computer to control the printer or an external circuit. Finally, data pins can be used to send or receive data, or may be used in the same manner as the status and control pins. It is important that external voltages are imposed only to data pins that are in bi-directional mode and to status pins. To prevent damage to the computer, these imposed voltages should not deviate much from 0 V to +5 V. Additionally, imposing external voltages, either to data pins that are not in bidirectional mode or to control pins, may result in damage to the computer chips. In some cases, imposing voltages to these pins can be done, but you must be very careful. We will discuss this in a later lab. In addition to voltage levels damaging the computer, you must also worry about how large currents are when connecting circuitry to the parallel port. There are two cases we are worried about. First, sourcing is when you let current flow out of the port through the data pins. Second, sinking is when you let current flow into the port through the data pins. Most ports can sink and source around 12 mA, although this can vary widely from computer to computer. To be safe, some sort of buffer or resistors should be used to keep the current drawn to or from the port from being too high. This does leave us with a problem: 12 mA is enough to light an LED, but not really enough current to power anything very large. Fortunately, 12 mA is enough to throw a small electrical switch called a relay, and a relay can control a circuit that does use a lot of current. We will discuss relays in a later lab. For now, we will only be dealing with small amounts of current.

The Parallel Port - 8

LPT Data Register

The data pins are monitored and controlled from the data register (base + 0) for a given port. Table 3h lists the meaning of each bit in this register.

Address

Base + 0 R/W LPT Data Register

Bit 7 6 5 4 3 2 1 0

Name/Description Data Bit 7. Data bus. Data Bit 6. Data bus. Data Bit 5. Data bus. Data Bit 4. Data bus. Data Bit 3. Data bus. Data Bit 2. Data bus. Data Bit 1. Data bus. Data Bit 0. Data bus.

Interpretation of Bit Value 1 = +5 V at Pin 9, 0 = 0 V at Pin 9 1 = +5 V at Pin 8, 0 = 0 V at Pin 8 1 = +5 V at Pin 7, 0 = 0 V at Pin 7 1 = +5 V at Pin 6, 0 = 0 V at Pin 6 1 = +5 V at Pin 5, 0 = 0 V at Pin 5 1 = +5 V at Pin 4, 0 = 0 V at Pin 4 1 = +5 V at Pin 3, 0 = 0 V at Pin 3 1 = +5 V at Pin 2, 0 = 0 V at Pin 2

Pin 9 8 7 6 5 4 3 2

Table 3e: LPT Data Register.

For most recent ports, this register can be R/W if set to be bi-directional in BIOS. When bi-directionality is off, this register will be W-only and data can only flow out of the port. However, when bi-directionality is on, some registers will be R-only and others will be R/W (see LPT Control Register below for details on setting bi-directionality).

The Parallel Port - 9

LPT Status Register

This port allows the printer to send status information to the computer. The printer uses pins 10, 11, 12, 13 and 15 to inform the computer of its status. Therefore, the status register is a read only register to monitor the states of these pins. It is important to note that the meanings of these pins are arbitrary. That is, everybody who programs for printers agrees that these pins and bits have the meanings listed below. However, if you are programming for a device other than a printer, you can use these pins for whatever function you wish, only the voltages matter. This will be the case in several later labs. The standard functions of these pins and bits are listed in the table below.

Address Base + 1 R-Only LPT Status Register

Bit 7 6 5 4 3 2 1 0

Name/Description BUSY ACK PO SLCT OUT ERROR IRQ Not defined. Time Out.

Interpretation of Bit Value 1 = Printer accepting data, 0 = Printer busy or full 1 = Not ready, 0 = Printer ready for next data 1 = Printer out of paper, 0 = Printer has paper 1 = Printer on-line, 0 = Printer off-line 1 = No errors, 0 = Printer Error 1 = No interrupt, 0 = Interrupt has occurred.

Pin 11 10 12 13 15

Table 3f: LPT Status Register

A description of each status bit follows: Status(7): Printer busy. When this bit is high (logic 1), the printer is ready to accept data. When it is low (logic 0), the printer is busy and is not accepting data. The computer must wait until this bit returns to logic 1 before sending any more data. Status(6): Line Printer Acknowledge. After a successful data transfer, this input line goes low. Status(5): Paper Out. When this bit is high (logic 1) the printer is signaling the computer that it is out of paper. When this bit is low (logic 0) the printer indicates that it has paper. Status(4): Select Out. Informs the computer that the printer is online (logic 1) or offline (logic 0).

The Parallel Port - 10

Status(3): Error. This line goes to logic 0 when there is a printer error. Status(2): Interrupt Request. Status(1): Not defined. Status(0): Time Out.

The Parallel Port - 11

LPT Control Register

The control register is a write only register. In some sense, it is the reverse of the status register, allowing the computer to send messages to the printer. It allows the computer to set the bi-directionality of the parallel port, and control the states of pins 1, 14, 16 and 17 (thereby sending messages to the printer).

Bit Name/Description Interpretation of Bit Value 7 Reserved. Base + 2 6 Reserved. W-Only 5 Bi-directional select. 1 = Port set to input and output*, 0 = Output only LPT 4 IRQ Enable 1 = IRQ enable for ACK, 0 = IRQ disable for ACK Control 3 SLCT IN 1 = No request to printer, 0 = Send request to printer Register 2 INIT 1 = No reset, 0 = Reset printer 1 AUTO FEED 1 = No line feed, 0 = Line feed the printer 0 STROBE 1 = Data at bus invalid, 0 = Data at bus valid *May set the port to input-only mode.

Address

Pin

17 16 14 1

Table 3g: LPT Control Register.

A description of each control bit follows: Control(7): Reserved. Control(6): Reserved. Control(5): As mentioned, many parallel ports can be set to be bi-directional. To do this, two things must happen. First, the port must be set to a bi-directional mode in BIOS. The modes available to you may vary from computer to computer. Second, the port must be set bi-directional by setting bit 5 at address base + 2 high. The behavior of the port when bit 5 is set low is almost universally output-only. When bit 5 is set high, some ports will be input-only and other ports will be simultaneously input and output. Exercise 3-3 shows a circuit to check the behavior of your port. Control(4): Interrupt Request Enable. Control(3): Select In. When low (logic 0), the computer sends a request to the printer.

The Parallel Port - 12

Control(2): Initialize. This line allows the computer to tell the printer to start its initialization routine. Control(1): Auto Feed. Allows the computer to tell the printer to continuously feed paper. Active when low. Control(0): Strobe. When this line goes low (logic 0), the computer tells the printer that the data at the parallel port is valid as written and is ready to be read.

The Parallel Port - 13

Prelab
You will need to do this prelab before coming to class. This will save you significant time in class.

Prelab 3-1: In your laboratory kit, you have a cable that connects the parallel port of the computer to a protoboard. This cable is a DB25 cable as shown in Figure 3a. Use your DMM to make certain that all the pins on the parallel port end of the cable have continuity through to the pins on the protoboard end of the cable. When you touch the DMM leads to a pin on one end of the cable to the corresponding pin on the other end you will get a small resistance reading. Using a small bit of masking tape, label the pins on the protoboard end with the numbers of the pins they connect to on the parallel port end.

Prelab 3-2: Repeat Prelab 3-1, this time with the game port cable supplied in your kit. The game port cable is a DB15 cable. For this lab, you just need to check pins 1 and 5. Save the other pins for Lab 4. Pins 1 and 5 are in the same places on the game port cable as they are on the parallel port cable.

The Parallel Port - 14

The Lab
The goal: To learn how to use the game and parallel ports to connect circuitry to the computer.

Warning: While putting high voltages or currents into the ports may not cause an immediate effect, over time it will burn out the port. Never short any of the pins directly to ground (i.e. always use resistors to limit your current). Also, turn off the circuit before leaving it alone for any period of time.

Exercises

Exercise 3-1: Build the following circuit. Make certain that the currents that the pins are sourcing are less than about 2 mA. If they are not, increase the resistors above 1 k.

Figure 3b: Exercise 3-7. Circuit for testing output to the parallel port. Then, write a program using the Advanced>>Memory>>Out Port VI to write a number from 0 to 15 to the parallel port. Check to see that the LEDs (light emitting diodes) light up to show the binary equivalent to the number that you wrote to the port. (Hint: If it doesnt seem to be working, turn some or all of your diodes around, or replace any diodes that seem to be burnt out.)

The Parallel Port - 15

Exercise 3-2: Heres a nice trick for setting particular bits to either on or off. Using the circuit in Exercise 3-1, write a program that makes sure a particular LED is always on regardless of the number you send to the port. This can be done with the boolean OR command. Repeat the exercise, but this time make sure a particular LED is always off no matter what. Use both the boolean OR and XOR commands to accomplish this.

Exercise 3-3: Build the following circuit to input voltages into the parallel port. Touching the game ports pin 1 to any of the parallel ports data pins will pull that data pin high, and using the game ports pin 5 will pull that data pin low. Make certain that the currents that the data pins are sinking are less than about 2 mA. Write a program to read the value coming into the port. You will need to set bit 5 high at address base + 2 to be sure that the port is in bi-directional mode.

Figure 3c: Exercise 3-3. Circuit for testing input to the parallel port.

The Parallel Port - 16

You might also like