Tuesday, August 25, 2009
If you have built any of the interfaces on my circuits page and now want to know how to actually make use of them, this page is for you. This is a simple introduction to programming the parallel port in QBasic, QuickBasic or similar language. Note that most of the concepts in this page can also be applied to GWBASIC. If you are interested in using Visual Basic to control the port, see Programming The Parallel Port In Visual Basic. What this document will not do is give you lots of details on using bi-directional ports, DMA and other advanced topics. This document assumes that you are familiar with the basic functions of BASIC itself.
The parallel port is made up of three different sections. These are the data lines, control lines and status lines. There are 8 data lines, and they are the primary means of getting information out of the port. In simple projects, you will be concentrating mostly on the data lines. The control lines are another 4 outputs. They are meant to provide control signals to the printer (such as form feed or initialize). The status lines are a standard parallel port's only inputs. There are 5 of them. They were meant to allow the printer to communicate things such as error, paper out and busy to the PC.
Each section is accessed by it's own address and will act independently from the rest. This is almost as if they were different ports. The addresses are as follows:
Data Lines | ||
Control Lines | ||
Status Lines |
You need to know the address of the port you want to use. You will also need two other things; the command to access the port and the number you want to set it to. The command will be explained in a little while. The ports work with numbers. These can be expressed in hex, binary or decimal, but for this document all values will be expressed in decimal. It's just easier that way. Anyway, you operate the port by sending it a number that represents the binary pattern of the physical outputs on the port. For example, to set the 8 data lines to 11111111, you would send 255. To set them to 00000000 you would send 0. Note that these are all 8 bit binary numbers, and the port is also 8 outputs. Coincidence? I think not.
Now that we know how to tell the port what bit patterns we want, we have to actually apply that to the BASIC language. BASIC uses two commands to talk to the computer's ports. These are OUT
and INP
. OUT
is a statement and is used like the following:
OUT [port],[number]
We will get to INP
later. As you can see, the two parameters required are the port address and the value we want to set it to. The address can be decimal or hex, as can the value. Because there are only 8 data lines, we can only send a maximum of 255 to the port (255 is decimal for binary 11111111). The examples below illustrate sending a few different bit patterns to the data lines.
'set port to 00000000
OUT 888, 0
'set port to 10000000
OUT 888, 1
'set port to 01000000
OUT 888, 2
'set port to 00100000
OUT 888, 4
'set port to 00010000
OUT 888, 8
Of course, you can also turn on more than one bit:
'set port to 10110000
OUT 888, 11
Note that when you send a bit pattern to the port everything that was there previously is cleared. This is a convenience and also a annoyance. For example, what if we want bit 2 to always stay at 1, but want to turn bit 5 on and off in sequence? Every time we set bit 5, bit 2 is turned off, and vice versa. We will discuss how to get around this when we get to the INP
function.
The control lines are just as easy to control, but there are a few differences. First, the address of the port is 890. Second is that there are only 4 outputs, so the highest decimal representation of the binary bit pattern you will be using is 15 (binary 1111).
Outputting information is easy, and inputting is just as easy. If you actually want to get information into the computer, you will be using the 5 status lines. Reading the bit pattern of a port is done using the INP
function. This function is used in the following way:
[variable]=INP([port])
So if we wanted to get the current status of the status lines (port 889) we would use:
PortNum%=INP(889)
PortNum%
would then contain the decimal representation of the binary bit pattern present at the 5 status lines. If you try this and get 31 (11111) with nothing connected to the port don't be surprised. When there is nothing connected to the input of a TTL logic chip, a high input is usually assumed.
Not only can you perform inputs on ports actually designed for inputting, but you can also use INP
to read the status of an output port. For example:
PortNum%=INP(888)
The above would set PortNum%
to the current value of the data lines (port 888). We can prove this by doing the following:
OUT 888, 56
PortNum%=INP(888)
PRINT PortNum%
If all is well, the number 56 will appear on the screen.
Now that we know the INP
function we can use it to solve the problem of keeping the state of one bit while changing the state of another. For that we will define a subroutine that uses both functions:
SUB OutPort(PortAddress%, OutNum%)
PortState% = INP(PortAddress%)
PortNum% = PortState% + OutNum%
OUT PortAddress%, PortNum%