of the compiler’s __PARAMx variables, we can use the
NOPRESERVE keyword so that those variables — which
aren’t changed during the ISR — are not saved and restored;
this just wastes time if we don’t need it to happen.
The first VP encountered decrements the delay timer if
it’s running. Remember, PAUSE will not work properly with
the ISR so we’ll have to use a custom subroutine to handle
that — we’ve done that in the last couple projects so I’m
sure it’s old hat by now.
Next up is the sync timer. It will be incremented each
time through the ISR and if it goes past the required idle
time bit count (20), a flag will be set that will allow the
foreground process to receive serial data. What you’ll see
up ahead is that this flag and its control timer get cleared at
the end of a received byte; this forces the program to ignore
serial data for two byte periods after the last packet byte has
been received.
And now for the receive UART. We’ve actually used
this code a couple times before (see the serial seven-segment display project from January 05 for a detailed
description). Briefly, the receive line is sampled until a
start bit is detected. When this happens, the receive
bit counter is loaded and the timer decremented. When
the start bit timer (which is 1.5 bits long so that sampling
starts in the middle of the first bit) expires, the line is
sampled and the bit timer reloaded with one bit
period. After all bits have been received, the rxReady flag bit
is set and the armed flag and packet sync timer are reset.
Receive:
ASM
JB rxReady, RX_Done
BANK serial
MOVB C, RX
TEST rxCount
JNZ RX_Bit
MOV W, #9
SC
MOV rxCount, W
MOV rxDivide, #Baud1x5
RX_Bit:
DJNZ rxDivide, RX_Done
MOV rxDivide, #Baud1x0
DEC rxCount
SZ
RR rxByte
JNZ RX_Done
SETB rxReady
CLRB armed
CLR syncTimer
RX_Done:
BANK 0
ENDASM
■ FIGURE 2.
Outputs schematic.
For those who might be new
to SX/B, you can see how easily
assembly code is integrated
into the program through the
ASM..ENDASM block. And to
be honest, I didn’t create this
code; I “liberated” it from Al
Williams’ SX programming book
Exploring the SX Microcontroller
(available as a free PDF from
Parallax). As good as SX/B is, there
will be times when inserting some
assembly code will be the best
development choice — it’s nice
that SX/B makes it so easy.
Another great source of assembly
routines is Günther Daubach’s
excellent book Programming the
SX Microcontroller. And don’t
forget James Newton’s SX List
( www.sxlist.com).
Next up is the transmit UART,
something we haven’t used in
the past, but as with the receive
UART, this was lifted right out of Al’s
book.
■ FIGURE 3. Control packet.
20
May 2007