Page 208 - Programming Microcontrollers in C
P. 208
Timers 193
its tracks, but the background operations being serviced by the inter
rupts will continue to be processed unhindered. In this case, a global
variable has been used to transfer data between an interrupt service
routine and the applications portion of the program. The variable
time is set in the applications program, and the completion of the
time delay is evaluated in the applications portion of the program,
and time itself is decremented in the interrupt service routine.
It is possible to create errors in operation with this type of proce
dure. One place where a nasty bug can creep into your code is when
you are dealing with bit manipulations in both the application por
tion of the program and the interrupt service routine. Suppose that
you want to toggle a bit when an event was detected in the applica
tion, and simultaneously you need a periodic bit toggle that is
controlled by code in the interrupt service routine. The code sequence
might appear as follows:
.
.
PORTA.BITAPP = !PORTA.BITAPP;
.
.
In the interrupt service routine, the code could be
.
.
PORTA.BITINT = !PORTA.BITINT;
In each case, this code will compile into
lda PORTA
eor 2^BITNUMBER
sta PORTA
The same code sequence will appear in both the application and
the interrupt service routine with the BITNUMBER appropriately cho
sen. Suppose that we are in the application, and have just executed
the lda PORTA instruction when the interrupt occurs. In the inter
rupt service routine, the above code will be executed properly, and
when control is returned to the program main line, it will continue
with the eor instruction. However, the contents of PORTA will have