Parallel Port Programming (PART 1) With C
Parallel Port Programming (PART 1) With C
c o m Página 1 de 13
Electronics
HOME Articles/ Tutorials eBooks
Directory
About Us FORUM Links Contact Us
Parallel port is a very commonly known port, widely used to connect the printer to the PC.
If you see backside of your computer, there will be a port having 25 pins with a small
symbol like this: . That port is known as LPT port or printer port. We can program this
port for device control and data transfer. In this article, we will learn basics of parallel port
and programming the parallel port.
In computers, ports are used mainly for two reasons: Device control and
communication. We can program PC's Parallel ports for both. Parallel ports are mainly
meant for connecting the printer to the PC. But we can program this port for many more
applications beyond that.
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 2 de 13
EasySync Ethernet-Serial
1 to 8 Port Ethernet RS232/422/485 Serial Servers
from under $120
www.easysync-ltd.com
Parallel ports are easy to program and faster compared to the serial ports. But main
disadvantage is it needs more number of transmission lines. Because of this reason
parallel ports are not used in long distance communications. Let us know the basic
difference between working of parallel port and serial port. In serial ports, there will be two
data lines: One transmission and one receive line. To send a data in serial port, it has to be
sent one bit after another with some extra bits like start bit, stop bit and parity bit to detect
errors. But in parallel port, all the 8 bits of a byte will be sent to the port at a time and a
indication will be sent in another line. There will be some data lines, some control and
some handshaking lines in parallel port. If three bytes of data 01000101 10011100
10110011 is to be sent to the port, following figures will explain how they are sent to the
serial and parallel ports respectively. We can understand why parallel port communication
is faster compared to that of serial.
In computers, ports are used mainly for two reasons: Device control and
communication. We can program PC's Parallel ports for both. Parallel ports are mainly
meant for connecting the printer to the PC. But we can program this port for many more
applications beyond that.
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 3 de 13
Parallel ports are easy to program and faster compared to the serial ports. But main
disadvantage is it needs more number of transmission lines. Because of this reason
parallel ports are not used in long distance communications. Let us know the basic
difference between working of parallel port and serial port. In serial ports, there will be two
data lines: One transmission and one receive line. To send a data in serial port, it has to be
sent one bit after another with some extra bits like start bit, stop bit and parity bit to detect
errors. But in parallel port, all the 8 bits of a byte will be sent to the port at a time and a
indication will be sent in another line. There will be some data lines, some control and
some handshaking lines in parallel port. If three bytes of data 01000101 10011100
10110011 is to be sent to the port, following figures will explain how they are sent to the
serial and parallel ports respectively. We can understand why parallel port communication
is faster compared to that of serial.
In the PC there will be D-25 type of female connector having 25 pins and in the printer,
there will be a 36-pin Centronics connector. Connecting cable will combine these
connecter using following convention. Pin structure of D-25 and Centronics connecters are
explained bellow.
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 5 de 13
16 16, 31 Init
17 17, 36 Select In
18 to 25 18 to 30, 33 GND
- 34, 35 N/C
figure 1.2
Now let us know how communication between PC and printer takes place.
Computer places the data in the data pins, then it makes the strobe low. When strobe goes
low, printer understands that there is a valid data in data pins. Other pins are used to send
controls to the printer and get status of the printer, you can understand them by the names
assigned to the pins.
To use the printer port for applications other than printing, We need to know how
ports are organized. There are three registers associated with LPT port: Data register,
Control register and Status register. Data register will hold the data of the data pins of the
port. That means, if we store a byte of data to the data register, that data will be sent to the
data pins of the port. Similarly control and status registers. The following table explains
how these registers are associated with ports.
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 6 de 13
* Pins with * symbol in this table are hardware inverted. Than means, If a pin has a
'low' ie. 0V, Corresponding bit in the register will have value 1.
Signals with prefix 'n' are active low. That means, Normally these pins will have low
value. When it needs to send some indication, it will become high. For example, Normally
nStrobe will be high, when the data is placed in the port, computer makes that pin low.
Normally, data, control and status registers will have following addresses. We need
these addresses in programming later.
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 7 de 13
Note: All the parallel ports do not have bidirectional capability. Earlier parallel ports
had only output enabled in data pins since printers only inputs data. But latter, to make
parallel port capable of communicating with other devises, bidirectional ports are
introduced.
By default, data port is output port. To enable the bidirectional property of the port,
we need to set the bit 5 of control register.
To know the details of parallel ports available in your computer, follow this
procedure:
You will get a tree structure of devices; In that Expand "Ports(Com1 & LPT)".
Double Click on the ECP Printer Port(LPT1) or any other LPT port if available.
You will get details of LPT port. Make sure that "Use this Port (enable)" is
selected.
Select tab recourses. In that you will get the address range of port.
To start programming, you will need a D-25 type Male connector. Its pin structures
can be found in the connector as follows:
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 8 de 13
When we want to find out whether particular pin of the port is high or low, we need to
input the value of corresponding register as a byte. In that, we have to find out whether the
corresponding bit is high or low using bitwise operators. We can't access the pins
individually. So, you need to know basic bitwise operations.
Main bitwise operators that we need are bitwise AND '&' and bitwise OR '|'. To make
a particular bit in a byte high without affecting other bits, write a byte with corresponding bit
1 and all other bits 0; OR it with original byte. Similarly, to make particular bit low, write a
byte with corresponding bit 0 and all other bits 1; AND it with original byte.
In Turbo C, there are following functions used for accessing the port:
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 9 de 13
outport() function sends a word to port, inport() reads a word from the port. outportb()
sends a byte to port and inportb() reads a byte from the port. If you include DOS.H header,
these functions will be considured as macro, otherwise as functions. Function inport() will
return a word having lower byte as data at PORTID and higher byte as data at PORTID+2.
So, we can use this function to read status and control registers together. inportb() function
returns byte at PORTID. outport() writes the lower byte to PORTID and higher byte to
PORTID+1. So this can be used to write data and control together. outportb() function write
the data to PORTID. outport() and outportb() returns nothing.
Let us start with inputting first. Here is an example program, copy it and run in Turbo
C or Borland C without anything connected to parallel port. Then you should see data
available in status register and pin numbers 10, 11, 12, 13 and 15 of the parallel port. Pin
11 (active low) is 0 and all other pins are 1 means it is OK.
/* file: ex1.c
by HarshaPerla for electroSofts.com.
Displays contents of status register of parallel port.
Tested with TurboC 3.0 and Borland C 3.1 for DOS.
*/
#include"stdio.h"
#include"conio.h"
#include"dos.h"
void main()
{
int data;
clrscr();
while(!kbhit())
{
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 10 de 13
data=inportb(PORT+1);
gotoxy(3,10);
printf("Data available in status register: %3d (decimal),
%3X (hex)\n", data, data);
printf("\n Pin 15: %d",(data & 0x08)/0x08);
printf("\n Pin 13: %d",(data & 0x10)/0x10);
printf("\n Pin 12: %d",(data & 0x20)/0x20);
printf("\n Pin 11: %d",(data & 0x80)/0x80);
printf("\n Pin 10: %d",(data & 0x40)/0x40);
delay(10);
}
}
Access to parallel
ports
Visual Basic 6.0 to
VB.NET, Delphi
Windows Vista, both
x86 and x64
www.zealsoft.com/ntport/
To understand bitwise operations: you want to find data in pin 15, value of (data & 0x08)
will be 0x08 if bit 3 of register is high, 0therwise.
Now, take a D-25 male with cables connected to each pins. Short all the pins from 18
to 25, call it as ground. Now you can run above program and see the change by shorting
pins 10, 11, 12, 13 and 15 to ground. I prefer using switches between each input pins and
ground. Be careful, do not try to ground the output pins.
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 11 de 13
To find out the availability of ports in a computer programmatically, we will use the
memory location where the address of port is stored.
If you run the the following code in Turbo C or Borland C, You will get the addresses of
available ports.
/*PortAdd.c
To find availability and addresses of the lpt ports in the
computer.
*/
#include <stdio.h>
#include <dos.h>
void main()
{
unsigned int far *ptraddr; /* Pointer to location of Port
Addresses */
unsigned int address; /* Address of Port */
int a;
ptraddr=(unsigned int far *)0x00000408;
clrscr();
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 12 de 13
else
printf("Address assigned to LPT%d is 0x%X
\n",a+1,address);
ptraddr++;
}
getch();
}
Next we will go to check output pins. To check the output, we will use LED's. I have
driven LED's directly from the port. But it is preferred to connect a buffer to prevent
excessive draw of current from the port. Connect an LED in series with a resister of 1KW
or 2.2KW between any of the data pins(2 to 9) and ground. With that, if you run the
program given below, you should see the LED blinking with app. 1 sec frequency.
#include"conio.h"
#include"dos.h"
void main()
{
while(!kbhit())
{
outportb(PORT, ~inportb(PORT) );
delay(1000);
}
}
I will stop this part here itself. Next part of this article is now ready. In the PART 2,
you will learn programming the parallel port in VC++. PART 2 is designed for the beginners
of VC++. Click here to Read part 2.
Part 3 is having the example using LCD module. There we are going to learn
connecting LCD module to parallel port. Read part 3.
http://electrosofts.com/parallel/ 3/1/2009
Parallel port interfacing tutorial: with C - e l e c t r o S o f t s . c o m Página 13 de 13
Also Read...
Search
n Web n
i
j
k
l
m j electroSofts.com
k
l
m
Home | About Us | Articles/ Tutorials | Downloads | Feedback | Links | eBooks | Privacy Policy
Copyright © 2005-2007 electroSofts.com.
[email protected]
http://electrosofts.com/parallel/ 3/1/2009