Parallel Port Interfacing
A parallel port is a type of interface found in a computer which is commonly connected by a DB25 connector.These are commonly used by printers.There are 3 types of parallel ports
- Standard Parallel Port (SPP)
- Enhanced Parallel Port (EPP)
- Extended Capabilities Parallel Port (ECP).
Standard Parallel Port (SPP)
Original parallel ports in IBM PCs were unidirectional and data was sent to the device.Unidirectional mean flow of data only on one direction either from the device the computer or computer to the device. This mode of operation is referred as standard mode or Centronics mode.In the current systems parallel ports have bidirectional function and at a speed of 80-300 KB per second.
-------------------------------------------------------------------------------------------------------------------------------
DB25 Connector:
The lines of the connector are divided into 3 groups , data lines,status lines and control lines.There are 8 data lines in a DB25 connector, 4 lines for control and 5 for status.
===============================================================================================
Data lines:
These lines are used for transmitting or receiving data.It is also known as data bus.
D0 to D7 are the data registers bits and their pins are pin 2 to pin 9 respectively.
===============================================================================================
Control Lines:
These are used to control the parallel device attached to the parallel port.
C0 to C5 are control registers bits and their pins are 1,14,16,17 respectively except C4 and C5 which is not on the connector.
STROBE - Pin 1 -This pin is used to indicate whether there are data on the data lines.It makes the printer activate the BUSY line and makes the printer to take in data.
AUTOFEED - Pin 14 - This is used to instruct the printer to insert a new line automatically on a carriage return.
INIT - Pin 16 - Used to initialize the printer
SLCT-INP - Pin 17 - Used to turn off or on the printer
===============================================================================================
Status Lines:
These are sent by the device attached to show the state or status of the device.
S3 to S7 are the status registers bits and their pins are 15,13,12,10,11.
ACK - Pin 10 -This is used to indicate that the printer has accepted the data and ready to take the next data or if interrupt is enabled it fires an interrupt.
BUSY - Pin 11 - This represents weather the printer is ready or not ready to take data(Reason might be anything).
PE - Pin 12 - Paper!Paper!, this is used indicate the printer is out of paper
SLCT - Pin 13 - Indicates weather the printer is in selected state or not.
ERROR - Pin 15 - Used to check if there is an error
===============================================================================================
Ground Lines:
The pins 18 to 25 are grounds.
These lines are connected to their respective registers internally except ground pins.
-------------------------------------------------------------------------------------------------------------------------------
I\O Ports:
Port Interrupt Start End
Port1 IRQ7 0x378 0x37F
Port2 IRQ6 0x278 0x27F
Port3 IRQ5 0x3BC 0x3BF
Each SPP Parallel Port has 3 registers; Data Register,Control Register , Status Register.
Standard Parallel Port Registers:
There are 3 registers , data register,status register, control register.All these are IO Mapped on an IBM PC.Here let us take an example of PORT1.The data register is at PORT1 + 0, Status register is at PORT1 + 1, and the control register is at PORT1 + 2.So now, the data register is 0x378, status register at 0x379 and the control register at 0x37A.The same follows for the PORT2 and the PORT 3.
Data Register:We write a byte to the parallel port by sending the data to the data register.The data is kept ready in the D7-D0 pins of the female connector.Reading the data register makes the data present on the data lines to transferred to the data register.But reading can only be possible if the parallel port is bidirectional because the device can send if the port is bidirectional.
Status Register:This status register is use for checking the state of BUSY,ACK,PE,SLCT and ERROR and it is read only.The BUSY pin is inverted and stored.The bits S0-S2 are not used.This is commonly use for checking the state of the printer weather it is busy or free.
Control Register:This register stores the 4 control instructions\signals of the device or printer and it is read\write register.The four signals are SLCT-INP,INIT,AUTOFEED, and STROBE on C3-C0 bits.The C3,C1 and C0 are inverted.
Not yet, the C4 Bit is used to enable or disable the parallel port interrupts and it is not on the connector.The IRQ 7,6,5 are assigned for parallel port 1,2 and 3 respectively.You need to enable this bit to get interrupts.Interrupts are raised from the ACK pin.
C5 Bit is used to enable or disable bidirectional function of the parallel port.Also note this bit would be a useless if bidirectional is not supported by the parallel port.
-------------------------------------------------------------------------------------------------------------------------------
Examples in C:
Looks for parallel port weather it exists or not.
#define ADDRESS_PTR 0x00000408
void check_port(void)
{
unsigned int far *p;
unsigned int address; int retval;
int count;
p=(unsigned int far *)ADDRESS_PTR;
for(count = 0; count < 3; count++)
{
address = *p;
if (address == 0) {
printf("No parallel port for LTP%d \n",++count); }
else {
printf("Parallel Port LTP%d is present and address is %Xh\n",++count,address); retval++; }
*p++; } return retval;//Returns the number of LTP Ports on the system
}
Initializes the printer connected to the given parallel port
void init_parallel(unsigned short port)
{
outportb(port+2, 0x0C); //Activates SLCT INP
outportb(port+2,inportb(port+2) & 0x0FB);//Send the INIT Signal
outportb(port+2, inportb(port+2) | 0x04 );
}
This reads the data from the given parallel port.
unsigned int read_parallel(unsigned short port)
{
unsigned int data;
data=inportb(port+1);
return data;
}
Writes data to the data register and send them to the printer
void write_parallel (unsigned short port,unsigned char data)
{
while(!inportb(port+1) & 0x80)
{
wait_ms(2);
}
outportb(port+0,data);//Places the data on the data lines
outportb(port+2,inportb(port+2) | 0x01);//Activate STROBE
wait_ms(10);
outportb(port+2,inportb(port+2) & 0x0FE);//Everything has an end, deactivate STROBE
}