One task that faces many engineers when evaluating a new device is finding a way to control its various functions. It's sometimes quite difficult to figure out how to read and write data to and from its registers. This can involve setting up data generators, logic analyzers, and other equipment. In order to be useful, the device must have the correct timing sequence.
Another solution is to use a dedicated digital I/O board. These are often quite expensive and may require special software. A good alternative is to use an I/O port that exists on virtually all PCs and laptopsnamely, the printer port. That port is often left idle in an environment of networked printers and servers. Even if it is being used by a printer or scanner that can't be disconnected, it's possible to buy a second printer-port card for less than $50.
The most basic type of printer port is the AT version. It contains an 8-bit output port (data port), a 5-bit input port (status port), and a 4-bit output port (control port). Later models of PCs improved on this slightly. The PS/2 printer port has a data port that's bidirectional. It can be programmed to read data in as well as write it out. The most recent addition to the printer-port family is the enhanced printer port (EPP), which boasts data transfer rates of up to 1 Msample/s. This article will concentrate on the AT/PS2 versions, as these are the most straightforward and the easiest to program.
The name given to the printer port indicates its location in the PC's memory map. There are three possible locations in which the printer-port registers can reside:
| Port Name | Base Address |
| LPT1 | 0x378 (most common) |
| LPT2 | 0x278 |
| PRN | 0x3BC |
A number of ways exist for determining where the printer port lies in the memory. The most foolproof is to examine the computer's BIOS information. This area of memory contains information about all of the peripherals which make up the computer, such as serial ports, disk drives, and of course the printer port.
When the PC is powering up, it first attempts to locate a printer port by checking address 0x3BC. It then checks address 0x378 and 0x278. As printer ports are discovered, their addresses are copied into locations in the BIOS memory. The first address will be stored at location 0040:0008, the second at 0040:000A, and the third at 0040:000C. Listing 1 is a C-code example of a function that will return the address of the first printer port found.
Once the user figures out which port he or she is working with, that port can start being used as a control device. As stated previously, the three ports comprising the printer port are data, status, and control. Each has an associated register. Figure 1 shows the contents of those registers and the pin assignments of the printer-port connector.
The data-port register contains 8 bits. Every bit corresponds to a pin on the printer-port connector. In other words, writing a logic 1 to a bit position will set the corresponding pin to a logic 1. To set data bit 0 (pin 2) and data bit 7 (pin 9) to a logic 1 while setting all other bits to a logic 0, load the register with 0x81 (10000001 in binary). While the data port is set to input mode, reading the register's contents will reflect the state of the pins on the port connector at the time of the read.
The status register contains 5 bits that reflect the state of the input pins on the D-type connector. The five signal lines used for data input are BUSY, ACK, PE, SLCT, and ERROR. Note that the BUSY bit contains the inverse of the logic level on the BUSY pin.
Though it needs some care in programming, the control register basically acts like the data register. It contains two bits (6 and 7) that are reserved and shouldn't be changed. Bit 5 controls the direction of the data port (logic 1 for input mode, logic 0 for output mode). Every time a falling edge is detected on the ACK pin, bit 4 can generate an interrupt. This bit should be set to a 0 for normal operation. Bits 3 to 0 will be used to set the state of the control-port pins on the D-type connector. The SLCT IN, AUTOFEED, and STROBE pins will be the inverse of their register setting.
Understanding the registers and bits still doesn't illustrate how the printer port can be used as a control device, however. As an example, say the objective is to program a digital-to-analog converter (DAC) to produce a ramp at its output. The DAC used here is the AD7801, which is an 8-bit parallel-loading DAC.