How to use parallel port in C for control of external circuits

Hi All

I assume that some of the guys here have used laptops to control external circuits for roboticee type projects. I would like to control some motors using my laptop as the programming/control environment would be alot better. I can handle the electronic side of things quite well but I fall down when it comes to C programming. I have been learning for a while now, on and off. I need to know how to read data and output data, basically control the parallel port using c code. The old laptop is running win95 if it helps.

I think I need to write data to certain addresses but am not sure. Can anyone help?

Thanks

Naveed

Reply to
The Man With The Harmonica
Loading thread data ...

The parallel port is usually at 378 hex. If in doubt run debug at the DOS prompt, type d40:0 and look halfway along the top line for 78 03

q to exit debug

Anything you write to 378 shows on the pins

bit 0 on 3FA writes the strobe line inverted

bits 3-7 of 379 read as the status lines, b7 is inverted

The original PC would read back the data lines when you read 378, this is no longer the case, you just read back what you wrote.

It is possible to put the port into everal different and clever modes but hey, life's too short :o)

Reply to
Robin G Hewitt

Check the book "Parallel Port Complete." Discusses the modes, addresses, etc. that may be of help.

HTH Bill

Reply to
Bill Turnip

I can't speak to c programming, but the below links show how I use qbasic (should be included on your win95 machine) to send bytes to the parallel port to set the data pin values, and how to then read the resulting values on the status pins (in this case checking to see if contacts are open or closed). You probably could use the qbasic to test your hardware until the c code is worked out. The bottom links show other simple parallel port control setups.

formatting link
formatting link

Reply to
Si Ballenger

Like the others have said the Internet is full of examples on how to do this. Even if the example is written in Basic you ought to be able to readily convert it to C, as it only involves sending a byte to the proper physical memory address. I found a zillion (okay, only 421,000) links with the search phrase "parallel port programming" (no quotes).

However, I *strongly* advise you to either get a good book on the subject (I echo the recommendation for Jan Axelson's "Parallel Port Complete") as even with Win95 some BIOS settings and parallel port types can cause problems. The better how-to's discuss how to determine the type of parallel port you have, and how to change BIOS settings if need be.

There are also DLLs you can use with Windows to write and read with the parallel port. Again, do a Google search and you'll turn up many of them. (Also try Jan's Web site at

formatting link
Most of these come with examples.

Be sure to use the proper interface, or else you'll blow the parallel port right up. You mention you got the electronics side handled, but this needs to be said anyway...

-- Gordon Author: Constructing Robot Bases, Robot Builder's Sourcebook, Robot Builder's Bonanza

The Man With The Harm> I assume that some of the guys here have used laptops to control external

Reply to
Gordon McComb

A google search should bring up lots of info on how to use the parallel, serial and game port. I have used the parallel port using DJGPP. I suggest you start by turning LEDs on and off (output) and reading simple on/off switches (input).

Warning: Get it wrong an you may destroy your parallel port! So I suggest plugging in a separate parallel port card to play with.

Each parallel port has three registers.

DATA 278h 378h 3BCh STATUS 279h 379h 3BDh CONTROL 27Ah 37Ah 3BEh

// PARALLEL PORT REGISTERS #define DATA_REG 0x378 #define STATUS_REG 0x379 #define CONTROL_REG 0x37A

If you wanted to set the CONTROL_REG to say 4,

outportb(CONTROL_REG,4);

If you wanted to monitor bit 3 of the STATUS_REG,

while ((inportb(STATUS_REG) & 8) == 0){}

You might be able to set the DATA_REG to input or output by setting a bit in the CONTROL_REG

//Port turn around set bi-directional control as input outportb(CONTROL_REG,46);

//Set output to uni direction outportb(CONTROL_REG,14); // clear bit 5

Reply to
John Casey

I just relised you can't plug in an extra parallel port into a laptop. You might want to use a safe interface before experimenting, the 8255 perhaps?

formatting link
Other useful sites might be,

formatting link
formatting link

As someone pointed out you can use QBasic,

DEFINT A-Z DataReg = &H378 StatusReg = &H379 ControlReg = &H37A

OUT DataReg, v v = INP(StatusReg)

You can read the game port using QBasics STICK() for the analog inputs and STRIG() for the buttons.

You can also access the serial port from QBasic

Reply to
John Casey

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.