In
my last post, I described the circuit that I built for the project of controlling LEDs from the parallel port of a computer. At the end of that post, the curcuit was completed and we were ready to write the software to control it.
After doing some reasearch, I found that the parallel port can be controlled quite easily on Windows using a dll called inpout32.dll. This dll ( including source code ) can be found at:
http://www.logix4u.net/inpout32.htm.
The function that this dll offers that I was interested in for this project was called Out32. I created a simple wrapper unit that interfaces with this dll and calls the output function. The code for interfacing with the parallel port is very simple.
unit ParallelPortUnt;
interface
function Out32(wAddr:word;bOut:byte):byte; stdcall; external 'inpout32.dll';
function SendByteToParallelPort( Value: Integer;
AParallelPortAddress: Integer = $379 ): Boolean;
implementation
function SendByteToParallelPort( Value: Integer;
AParallelPortAddress: Integer = $379 ): Boolean;
begin
Out32( $378, Value );
Result := True;
end;
end.
The function that I created to call the Out32 function is called SendByteToParallelPort. The parallel port has 8 DATA pins, which are the eight pins that I hooked my eight LEDs up to. The intended use for these 8 pins was to be able to send one byte of information via the parallel port at one time. Each of the 8 pins represents one bit. So, the function that we have here, SendByteToParallelPort, accepts an integer value. This value has to be between 0 and 255, because that is the biggest number that can be stored in one byte ( eight bits ).
For my uses, I am most interested in not the ability to send bytes of information through the port, but more the ability to control electronics with electrical signals. In order to turn particular pins on and off, you simply need to send the correct value to the port.
For example, if we wanted to turn on the first and last pins, we would need to send the binary value of 10000001. Since one byte is 8 bits, we can send one byte of information with bits 1 and 8 turned on ( one ) and the other bits turned off ( zero ). Of course, in Delphi we cannot send a binary value to this function, so we first need to convert that value to decimal or hexidecimal form. The code that my program uses to determine a decimal value is the following:
FAsciiByteToSend := 0;
for i := High( FBinaryByteToSend ) downto Low( FBinaryByteToSend ) do
if FBinaryByteToSend[i] then
FAsciiByteToSend := FAsciiByteToSend + ( 1 shl ( i - 1 ) );
In the above code example, I have an array of 8 boolean values that I am converting to a decimal value ( FAsciiByteToSend ). I loop through the boolean values, and for each one, if it is a true value use the shl function to set that bit to a 1.
In our above example of wanting to set the first and last LEDs, we had a binary value of 10000001. That value translated to decimal is: 129. So in order to turn on the first and last LEDs we call SendByteToParallelPort and pass the value of 129.
The second parameter to SendByteToParallelPort is the address. This is the memory address of the parallel port. On most computers the address is 378. In order to send data to the DATA port you have to send to one more than that address. So on most computers the address you will want to send to is 379 ( $379 in hex ).
If that address doesn't work, the address of your parallel port can be found by going into your computer's Device Manager. To get to the device manager on Windows 2000, go Start->Settings->Control Panel->System. On the hardware tab, click Device Manager. Under Ports, you should have an entry for LPT1 ( your parallel port ). Right-click properties and go to the Resources tab. This tab will show the address of your parallel port.
Once I had the ability to turn on and off the eight LEDs, it was easy to build a simple application that would allow the user to control the LEDs in a variety of ways. The following is a screenshot of the program I wrote.
This program allows the user to send a charactor or ascii code to the parallel port, toggle individual pins on and off and supports a random feature that will keep changing the combination of LEDs that are lit to create a neat effect.
This is a couple pictures of the curcuit in action:
The program I created to run the curcuit is available for download. You can download both the Windows executable and the source code for free. You can get to it by
clicking here.
I have included the
Inpout32.dll as well. This dll was created by
Logix4u, so check with them for the latest version. They offer this dll and source code for free for use in both commercial and non-commerical applications.