The Light-byte:

Our first breadboard experiment will be to light a block of 8 LED's, so that we can see the values of all 8 bits in the byte that is sent to the parallel port-- basically the same as the Lit-bit, only times 8.  We could certainly do this with 8 separate LED's and 8 individual resistors, but this would not only take a lot of time, it is harder to keep 16 separate parts nice and neat on the breadboard.  Keeping your parts and jumpers as neat as you can is extremely important to avoid making mistakes.  And so we will use a trick-- a single part which has 10 LED's in it (2 will be left unused) and another that has 9 resistors (1 unused).

Since the 8 data bits from the parallel port ride on wires that are lined up next to each other (#2-#9), this makes for a very efficient layout of parts on our breadboard:

 
Light-byte before...                    and after connection to the parallel port.
 
Remember to be particularly careful to make sure that the 26-hole connector goes onto the 9+2 pins sticking up from the breadboard!

Now, lets trace out the circuit's paths:

So each bit wire (#2-#9) is connected to ground through the LED-resistor combination-- just like we did with the Lit-bit.
 

To test this out, we need to write a program:

First lets start with blink1.bas-- open this program (that you saved previously) in QBASIC--

    CLS
    INPUT "enter count value for speed control: ", speed
    FOR counter = 1 TO 10
    OUT 888,1
    FOR zz = 1 TO speed: NEXT
    OUT 888,0
    FOR zz = 1 TO speed: NEXT
    NEXT

Go ahead and run it.  You should see LED 0 blinking.  Now how can we get the other LED's to blink?  Well there are lots of ways-- but probably the simplest is to change OUT 888,1 to OUT 888,255Try it!  Hopefully this should start making sense, since 255 is equal to 11111111 (all bits on) in base 2.

It's nice to see all our 8 light-bits working, but this is not much more interesting than a single blinking LED.  Remember that we can turn on and off each bit independantly of its neighbors.  So let's create a program that blinks each LED in succession.  We'll call it march1.bas:

    CLS
    INPUT "enter count value for speed control: ", speed
    FOR counter = 1 TO 10

    OUT 888,1
    FOR zz = 1 TO speed: NEXT
    OUT 888,2
    FOR zz = 1 TO speed: NEXT
    OUT 888,4
    FOR zz = 1 TO speed: NEXT
    OUT 888,8
    FOR zz = 1 TO speed: NEXT
    OUT 888,16
    FOR zz = 1 TO speed: NEXT
    OUT 888,32
    FOR zz = 1 TO speed: NEXT
    OUT 888,64
    FOR zz = 1 TO speed: NEXT
    OUT 888,128
    FOR zz = 1 TO speed: NEXT

   NEXT

Go ahead and try it.  You should see a nice march of LED lightings repeated 10 times. (Why is it unnecessary to have  OUT 888,0 statements to turn the LED's off after they have been on? -- hint: take a look at the bit values in this table)

When writing a program, there are almost always many ways to accomplish the same task.  Often, the goal of the programmer is to write code which is efficient-- meaning that the task is accomplished using the least lines of code.  March1.bas does it's job OK, but are there other ways?  How about march2.bas:

    CLS
    INPUT "enter count value for speed control: ", speed
    FOR counter = 1 TO 10

    OUT 888,2^0
    FOR zz = 1 TO speed: NEXT
    OUT 888,2^1
    FOR zz = 1 TO speed: NEXT
    OUT 888,2^2
    FOR zz = 1 TO speed: NEXT
    OUT 888,2^3
    FOR zz = 1 TO speed: NEXT
    OUT 888,2^4
    FOR zz = 1 TO speed: NEXT
    OUT 888,2^5
    FOR zz = 1 TO speed: NEXT
    OUT 888,2^6
    FOR zz = 1 TO speed: NEXT
    OUT 888,2^7
    FOR zz = 1 TO speed: NEXT

   NEXT

OK, it's not more efficient.  But it is an improvement because it is easier to see the "marching" of the bit numbers (0 to 7).  This counting of exponents suggests another idea-- march3.bas:

    CLS
    INPUT "enter count value for speed control: ", speed
    FOR counter = 1 TO 10
    FOR bit = 0 TO 7
    OUT 888,2^bit
    FOR zz = 1 TO speed: NEXT
    NEXT bit
    NEXT counter

Ahh, much better.  Now we are using two loops-- one within the other.  The inner loop steps the exponent called "bit" through the values 0 to 7 (turning on the corresponding LED's as it does).  The outer loop simply repeats this process 10 times, as we have seen before.

Although the Light-byte may be simple, you may be surprised by how many interesting and different ways you can sequence the LED's.  Keep in mind that for simplicity, all the examples thus far have kept the same timing for each LED, but this need not be so.  Try out a few of the examples that are provided.  As always, you will learn far more by trying to create new programs by modifying the examples.

Of course, our Light-byte software can be used to control different hardware than just LED's...



next-- Geyser-bot and Pipedream
home
Copyright © 2000 Bruce Shapiro