Stamp
instruction cycle (bit 5=0), and set the divide rate for
the RTCC to 1:1 (bit 3=1). The SX28 documentation
(download from Ubicom — www.ubicom.com) goes into
all the details of the OPTION register.
Okay, now that we’ve enabled interrupts, how do we
make that happen at the desired interval of 26 microseconds? Here’s what an empty ISR block looks like in SX/B:
have to reduce the oscillator speed or enable the RTCC
prescaler.
At this point, our program will be interrupted every
26 microseconds — a rate that we’ve determined is
fast enough to sample the serial input line enough to
accurately capture data at 9600 baud Okay, let’s do it.
INTERRUPT
‘ ISR code
RETURNINT 104
The key is actually at the end — the value following
RETURNINT. This tells the SX how many cycles to run
before generating an interrupt. How then, did we come
up with 104? We start with the clock frequency of our
project: 4 MHz.
At this rate, each instruction cycle takes 0.25
microseconds. Since we want our interrupt to trigger
every 26 microseconds, we divide the instruction cycle
speed into that. So, 26 divided by 0.25 is 104. This works
because it is less than 255 (the maximum value of the
RTCC). If you’re ever doing a project where your
interrupt cycles calculate to greater than 255, you either
F
o
r
E
l
e
c
t
r
o
n
i
c
s
NUTS & VOLTS
E
v
e
r
y
t
h
i
n
g
ISR_Start:
ASM
BANK $00
MOVB C, Sin
TEST rxCount
JNZ RX_Bit
MOV W, #9
SC
MOV rxCount, W
MOV rxTimer, #BitTm15
RX_Bit:
DJNZ rxTimer, Multiplex
MOV rxTimer, #BitTm
DEC rxCount
SZ
RR rxByte
SZ
JMP Multiplex
RX_Buffer:
MOV FSR, #rxBuf
ADD FSR, rxHead
MOV IND, rxByte
BANK $00
INC rxHead
CLRB rxHead.4
ENDASM
78
Even though SX/B allows high level code in the ISR,
we’re not going to do that for the serial input. Why not?
Well, there are two reasons: With assembly language, we
can be a tiny bit more code-efficient and — even more
importantly — the code was already written and working,
so why not just use it?
Let me pause for a second and suggest that, if you’re
serious about programming the SX, you should consider
the books that Parallax makes available: Exploring the SX
Microcontroller by Al Williams (no, we’re not related, but
he lives in TX, too) and Programming the SX
Microcontroller by Guenther Daubach. Both authors are
great guys and very active in the Parallax support forums.
You can get an SX starter kit that includes both books and
— if you’re on a budget — Al’s book is available as a free
PDF download.
Okay, back to the code. On entering the ISR, we want
to make sure that we’re pointing at the serial variables, so
we issue a BANK $00 statement to do that. Then, we
sample the serial line by copying it into the SX Carry bit.
When the serial line is idle, the Carry bit will now hold a
value of 1. Let’s continue to go through the code as if
we’re in the idle state. The TEST instruction will set the
Zero bit if the register tested holds a value of zero. In our
program, the variable rxCount is used to count down the
JANUARY 2005