Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

Computer In Daily Life

Student    : Didar Cherkezov

Aim         : To control and work all of the electronic devices at home such as lamps, radio, tape recorder, alarm, air conditioner and so on by using the printer port of computer.

Ashgabat-2000


Controlling Hardware By Parallel Interface

Acknowledgements

The printer port or parallel port on your IBM computer is well suited for hardware control,
data input, or other interfacing. If you just send (write or output) a number to the correct processor I/O port the corresponding bits will appear as ones and zeros (5 volts and 0 volts) on the connector pins on the printer adapter. It's as simple as that. It's nearly as simple to get signals back, but there are only five bits of input -- the status signals that the PC expects from the printer.

PC parallel port can be very useful I/O channel for connecting your own circuits to PC. The port is very easy to use when you first understand some basic tricks. This document tries to show those tricks in easy to understand way.

WARNING: PC parallel port can be damaged quite easily if you make mistakes in the circuits you connect to it. If the parallel port is integrated to the motherboard (like in many new computers) repairing damaged parallel port may be expensive (in many cases it it is cheaper to replace the whole motherborard than repair that port). Safest bet is to buy an inexpensive I/O card which has an extra parallel port and use it for your experiment. If you manage to damage the parallel port on that card, replacing it is easy and inexpensive.

DISCLAIMER: Every reasonable care has been taken in producing this information. However, the author can accept no responsibility for any effect that this information has on your equipment or any results of the use of this information. It is the responsibly of the end user to determine fitness for use for any particular purpose. The circuits and software shown here are for non commercial use without consent from the author.

 

Introduction

The printer port works well as a hardware interface because it is a "real-time" device. When
you output a number to the port, voltages appear on the connector pins, and when you input

you get the real time status of the bits (voltages) in the input pins. This is in contrast to the serial port which handles things by the RS232 protocol and sends and receives strings of pulses which have to be 're-assembled' into meaningful numbers. RS232 also uses 'handshaking' signals to figure out when to start and stop strings of pulses and when to expect another batch. When the printer adapter is feeding a printer it also uses handshaking, hardware interrupts and so forth, but for our simple interfacing we can ignore these.

How to connect circuits to parallel port

PC parallel port is 25 pin D-shaped female connector in the back of the computer. It is
normally used for connecting computer to printer, but many other types of hardware for that port is available today.

Not all 25 are needed always. Usually you can easily do with only 8 output pins (data lines) and signal ground. I have presented those pins in the table below. Those output pins are adequate for many purposes.

pin function
 2   D0
 3   D1
 4   D2
 5   D3
 6   D4
 7   D5
 8   D6
 9   D7

Pins 18,19,20,21,22,23,24 and 25 are all ground pins.

Those datapins are TTL level output pins. This means that they put out ideally 0V when they are in low logic level (0) and +5V when they are in high logic level (1). In real world the voltages can be something different from ideal when the circuit is loaded. The output current capacity of the parallel port is limited to only few milliamperes.

        Dn Out ———+
                     |+
  Sourcing         Load (up to 2.6 mA @ 2.4 v)
                     |-
        Ground ———+
 

 Simple LED driving circuits

You can make simple circuit for driving a small led through PC parallel port. The only components needed are one LED and one 470 ohm resistors.

You simply connect the diode and resistor in series. The resistors is needed to limit the current taken from parallel port to a safe value (not overloading the parallel port chip).

Then you connect the circuit to the parallel port so that one end of the circuit goes to one data pin (that one you with to use for controlling that LED) and another one goes to any of the ground pins. Be sure to fit the circuit so that the LED positive lead (the longer one) goes to the datapin. If you put the led in the wrong way, it will not light in any condition. You can connect one circuit to each of the parallel port data pins. In this way you get eight software controllable LEDs.

The software controlling is easy. When you send out 1 to the datapin where the LED is connected, that LED will light. When you send 0 to that same pin, the LED will no longer light.

 

Control program

The following program is an example how to control parallel port LPT1 data pins from your
software. This example directly controls the parallel port registers, so it does not work under some multitasking operating system which does not allow that. It works nicely under MSDOS. You can look the Borland Pascal 7.0 code (should compile also with earlier versions also) and then download the compiled program LPTOUT.EXE.

Program lpt1_output;
Uses Dos;
Var
   addr:word;
   data:byte;
   e:integer;
Begin
   addr:=MemW[$0040:$0008];
   Val(ParamStr(1),data,e);
   Port[addr]:=data;
End.

How to use the program

LPTOUT.EXE is very easy to use program. The program takes one parameter, which is the
data value to send to the parallel port. That value must be integer in decimal format (for example 255). Hexadecimal numbers can also be used, but they must be preceded by $ mark (for example $FF). The program hoes not have any type of error checking to keep it simple. If your number is not in correct format, the program will send some strange value to the port.

 

Example how to use the program

LPTOUT 0
Set all datapins to low level.

LPTOUT 255
Set all datapins to high level.

LPTOUT 1
Set datapin D0 to high level and all other datapins to low level.
 

How to calculate your own values
to send to program

You have to think the value you give to the program as a binary number. Every bit of the
binary number control one output bit. The following table describes the relation of the bits, parallel port output pins and the value of those bits.

Pin     2   3   4   5   6   7   8   9
Bit     D0  D1  D2  D3  D4  D5  D6  D7
Value   1   2   4   8   16  32  64  128

For example if you want to set pins 2 and 3 to logic 1 (led on) then you have to output value 1+2=3. If you want to set on pins 3,5 and 6 then you need to output value 2+8+16=26. In this way you can calculate the value for any bit combination you want to output. 


Making changes to source code

You can easily change te parallel port number int the source code by just changing the
memory address where the program read the parallel port address. For more information, check the following table.

Format of BIOS Data Segment at segment 40h:
Offset  Size    Description
 08h    WORD    Base I/O address of 1st parallel I/O port, zero if none
 0Ah    WORD    Base I/O address of 2nd parallel I/O port, zero if none
 0Ch    WORD    Base I/O address of 3rd parallel I/O port, zero if none
 0Eh    WORD    [non-PS] Base I/O address of 4th parallel I/O port, zero if none

For example change the line addr:=MemW[$0040:$0008]; in the source code to addr:=MemW[$0040:$000A]; if you want to output to LPT2.

The following examples are short code examples how to write to I/O ports using different languages. In the examples I have used I/O address 378h which is one of the addresses where parallel port can be. The following examples are useful in DOS.

 Assembler


MOV DX,0378H
MOV AL,n
OUT DX,AL 

Where n is the data you want to output.

 

Parallel port controlling in windows programs

Direct parallel port controlling in possible under Windows 3.x and Windows 95 directly from
16 bit application programs and DLL libraries. So you can use the C example above in Windows 3.x and Windows 95 if you make your program 16 bit application. If you want to control parallel port from Visual Basic or Delphi then take a look at the libraries at Parallel Port Central at http://www.lvr.com/parport.htm.

Direct port controlling from application is not possible under Windows NT and to be ale to control the parallel port directly you will need to write some kind of device driver to do this. You can find also this kind of drivers from Parallel Port Central.


Controlling some real life electronics

The idea of the interface shown above can be expanded to control some external electronics
by simply adding a buffer circuit to the parallel port. The programming can be done in exactly the same way as told in my examples.

 Building your own relay controlling circuit

The following circuit is the simples interface you can use to control relay from parallel port:

                             Vcc
                              |
                              +———+
                              |    __|__
                            Relay   /^\  Diode 1N4002
                             Coil  /—\
                              |      |
                              +———+
                              |
                           | / 
                 4.7K    B |/  C
parallel port >-\/\/\/\/—|        NPN Transistor: BC547A or 2N2222A
data pi                    |\  E
                           | V
                             |
parallel port >———————+
ground pin                   |
                          Ground

The circuit can handle relays which take currents up to 100 mA and operate at 24V or less. The circuit need external power supply which has the output voltage which is right for controlling the relay (5..24V depending on relay). The transistor does the switching of current and the diode prevent spikes from the relay coil form damaging your computer (if you leave the diode out, then the transistor and your computer can be damaged).

Since coils (solenoids and relay coils) have a large amount of inductance, when they are released (when the current is cut off) they generate a very large voltage spike. Most designs have a diode or crowbar circuit designed to block that voltage spike from hitting the rest of the circuit. If that diode is bad, then the voltage spike might be destroying your “sink” transistor or even your I/O card over a period of time. The mode of failure for the sink transistor might be short circuit, and consequently you would have the solenoid tap shorted to ground indefinitely.

The circuit can be also used for controlling other small loads like powerful LEDS, lamps and small DC motors. Keep in mind that those devices you plan to control directly from the transistor must take less than 100 mA current.

WARNING: CHeck and double check the circuit before connecting it to your PC. Using wrong type or damaged components can cause you paralllel port get damaged. Mistakes in making the circuit can result that you damage your parallel port and need to buy a new multi-io card. The 1N4002 diode in parallel with the relay is an essential protection component and it should not be left out in acu case, or a damage of the parallel port can occur because of high voltage inductive kickback from the relay coil (that diode stops that spike from occuring),

 

Safer new design

The circuit example above works well and when transistor is of correct type and working
properly. If for some reason B and C sould be shorted together and you are suing more than +5V in the relay side, the circuit can push that higher voltage to the parallel port to damage it. The following circuit uses two 1N4148 diodes to protect parallel port against higher than +5V signals and also against wrong polarity signals (power on the circuit is accidentally at wrong polarity.

                             Vcc
                              |
                              +———+
                              |    __|__
                            Relay   /^\  Diode 1N4002
                             Coil  /—\
                              |      |
                              +———+
                              |
          Diode            | /
          1N4148  4.7K   B |/  C
parallel  >-|>|-+—\/\/\/—|        NPN Transistor: BC547A or 2N2222A
port data       |          |\  E
pin             +-|<|-+      | V
            1N4148    |      |
parallel  >—————+———+
port ground                  |
                          Ground
 

Adding even more safety idea: Repalce the 1N4148 diode connected to ground with 5.1V zener diode. That diode will then protect against overvoltage spikes and negative voltage at the same time.

 

Bad circuit example

I don’t know WHY I see newbies who don’t THINK electronics very well yet always putting
the relay “AFTER” the transistor, as if that was something important. Well it’s NOT, and in fact its a BAD PRACTICE if you want the parallel port to work well! This type of bad circuit designs have been posted to the usenet electronics newsgroups very often. The following circuit is example of this type of bad circuit design (do not try to build it):

                             Vcc
                              |
                           | /
                 4.7K    B |/  C
parallel port—\/\/\/\/—|        NPN Transistor: BC547A or 2N2222A
                           |\  E
                           | V
                             |
                             +———+
                             |    __|__
                           Relay   /^\  Diode 1N4002
                           Coil   /—\
                             |      |
                             +———+
                             |
                           Ground

NOTE: This is a bad design. Do not build or use this circuit.

The problem of the circuit is that the voltage which goes to the relay is always limited to less than 4.5V even if you use higher Vcc supply. The circuit acts like an emitter follower, which causes that the voltage on the emitter is always at value base voltage - base to emitter voltage (0.6..0.7V). This means that with maximum of 5.1V control voltage you will get

The output side of the opto-isolator is just like a transistor, with the collector at the top of the circuit and the emitter at the bottom. When the output is turned on (by the input light from the internal LED in the opto-coupler), current flows through the resistor and into the transistor, turning it on. This allows current to flow into the relay.

Turning the input on the parallel port off causes the output of the opto-isolator to turn off, so no current flows through it into the transistor and the transistor turns off. When transistor is off no current flows into the relay, so it switches off. The diode provides an outlet for the energy stored in the coil, preventing the relay from backfeeding the circuit in an undesired manner

Conclusion

The first line above may be a bit confusing, but remember that bits 0, 1, and 3 are inverted, so
to get 1111 on the pins, you need to send out 0100. 04h = 00000100, so we also are zeroing the top 4 bits. This insures that the interrupt enable on bit four is not set, which might cause trouble. In basic this routine is apt to be pretty slow, and if you need speed, you should code in assembly. These are byte wide logical operations which are very fast in machine language. You would then pass the byte to a high level language to use as an integer variable. I understand that C can do this kind of stuff very fast, if optimized. I have no experience with this.

By using this kind of circuits allmost everything in the house mey be controlled by the computer. At the same time it makes the human life easy. 


References:

1. Thomas S. Dickinson

National Synchrotron Light Source

Brookhaven National Laboratory

Upton, NY 11973 USA

Internet: tsd@bnl.gov

(516) 282-7196 (work)

(516) 282-3238 (fax)

 

2. Locksley Haynes

Dept of Electrical Engineering,

Morgan State University, Baltimore, MD, May 7, ‘97

3.Scott McGrath, Vermont Technical College

 smcgrath@night.vtc.vsc.edu