Electronic and Computer Resources - Parallel Port Interfacing and Programming using Turbo C

## Sending a Byte to the Port

Lets have an example of sending a byte to the DATA port. Let say, we want to output high bits to pin2 to pin9 of the parallel port. Our code for this will be:

The address that we used here for the data port is 378Hex. This address may not always be the same to all computers. We output the number FFHex since we want to output all high bits or 11111111Bin to D0 to D7.

After the above statement will be executed, the following voltages can be read from pin2 to pin9 of the parallel port.

In the same way, if we want to output the bits 00001111 to the data port, we just convert the binary number to its hexadecimal equivalent and then write the following statement to the source C/C++ program:

Once this statement will be executed, the following digital voltages can be read from the port.

What should we write if want to output bits to the control port instead to the data port?

To output bits to the control port, the syntax is still the same but we will write 0x37A for the address.

The code above will send the bits 00001111 to the control port. But since, C2 is the only pin that is not inverted, the actual bits that we can read from pin17, pin16, pin14 and pin1 are 0, 1, 0, and 0 respectively as shown in the figure below.

The number that we write in our code is not actually the number that can be read from the port. So, if we really want to have 1111 bits to the control port, we should output the bits 0100. Our code will now look like this :

Another way of solving this problem is to use XOR (^) operator. We will XOR the number that we want to output with 00001011Bin or 0x0B . XORing a bit with 1 will cause the bit to be inverted.

This will simplify the taxing job of inverting some bits.

In reading a byte from the printer port we will use the function inportb() of the Turbo C language. The syntax for reading a byte is:

For our examples, we will use 0x379 for the address of the status port. The status port is an input port of the printer port. An actual example of a Turbo C statement that reads the bits of the status port is given below:

We use input as our variable. Executing this statement will cause the bits in the status port to be read and store it to variable input.

If for example, the pins of the status port is connected to a +5 volts supply as illustrated below.

Executing the statement,

in the program will cause the bits from S7 to S3 (1111 1) to be stored in the input variable. But since, S7 is inverted, the bit that will be stored in the MSB of the input variable is 0. So, the bits that will be actually stored in the input variable are 0111 1xxx. X stands for unknown bits since S2 to S0 is not available in the status port and it is neither connected to any voltage. But in TTL standard, input pins that are not connected is high or 1. Therefore, the number that can be stored in the input variable is actually 0111 1111 in binary or 7F in hexadecimal or 127 in decimal.

Contents

Parallel Printer Port

Electronic and Computer Resources - Parallel Port Interfacing and Programming using Turbo C

For questions, corrections and suggestions,

email to: [email protected]

Hosted by www.Geocities.ws