This task is designed to be run every 10 milliseconds
and will update all of the registers in the software RTC.
And, like the DS1302 and DS1307, it uses BCD (binary
coded decimal) registers. Note that on entry there is a
BANK @clock instruction. This is telling SX/B to point to
the clock bank instead of the default (general-purpose)
RAM bank. What this means, then, is that we can treat the
clock variables like regular variables. That said, we cannot
use regular program variables here without aliasing them
(with the __RAM() array).
I’m always on the hunt for neat tricks and this task
uses one that helps us to create a BCD RTC (say that
three time, very fast). Why BCD? Well, it lets you move
the values to displays without the use of decimal division
which can, in fact, chew up a fair bit of code space.
If you look at the BCD_RTC task code, it is built of
four identical sections, so we’ll just look at the hundredths
register. The first part is easy: We use INC to bump the
value by one. What we want to check for now is if the
lower nibble of the register moved from $9 to $A ( 10).
There are many ways to do this but an elegant solution is
to add six to the register. Here’s why this is cool: If the
lower nibble does now contain $A then adding six will
cause the lower nibble to become zero and the upper
nibble to be incremented by one ($A + $6 = $10). This
is exactly what we want. There is a bit in the SX’s status
register called DC (digit carry). After adding six to the
register, we’ll have a look at the DC flag. If this flag is not
set (1), then there was no carry between nibbles and we
need to remove the six we just added.
At first blush, this may seem like a bit of work but
it is, in fact, the most efficient method I’ve found for
incrementing a BCD register. After the increment and
digit-carry checks, we examine the register to see if it has
reached its limit. If not, now the task exits. Otherwise, the
register will be reset and the code drops through to the
next register update/check. There is a critical step in this
task at the very end — note the lone BANK instruction.
This causes SX/B to point back to the default RAM bank.
We need to do this for the rest of the program to operate
correctly. One last note on the clock() array.
If we’re not pointing to the clock bank as above, then
we need to access values though the array itself, like this:
IF clock(R_SECS) = $10 THEN
TASKS STOP, 1
With this little bit of code, the RTC task will be
stopped when the clock seconds register reaches $10.
Note how we’ve created a constant called R_SECS that
makes using the clock() array obvious; obvious is always
good. We can also suspend a task which allows it to
restart after a pre-determined break; we’ll get into
that in a future article.
NEW TEMPLATE — PLEASE USE IT
Finally, I have included an updated SX/B template
in the download file on the Nuts & Volts website at
www.nutsvolts.com. Please install this in your SK-Key
IDE templates folder. The new template is better
organized, especially with the location of the PROGRAM
Start directive. With tasks, the interrupt code grows
quite a bit and by moving SX/B’s internal start-up
code (which is placed at PROGRAM Start) after the
subroutine, function, and tasks declarations we are
less likely to have those declarations pushed out of the
lower half of the code page — a situation that will cause
I know we’ve just scratched the surface of these
topics but this is still some pretty powerful stuff. In the
coming months, we will continue to explore and exploit
SX/B’s cool new features. As ever, I encourage you to
play, and if you’re willing, participate! SX/B 2.0 is what it
is because Parallax and compiler engineer, Terry Hitt, are
open to making it better for all of us. You, too, can have
a say in what comes next. How many products do you
get to do that with?
GO FORTH AND BE PROSPEROUS
An interesting thing happened with a few of my
recent columns. I have been contacted by some readers
vis-à-vis quasi-commercial ventures with the project
designs. A gentleman named Andrew wanted to provide a
kit based on the VEX decoder article (see Resources),
another suggested a commercial version of the Brake
Light Buddy, albeit with some enhanced features, and yet
another guy thought he’d do a bulk-buy to save himself
and others a little “scratch” on the BLB PCB.
For the record, if you think you can make money or
help others with what you’ve learned from my articles
and the code/files that I provide for them, God bless you
and please go forward. Just understand that beyond these
articles — which I absolutely love doing, especially when
I hear your success stories — I can’t be involved; my time
is just too limited. I hope this doesn’t seem harsh or
unkind, and I do appreciate your understanding.
Until next time, then, Happy Stamping! — SX/B 2.0
VEX DECODER PCB / KIT
November 2008 25