WinPort9x: A Parallel Port Application

By Pete Nowalski

 

WinPort9x is a Windows 9x application designed to demonstrate basic programming of a PC parallel printer port. The program displays the registers of LPT1 and allows the user to read and write data to the registers. With program modification, and a user designed hardware interface, the PC parallel port can be used to remotely monitor and control real world devices.

Parallel ports usually have three different modes that can be selected in the computers Bios. They are SPP or Standard, EPP, and ECP. WinPort should work in any of these modes, but if you experience difficulties, changing modes is an option.

Windows NT, 2000, and XP prevent user applications from accessing hardware. WinPort9x will fail on these operating systems. A driver or DLL would be required to access ports on these operating systems.

A Note of Caution

The PC parallel port is designed to operate at TTL voltage levels. An electrical high is +2.4 to +5 volts, and an electrical low is 0 to +0.8 volts. Output current is limited. Without specific port documentation, and using no input/output buffering, I would limit current to around 5 milliamps output low (sink) current, and -.2 milliamps source (output high) current. So, without any type of buffering, don't attempt to drive more than LED's from the port's output bits.

Disclaimer

This application was intended to operate with experimental circuits connected to the parallel port, not printers, scanners, or other conventional hardware. Exceeding the voltage and current limitations of your parallel port could damage the port or associated hardware. Until you read this help file for all recommended operating practices, don't use this program with hardware connected to the parallel port. I assume no responsibility for loss or damages incurred from use of this application.

 

Registers

The Parallel Port is composed of three 8-bit registers. Two of the registers are output registers, and one is an input register. All of the register's values can be read, and the output registers values can be written to.

Some of the register's bits are not connected to the port's DB25 connector. Bits with no DB25 connection are labeled NC.

Data register (Output)

Bit

7

6

5

4

3

2

1

0

Pin

9

8

7

6

5

4

3

2

 

Status register (Input)

Bit

7

6

5

4

3

2

1

0

Pin

11

10

12

13

15

NC

NC

NC

 

Control register (Output)

Bit

7

6

5

4

3

2

1

0

Pin

NC

NC

NC

NC

17

16

14

1

 

Several of the register bits are inverted. A value read from an inverted bit is opposite of its true value. When written to, an inverted bit will write the opposite of the value written. WinPort uses software to compensate for these inversions, making them transparent to the user. This is covered in the Theory section of this file.

Different parallel ports have their inputs hard wired high or low, and some a combination of both. It may be necessary to use an inverter; an IC7404 or equivalent, to achieve desired active high or low operation.

Caution

Do not attempt to force port output bits high or low with external devices.

 

Operation

Read

WinPort reads the port registers from the port menu, or by using the F12 key. Port data is displayed in binary and decimal, along with the time.

Write

The output registers can be written to from the port menu, or with the hotkeys. The F1 key will write the Data register, and the F2 key writes the Control Register. A binary or decimal value within the register's range can be entered in the dialog box. All write operations are automatically followed by a read operation. The escape key will cancel a write, and the enter key will send a selected value to the port.

Display

The most recent values are displayed at the top of the screen. The output values are displayed in red, and the input in blue.

Exit

Exit from the menu or by pressing the control and 'Q' keys.

 

Hardware

It is highly advisable to electrically isolate the parallel port from whatever circuit you decide to connect to it. One way to do this would be with a couple of 74LS367 buffer chips. These are Tri-State devices with an enable pin that electrically isolates the chip from anything connected to its output. These chips can sink about 24 milliamps (output low) and source -2.6 milliamps (output high) per pin. They are relatively cheap, about 70 cents apiece from DigiKey.

Optically isolated solid state relays are another alternative. Although more expensive, they can provide 2000v - 5000v of isolation, and are ideal for power line switching.

Another consideration would be to add an ISA parallel port card to your computer. They go for about 15-20 dollars.

Grounds

Pins 18 through 25 of the DB25 connector are grounds. When connecting the port to hardware, all eight of the grounds of the port should be tied to the user's circuit. The grounds act as shields, and help reduce crosstalk in the cable

Precautions

Never make or break connections to the computer while it is on.

Apply power to the computer and your circuit after your connections have been made.

Do not attempt to force the port output bits high or low with external devices.

This application was intended to operate with experimental circuits connected to the parallel port, not printers, scanners, or other conventional hardware.

 

Theory

This is a brief overview of the PC parallel printer port, and how WinPort programs the port. This application utilizes the port in its basic configuration. Bio-directional modes are not covered. Parallel ports integrated on the motherboard usually allow the modes to be changed in BIOS; otherwise a card may require a jumper change

Definitions

Input Register = A Register that receives an electrical signal from an external device.

Output Register = A Register that supplies an electrical signal to an external device.

The parallel port is defined by its by its LPT port number, and its I/O base address. LPT1 = base address 0x378 = parallel port #1

Composition

The port consists of three 8-bit registers at adjacent addresses.

Data register = base address + 0

Status register = base address + 1

Control register = base address + 2

Data register

Bit

7

6

5

4

3

2

1

0

Pin

9

8

7

6

5

4

3

2

The Data register is an output register. Since the Data register has no inverted bits, and all of its bits are connected to DB25 pins, coding the Data register the easiest. Here is a C code snippet used to write the Data register with a value of 255:

#Define BASEPORT 0x378

int DATA, STATUS, CONTROL;

_outp((BASEPORT), 255);

To read a value that has been written to the Data register:

DATA = _inp(BASEPORT);

Some compilers/languages may use a different variation of the _inp and_outp commands.

 

Status register

Bit

7

6

5

4

3

2

1

0

Pin

11

10

12

13

15

NC

NC

NC

The Status register is an input register. A read returns the state of the five pins connected to the DB25 connector. Bit 7 is inverted. A 1 or electrical high on pin 11 will return as a 0 when read. Bits 2-0 are undefined, and have no connection. A read of the Status register may return 111 for bits 2-0, which should be ignored. To read the Status register:

STATUS = (_inp (BASEPORT + 1) & 248) ^ 128;

The above statement reads the Status register, logically ands the result with 248 to remove the 1's from bits 2-0, then does an exclusive or on bit 7 to reverse the inverted value.

 

Control register

Bit

7

6

5

4

3

2

1

0

Pin

NC

NC

NC

NC

17

16

14

1

The Control register is an output register. Bits 3, 1, and 0 are inverted. So writing a 1 to these bits will actually output a 0, and writing a 0 will output a 1. Bits 7-4 have no connection. Since bits 3-0 are the only bits connected to the DB25 connector, we will only write a value from 0-15 to the register. To write the Control register with a value of 15:

_outp((BASEPORT + 2), (15 ^ 11));

The above statement does an exclusive or on the decimal value 15 before writing the register, therefore canceling the inversion of bits 3, 1, and 0.

When reading a value written to the Control register, bits 3, 1, and 0 will return the opposite value of the DB25 connections. Bits 7-4 may return 1111 for bits 7-4, which should be ignored. To read a value written to the Control register:

CONTROL = (_inp(BASEPORT + 2) & 15) ^ 11;

The above statement reads the Control register, logically ands the result with 15 to remove the 1's from bits 7-4, and does an exclusive or on bits 3, 1, and 0 to reverse the inverted values.

Bits 7 and 6 of the Control register are undefined. Bit 5 is an input mode selection bit for ports in the bio-directional mode. Bit 4 is an IRQ enable bit. These bits do not have any DB25 pin connections, and are not used in this tutorial.

SSP or Standard modes

If the parallel port has the SPP or Standard mode selection in the computer's bios, then the Control port has the capability of being an input or output register. In the Standard mode, bits 3-0 are open collector outputs, with pull-up resistors. They can be used as inputs by writing them high, and letting an external device pull them low. This allows the user to have 9 input bits as well as 8 output bits. This is illustrated in another program called "WinPort9x [SPP]". This program combines the Status and Control registers to create an input byte, and uses the Data register as an output byte. WinPort9x [SPP] should be available wherever you found this program.

Conclusion

So that wraps it up. This program is freeware. The source code may not be pretty, but it works. WinPort was written in C using the free lcc-Win32 compiler downloaded from http://www.cs.virginia.edu/~lcc-win32/

Sam's Teach Yourself C in 21 Days was used as a C reference guide.

 

Home