Site hosted by Angelfire.com: Build your free website today!

 

About Me

 

Downloads

 

Guestbook


Parallel Port Interfacing

If you want to control something electronic with your PC, well, the PC printer port is the easiest way to go. You can use the parallel port to control motors, lights etc. via simple circuits.

The BIOS of your motherboard defines the mode of the parallel port. The following modes are commonly found in Pentium-class motherboards: SPP, ECP, EPP and ECP+EPP. Auto and Bidirectional mode are sometimes included in newer boards, .

We'll be using SPP or Standard Parallel Port mode in our examples. It has 8 DATA lines, 5 STATUS lines and 4 CONTROL lines. A total of 12 output lines and 5 input lines. Although I have used ECP+EPP mode with my controller boards without damaging my PC, I still dont recommend using it especially when using the STATUS and CONTROL pins (to be discussed later).

All input and output pins are TTL levels. 5V comes out of each pin of the parallel port and can source a few milliAmperes. The current of the parallel port varies from manufacturer. There's really no standard value. Use a buffer (like 74LS244) to increase the current coming from the output pins.

Figure 1 (DB25)

The printer port or parallel port has 25 pins and here is a summary of their pin number, name, brief description, state.

Pin No.
Name
Description
1 strobe control
2 d0 data - active high
3 d1 data - active high
4 d2 data - active high
5 d3 data - active high
6 d4 data - active high
7 d5 data - active high
8 d6 data - active high
9 d7 data - active high
10 acknowledge status - active low
11 busy status
12 paper out status - active low
13 select in status - active low
14 auto control
15 error status - active low
16 initialize control
17 select prt control
18 Ground  
19 Ground  
20 Ground  
21 Ground  
22 Ground  
23 Ground  
24 Ground  
25 Ground  

Active high means that the pin is normally low or does not have voltage. Active low is you guessed it, the pin is normally has voltage. Active low pins are sometimes represented with a bar line on top. To elaborate further, the pins can have two states, they can either be with voltage or without voltage.

These pins are divided into three groups. They are DATA, STATUS and CONTROL groups or we'll simply call them ports. If you will examine the chart above, the description part of each pin states which port they belong.

Each port can be accessed using their default assigned addresses usually in hexadecimal notation. Each address can hold 8 bits of data. In some text, each port was also called as a register but enough with the terms, let's do something useful with the parallel port.


Programming

In order for us to manipulate the parallel port, we'll discuss first in detail the architecture of all the available ports.

DATA port (address 378H)

As I've mentioned earlier, each port contain 8 bits or simply, 1 byte. For the DATA port, bit d7 is the most significant bit and bit d0 is the least significant. Most of our experimentation and actual projects will use this port.

d7 d6 d5 d4 d3 d2 d1 d0

Here are the corresponding pin numbers on the DB25 connector.

9 8 7 6 5 4 3 2

Not all DATA ports are capable of input/output. They are commonly output-only ports but some motherboards offer bidirectional. Even if it is not bidirectional, you can also read the content of the port and you will get the last byte that was sent. I've also read something about people driving signals to this port, (using it as input even when it is not set as bidirectional) I haven't tried this before and I dont intend to try this on my LPT port. If you do know the effect, please let me know.

STATUS port (address 379H)

s7' s6 s5 s4 s3 s2 s1 s0

Each bit stands for: s7'= busy; s6=acknowledge ; s5=paper out ; s4=select in; s3=error; s2 to s0= reserved.

The pin numbers follow.

11' 10 12 13 15 00 00 00

The STATUS lines are input only ports. If you try to use it as output, it will not accept the byte sent. There will be no change in its content. It cant be manipulated by software. Its address is located at 379H or 889 in decimal. Pins 10, 12, 13, 15 are normaly hi and pin 11 is normally lo. The default value of this port when in SPP mode and when there is no peripheral connected is 7F in hexadecimal. If a printer or scanner is connected, its value will be 87H. This is used for feedback from the external system.

CONTROL port (address 37AH)

I personally have not used this port but I will update this page as soon as possible. Anyway, here are the pins and pin numbers.

c7 c6 c5 c4 c3 c2 c1 c0

 

00 00 00 00 17 16 14 10

The control lines are input/output ports. The software register of the control lines is accessed at address 37AH or 890 in decimal. The default value is CCH.


Using Assembly Language

This is the best way to manipulate ports and do low level i/o. You can do bit by bit manipulation. To write to a port use [ out ] and to read a port use [ in ].

Here's there syntax:

OUT port, register

IN register, port

Register must be AX or AL and port must be a number(can be decimal, hex, or binary). The port is the address e.g. 378H, 379H, 37AH.

An example, say we want to initialize the DATA port to 00H. Using just two instructions by first MOVing the value 00H to AL and then using the OUT instruction. Now the content of AL will be sent to the port address 378H which is the DATA port.

MOV AL, 00H

OUT 378H, AL

So if we want to send 17H to the DATA port just follow the first example.

MOV AL, 17H

OUT 378H, AL

The content of the DATA port in binary will be.

0
0
0
1
0
1
1
1

Why? Review the number system conversion. 17H is also equal to 23 in decimal or 00010111 in binary. You can use the calculator program provided by Windows to check the conversion. Just be sure that you are in scientific mode.

Now, say we want to read the content of the STATUS port. Remember you cannot use OUT on the STATUS port. Using the address of the STATUS port which is 379H, we can determine its value like so:

IN AL, 379H

The content of the STATUS port will be placed in register AL. Now you can verify if what I wrote about the default value is correct but in case you get a different result, well . . . :-)

Remember that the DATA port can also be read. A technique called "masking" can be used to turn on or turn off each bit in the DATA port. Masking is done by using AND, OR and NOT, which are also the basic logical operations. Say we want to turn off bit d1 and the current value of the DATA port is 00010111B. First we must get the value of the DATA port using IN. Then we will apply the mask using AND. Finally, we will use OUT to change the value of the DATA port.

  IN AL,378H ; get the content of DATA port and store it to AL
  AND AL,11111101B ; ANDing AL with the mask value
  OUT 378H, AL ; turn off bit d1

Now how did I get the mask value? By following the AND truth table

0
X
0
=
0
0
X
1
=
0
1
X
0
=
0
1
X
1
=
1

ANDing is multiplying the two operands. When we used IN the value of AL became 00010111B with the red bit as the bit we will turn off or set to 0. By using AND, we multiplied bit by bit, the content of AL with a value that will not effect any change to the other bits besides d1.

  00010111B
X 11111101B

So the current content of DATA port is

0 0 0 1 0 1 0 1

Simple?

AND is used to selectively turn off bits while OR is used to selectively turn on bits.

Using Visual Basic

Visual Basic does not have OUT and IN instructions only the DOS Qbasic has them. If you want to interface using Visual Basic, you have to get a DLL first. You can visit Jan Axelson's Parallel Port Central. I also downloaded inpout32.zip from Mr. Axelson's website and win95io.zip from SoftCircuits. Both files enable VB to manipulate the parallel port. Just follow the instructions along with the zipped files on how to use them. Inpout32 also includes a sample program with source code.


Sample Circuits

Here is an easy example of interfacing LEDs using the 8 DATA ports in SPP mode. The resistors are there to limit the current on the LEDs.

Figure 2 (Schematic 1)

Each pin of the DATA port controls one LED. A HI signal on the pin turns the LED on and LO signal turns it off.

So if you want to turn on LEDs D1, D3, D5 and D7, just use this command:

MOV AL, 55H

OUT 378H, AL

To turn off all the LEDs, just issue this command:

MOV AL, 00H

OUT 378H, AL

Just in case you're wondering how I got the value of 55H to turn on the four LEDs, well you have to know binary/hexadecimal conversion. To cut a long story short, you know that each port has 8 bits with a least and most significant digit (as mentioned in the DATA port above).

This 8-bit binary value has an equivalent hex and decimal value. To easily convert a binary value to decimal just remember this table with 1 as the least significant and 128 as the most siginificant digit

128
64
32
16
8
4
2
1

 

So if want to light up D1, D3, D5 and D7 then you have to send these hi signals to the DATA port.

0
1
0
1
0
1
0
1

 

All you have to do is add up the decimal equivalents where the bit is set to 1. We get 64 + 16 + 4 + 1 = 85. If we convert 85 to hex then we get 55H. Another method would be to use the 8-4-2-1 principle to convert from binary to hex directly.

8
4
2
1
8
4
2
1

 

Instead of adding all the decimal values like what we did before, we group them by four. We'll just add from the most significant bit to the fourth bit and from the fifth bit to the least significant. By adding the first group which is 4 + 1 = 5. The second group will give the result of 4 + 1 = 5. We get also 55H but this time there is no decimal to hex conversion.


I/O Board

A simple I/O board that you can build.

Output

The 8 output lines are the DATA ports, using the software register address 378H or base. All outputs are initially set to lo (~0V). All pins are active high. Pin 9 is the most significant bit while pin 2 is the least significant.

Any change in output will be displayed at the status bar.

Input

The 4 input lines are the STATUS ports, using the software register address 379H or base+1. All inputs are initially set to hi (~+5V). All pins are active low. The Input lines are the opposite of Output lines.

The initial value of the port 379H(STATUS) is 7FH. Pins 10, 12, 13, 15 of the parallel port are used and are initially held hi. You must use the input pins of the 74LS244. You can not change the value of the port via software. In order to detect an input, you must send a lo signal to the pin. Once the lo signal is removed, the pins return to their settings which is hi.

Any change in input will be displayed at the status bar.

Circuit Description

The circuit uses a ULN2803 Darlington transistor array current driver. It utilizes all the 8 pins of the DATA port for output and can drive relays, stepper motors, etc. The IC is made by Allegro Microsystems, a data sheet is available so if you want more information, go to their website.

The 74LS244 was used as an input buffer. You have to send a LO signal to the IC in order for the parallel port to detect an input. The STATUS lines of the parallel port are held HI (there normal state) by the 4 pull-up resistors when there is no input. The 4-220Kohm 1/4 Watt resistors are there to ensure that the input current is well within the limits of the IC. If you cant find 250K-ohm resistors, you can use 220K ohms instead.

It is advisable to use normally-open push button switches on the inputs. One end of the switch will be connected to one input pin of the 74LS244 and the other pin will be connected to ground.

The 7805 voltage regulator supplies the power for the 74LS244.

The supply can be a 9-volt battery, a dedicated power supply of a minimum of 5 V up to a maximum of 30V, or better yet, construct a variable power supply. If you want to drive a stepper motor, use a supply with at least 500 mA.

Download the sample software I made in Visual Basic.


Further Notes

The inputs and outputs of the parallel port are TTL logic, if you want to use CMOS ICs with voltage other than 5V then you will have to use a level shifter like CD4504.

revised 07-04-2002

orig 03-20-2001


pao7/4/2k2
Home
Top
    Email