Reference:ParallelPort
From Bob's Basement
The Parallel Port is an excelent way of controlling simple circuits using a computer, for example a bank of relays.
For this I assume you have basic knowlege of what logic 1 and 0 are (typically 0 and +5V in most trivial applications).
Contents |
Differences
Serial
Most common kinds of outputs in use these days are 'serial'. This means data is sent over a single line in series, one bit at a time.
For example, for an 8-bit number 42 (in binary, 00101010) would be sent in sequence over a period of time. The time between each bit is determined by a clock (an oscillator which goes from 0 to 1 at a particular rate). In this diagram, the time division is one clock cycle, whatever that may be.
(black lines indicate time divisions, red lines indicate voltage levels)
This means that in order to send n-bits of data, this will take n clock cycles.
This is the basis behind most modern interconnection technology in common use, for example RS232, USB, Serial ATA, Ethernet amongst many many others.
The advantage is that you can send as many bits as you like down the line.
Wikipedia: Serial Communications
Parallel
However, there is another method which can be used to transmit data. Rather than sending each bit, one at a time, you can send multiple bits at once. This is sending data in parallel. In the example set above, sending the number 42 down a cable, you would have, say, 8 pins, which you can use to represent each bit of the number. For example Number: 42 - 00101010
- Pin 1 - 0
- Pin 2 - 0
- Pin 3 - 1
- Pin 4 - 0
- Pin 5 - 1
- Pin 6 - 0
- Pin 7 - 1
- Pin 8 - 0
Typically, there are "strobe" pins which tell whatever is listening on the other end that it can read the data from the cable.
Why Parallel is Better
When I say better, you must understand I mean for "simple" stuff.
If you imagine each pin as a "switch" that you can turn on or off from a computer, you can litterally wire up each pin to a relay (with a few transistors to provide enough current to actually trip the relay), and have 8 computer controlled relays.
If you needed more outputs, you'd be able to get a simple decoder to turn, say, a 7-bit number into 128 outputs.
Compare this to what's required for a serial device, which most likely needs a microcontroller, clock and other assciated components, which will effectively convert the serial signal into a parallel one! What's the point?
The disadvantage of using parallel interconnects is that you are limited by the number of pins available. You can't send piece of data that is greater than n-bits where n is the number of pins. Serial is more flexible in that it can send (within reason) any data imaginable.
Programming the Parallel port
Probably the simplest way to start programming the parallel port is by using the perl module, Device::ParallelPort. Take a look at the manual page. You'll also need to install Time::HiRes (for some reason the normal sleep() function doesn't work!)
To visualise your experiments, I suggest you get hold of a breadboard or veriboard, and wire up pins 2-9 (on a D25 connector) to an LED, and pick a pin from 18-25 and use that as a common ground for them all.
Once you've got this wired up, try the following scripts out:
This script should make all of the LEDs blink on and off.
#!/usr/bin/perl
use Device::ParallelPort;
use Time::HiRes qw { usleep };
my $port = Device::ParallelPort->new();
while (1) {
$port->set_byte(0,chr(255); # sets the output to 11111111
usleep(500000);
$port->set_byte(0,chr(0); # sets the output to 00000000
usleep(500000);
}
Or a binary counter:
#!/usr/bin/perl
use Device::ParallelPort;
use Time::HiRes qw { usleep };
my $port = Device::ParallelPort->new();
while (1) {
for (my $i = 0;$i<255;$i++) {
$port->set_byte(0,chr(255-$i)); # the chr converts the integer into a "byte"
usleep(60000);
}
}
Or, cylons!
use Device::ParallelPort;
use Time::HiRes qw { usleep };
my $port = Device::ParallelPort->new();
my $inc = 1;
while (true) {
$port->set_byte(0,chr(255));
if ($inc < 0) {
$i = 7;
} else {
$i = 0;
}
while (($i < 8) && ($i >= 0)) {
$port->set_bit($i,0);
$port->set_bit($i-$inc,1);
usleep(40000);
$i = $i + $inc; # go to the "next" bit
}
usleep(8000);
$inc = -$inc; # invert
}
You can have plenty of fun writing scripts to make LEDs blink. It's also a very useful way to test your script before letting it loose on high voltages.


