Step 3 : Program Code
A Basic program can be used to drive the system. An array of bytes would be used to store the port settings, where each byte represents 8 bits on one port.
- To turn on the second output on chip 3 you might execute the following Basic statement. (ie to set the data so it will get turned on when you do the shifting):
array(3) = array(3) or &B01000000
- To turn on outputs 1, 4 and 7 and turn off the others on chip 5 you might execute:
array(5) = &B10010010
- To shift the new data out to update all ports you would execute:
for i = 1 to 8 ' 8 bytes
bitpattern = array(i) ' get one byte
for j = 1 to 8 ' 8 bits per byte
out &H378,bitpattern and 1 'output rightmost bit
out &H37A,clockhi ' clock it through shift registers
out &h37A,clocklo
bitpattern = bitpattern / 2 ' shift bits right
next
next
out &H37A,latchhi ' latch new data into all outputs
out &h37A,latchlo
clockhi/lo and latchhi/lo are variables that contain the data to set the relevant pin hi/lo (see below).
A parallel port has 24 pins. A byte only has 8 bits. To allow you to access all necessary pins the computer splits up the parallel port into multiple logical "ports". Each port is simply an address in memory that you send data to. We are interested in the "data" port and the "control" port.
To send data to the data port you send a byte to address 378 hex. To send data to the control port you send a byte to address 37A hex.
The pins that the data port &H378 controls are as follows:
PINS 9 8 7 6 5 4 3 2 (ONLY 2 IS USED FOR THIS APPLICATION)
ie. if you execute: out &H378,5 (binary 00000101) then pins 4 and 2 change from 0v to 5v.
The pins that the control port (&H37A) controls are as follows:
PINS - - - - 17 16 14 1 (ONLY 1 AND 14 ARE USED FOR THIS)
Pin 14 is used for clock and pin 1 is used for strobe to latch the data. But note that those two pins are inverted. ie. you output a 0 to get 5v and vice versa.
When you toggle clock high/low you have to keep latch low. and when you toggle latch high/low you have to keep clock low.
So when you work it all out and invert the bits, you end up with the following variables:
CLOCKHI = 1 (Binary 00000001)
CLOCKLO = 3 (Binary 00000011)
LATCHHI = 2 (Binary 00000010)
LATCHLO = 3 (Binary 00000011)