Welcome to second part of my parallel port tutorial. If any of you thought that the previous part was too techie, then here's the good news...unless you intend to build a printer you don't need to learn ANY of the previous data...well almost. If you intend to just light a few LEDs or to simply read data without any handshake methods then it's very easy. All you have to know are the data port address (378h remember?) and the data pins (pins 2 to 7). This is how the port looks from backside of your computer.
Since C is the only language in which I can claim some experience, my programs will naturally be in it. However the logic is simple and can easily be ported to any other language.
/*********************************************************/
/******Simple program to write to and read from port******/
/*********************************************************/
#include <stdio.h>
#include <dos.h>
void main()
{
int val;
printf("Values currently in port");
val=inportb(0x378); //read the port
printf("%x \n",val);
outportb(0x378,0x0F); //write some value to port
printf("New values in port");
val=inportb(0x378); //read port again. should be 0F
printf("%x \n",val);
}
This program utilizes the dos.h header file. The functions inportb() and outportb() found in this file are used for direct I/O operations in DOS. The above program first reads the existing values latched in the port using val=inportb(0x378); This may be any junk value. The function "inportb" is of the format return-value=inportb(port
address). Next we output a value to port using outportb(0x378,0x0F); We write the value 0F(0000 1111) to the port as the function is of the form outportb(port address, out-value). Now using a multimeter or an LED circuit we can see that the pins 2 - 5 (D0 - D3) are HIGH, nearly 5V and the pins 6 - 9 (D4 -D7) are LOW, nearly 0V.
Now that you have seen the basic I/O functions required to handle data transfer from or to the parallel port, surely you can create programs to suit your needs.
That much was for the DOS user. For the Linux user I/O operations are generally more difficult owing to protected mode operation of the OS. However simple data transfers of the above type can be handled relatively easily. The header file to use instead of <dos.h> is <asm/io.h>. The functions to use are outb(value, port-address) and inb(port-address). For more details refer Linux I/O port programming mini-HOWTO by Riku Saikkonen.
By the way if anything goes wrong, please don't blame me. Be careful with grounding. It might be a good idea to shutdown your system before connecting your hardware to the port. You might damage it otherwise. Also try to use a buffer IC in the external circuit to interface with the port.
And thus we come to the end of our first tutorial. In due course I might come up with others.
 
|