The beauty of the bintoascii
command is that it automatically
converts each of these digits to the
corresponding ASCII code, which is
exactly what we need for a serial
transmission. However, we’re not
there yet; we just want the LED
display board to count from 0 to
9999, with no serial transmission
involved. In order to get it to do that,
we need to convert each digit back
from ASCII to the digit itself.
Fortunately, that’s an easy task. The
ASCII code for “0” is 48, the code for
“1” is 49, the code for “ 2” is 50, etc.
In other words, the ASCII code is
always greater than the digit itself by
exactly 48. All we need to do is to
subtract 48 from each bintoascii
argument to get back to the value of
the digit itself:
bintoascii counter,
tths,thos,hnds,tens,ones
thos = thos - 48
hnds = hnds - 48
tens = tens - 48
ones = ones – 48
Note that we don’t need to
convert the tths variable back from
ASCII because we aren’t going to
need it on a four-digit display. Once
we have converted the four digits
that we will be using, we just send
each one of them to the
corresponding LED on our display:
maxreg = 1 ‘first LED from
outbyte = thos‘left (thousands)
gosub shout
maxreg = 2 ‘second LED from
outbyte = hnds‘left (hundreds)
gosub shout
maxreg = 3 ‘third LED from
outbyte = tens‘left (tens)
gosub shout
maxreg = 4 ‘fourth LED from
outbyte = ones‘left (ones)
gosub shout
The above code snippets are all
we need to add to our
LED7219help.bas program (we used
this in the previous Primer to test the
LED display) to convert it into a
counting program. The resulting
program ( LED7219Count.bas) is
available on the N&V website at
www.nutsvolts.com. Download it,
along with the three other programs
that we will be using this month:
LED7219CountZB.bas,
LED7219Driver.bas, and
LED7219Test.bas, and try it out. To
program the on-board 08M, you will
need the same four-pin programming
adapter that you used last time.
When you install and run
LED7219Count.bas on the LED
display, you should see it count from
0 to 9999; of course, you will also
see all the “leading zeros” displayed
as well (e.g., “ 27” is displayed, as
“0027”). Since this is not the
optimum format for displaying a
number on LEDs, let’s see what we
can do to improve the program.
ZERO-BLANKING
In order to get the superfluous
zeros to not appear on the display,
we’ll need to do some if-then type
testing to determine whether or not
to “blank” a zero. Let’s start with the
simplest digits and work our way up
to the harder ones. The ones digit
doesn’t require any testing at all; a
zero in that position should never be
blanked. The thousands digit is
almost as simple because a zero in
that position should always be
blanked on a four-digit display. The
remaining two positions (hundreds
and ones) are more complicated
because sometimes we want to blank
a 0 in either (or both) of these
positions, and sometimes we want to
display it.
Again from last time, you may
recall that the BCD decoding schema
of the 7219 includes a value of 15 to
display a blank on any of the LEDs.
Therefore, our little “zero-blanking”
project requires that for each of the
digit positions except the ones digit,
we include the necessary if-then test
on the relevant variable. If its value is
0 and it should be blanked, we need
to change its value from 0 to 15 so
that the digit will be displayed as a
blank, not a 0. Before you read any
further, you may want to experiment
with the LED7219Count.bas program
to see if you can add the necessary
if-then statements to correctly zero-
PICAXE PRIMER
blank the LED display. (Actually, you
can read further if you want — the
answer isn’t in the Primer anyway; it’s
in the LED7219CountZB.bas program
on the N&V website.) See if you can
develop the necessary code to
successfully zero-blank the LED
display. If not, take a look at the
LED7219CountZB.bas program; it
contains one possible solution to
the problem.
CONFIGURING THE
LED DISPLAY AS A
STAND-ALONE SERIAL
PERIPHERAL
When you’re sure you
understand the programming
involved in zero-blanking, we’re
ready to move on to our goal of
configuring the LED display as a
stand-alone serial peripheral that we
can use with any PICAXE project. The
necessary driver software is actually
very similar in structure to the driver
program we used for our LCD display
a few months ago. In the main
program loop, we need to wait for
the master processor to serially send
a four-byte data string. Once the
string is received, it’s a simple matter
to display each byte on the
appropriate LED. The driver doesn’t
need to worry about zero-blanking or
anything else; in effect, our little LED
display is just a dumb output
terminal. The master processor is
responsible for zero-blanking when
it’s needed. All the LED driver does
is dutifully display the four characters
it receives.
Note that I said “characters.”
There’s no reason the master
processor can’t send “HELP” or
“HOHO” or whatever. If you want
even more flexibility, you could turn
off the 7219’s BCD decoding and set
up a lookup table to display even
more characters. (I’ll leave that one
for you as a little programming
challenge!)
If you do decide to modify the
driver software (or the 28X1 test
program we are about to discuss),
be sure to remember that there are
essentially four things upon which
April 2010 17