Paralel port control with Visual C
Date: Thursday, November 03 @ 04:11:40 EST
Topic: Tasarύmcύlarύmύz

Submitted by: Technoshaman

I’m not going to explain how to write an easy program using VC++ and MFC here. I learned what I needed while I was in the army from the book “Sam’s Teach Yourself VC++ in 21Days”, the PDF file is everywhere :) Let me begin with simply explaining the function which is going to turn pin 2 on and off each time the button is pushed (pin 1 is not used since it is the strobe pin for the printers). After getting the idea, you can use it with other programming languages. The first two lines are what we need, the rest is for displaying stuff on the main window.

int Pin;
unsigned short Port;
void CParallelPortControlDlg::OnButton1()
Pin = Pin ^ 1;

At first we assign 0 (the value to send to LPT1) to the Pin variable (to reset the port when we open the program). Then we assign 888 to the Port variable (address of LPT1). With the line Pin= Pin ^ 1; we XOR the value of Pin with 1 (during program this value may not be just 0), then we send this value to LPT1 with the command Out32(Port,Pin);. This results in turning pin 2 on if it is off, or turning it off it it is on (for decimal values 1,2,4,8,16,32,64,128 for pins 2,3,4,5,6,7,8,9 respectively). If we need to elaborate; Let our 8 bit Pin (LPT1 has 8 output pins) have the value 00010001 (decimal 1+16=17). Simply: Pins 2 and 6 are on. With this command we do 00010001 XOR 00000001 and get the result 00010000. If two values are XOR’ed and they are both 1s, the result is 0. If the values are 0,1 or 0,0 the result remains the same. The exe file is in the Debug folder.

If you are going to write a VC++ program for yourself, you need to add the necessary lines and the inpout32.lib to your project as shown in the picture. When you click OK on the link tab after typing the name of your lib file, it will be added to your projects options box.You also need to put this file in your project folder. The program needs the file ) inpout32.dll to be able to work (to be able to have direct access to the parallel port).

About the example circuit. I used LEDs to reveal the port status. But an NPN transistor such as a 2N2222 can be used with a diode (between E and C for protection) such as a 1N4001 to drive a relay (sinking current from it) to control higher loads. You can see all this in the schematic. Necessary project files are below. click to download. You can drop your comments for a more detailed explanation :)


This article comes from Robotik & Elektronik

The URL for this story is: