Controlling the PC Parallel Port
The standard PC printer port is handy for testing and controlling devices. It provides an easy way to implement a small amount of digital I/O. I like to use to during initial development of a product -- before the "real" hardware is ready, I can dummy up a circuit using the printer port, and thus get started testing my software.
This source code module provides the low-level control of the port, implementing code to control 12 outputs and read 5 inputs.
This code was written for Borland C/C++ v3.1, but you should be able to adapt it for other compilers. You can view the source code online, or download an archive (prn_io.zip) that contains PRN_IO.C and PRN_IO.H. To use the module in your program, simply #include PRN_IO.H from wherever you need to call the functions, and compile and link PRN_IO.C into your program.
The following tables list the details of how the software interfaces to hardware port. Refer to the source code itself for more information, or check out one of the links at the end of this page.
Printer Port Addresses | |
---|---|
Printer Port | Base Address |
LPT1 | 0x0378 or 0x03BC |
LPT2 | 0x0278 or 0x0378 |
LPT3 | 0x0278 |
Printer Port Registers | |
---|---|
Register Name | Address |
Data Register | Base + 0x00 |
Status Register | Base + 0x01 |
Control Register | Base + 0x02 |
Data Register Bit Definitions | |||
---|---|---|---|
Bit | Function | Low | High |
7 (MSB) | D7 | 0 | 1 |
6 | D6 | 0 | 1 |
5 | D5 | 0 | 1 |
4 | D4 | 0 | 1 |
3 | D3 | 0 | 1 |
2 | D2 | 0 | 1 |
1 | D1 | 0 | 1 |
0 (LSB) | D0 | 0 | 1 |
Status Register Bit Definitions | |||
---|---|---|---|
Bit | Function | Low | High |
7 (MSB) | Busy | Busy | Not Busy |
6 | Acknowledge | Nack | Ack |
5 | Paper Status | No Paper | Paper |
4 | Selection Status | Not Selected | Selected |
3 | Error Status | No Error | Error |
2 | Not Used | 0 | 1 |
1 | Not Used | 0 | 1 |
0 (LSB) | Not Used | 0 | 1 |
Control Register Bit Definitions | |||
---|---|---|---|
Bit | Function | Low | High |
7 (MSB) | Not Used | 0 | 1 |
6 | Not Used | 0 | 1 |
5 | Not Used | 0 | 1 |
4 | Interrupt Control | Interrupts Disabled | Interrupts Enabled |
3 | Select | Selected | Not Selected |
2 | Initialize | False | True |
1 | Auto Feed | True | False |
0 (LSB) | Strobe (Active-Low) | True | False |
Source Code Implementation Notes:
- The code is written to use LPT1 at 0x0378; change it if you need to
- The code doesn't attempt to use interrupts
- The parallel port is also capable of enhanced modes (see the links below), but this code doesn't attempt to use them
- This code assumes that the port is configured as a "standard" or "normal" port; configuring the port for EPP or ECP modes may or may not work
Parallel Port Books:
Parallel Port Specs And Info:
- Fil's FAQ-Link-In Corner
- IBM Parallel Port FAQ/Tutorial
- Interfacing the IBM PC Parallel Printer Port
- Parallel Port Central
- Parallel Port Page
- Use of a PC Printer Port for Control and Data Acquisition
Parallel Port Drivers:
- IOPORT.OCX - Visual Basic Parallel Port Drivers
- I/O from Win32 - Delphi code for the DriverLINX driver
- Linux Parallel Port Home Page
- Port95nt - Parallel Port Driver for WinNT
- Port I/O - Controlling the parallel port from Visual Basic
- Port I/O From BCB - Accessing the parallel port from C++ Builder and Delphi
- TDLPortIO - A C++ Builder component for the DriverLINX Win95/98/NT Port I/O driver
- TVicHW32 and TVicPort - Device drivers that allow Win32 hardware access
- ZeeCube - Parallel Port Direct IO Driver for WinNT