Texas Instruments MSC1210 User Manual

Page 124

Advertising
background image

Common Problems with Interrupts

10-18

10.11 Common Problems with Interrupts

Interrupts are a very powerful tool available to you, but when used incorrectly,
can be a source of a huge number of debugging hours. Errors in interrupt rou-
tines are often very difficult to diagnose and correct.

If you use interrupts and your program is crashing or does not seem to be
performing as expected, always review the following interrupt-related issues:

Register protection: Make sure all registers are protected, as explained
previously. Forgetting to protect a register that the main program is using can
produce very strange results. In the example above, failure to protect registers
caused the main program to apparently calculate that 25

H

+ 10

H

= 51

H

.

If registers start changing values unexpectedly or operations produce
incorrect values, it is very likely that the registers have not been protected.
Always protect the registers!

Forgetting to restore protected values: Another common error is to push
registers onto the stack to protect them, and then forget to pop them off the
stack before exiting the interrupt. For example, if you push ACC, B, and PSW
onto the stack in order to protect them, and subsequently pop ACC and PSW
off the stack before exiting, but forget to restore the value of B. you leave an
extra value on the stack. When executing the RETI instruction, the 8051 will
use that value as the return address instead of the correct value. In this case,
the program will almost certainly crash. Always make sure to pop the same
number of values off the stack as were pushed onto it.

Using RET instead of RETI: Remember that interrupts are always terminated
with the RETI instruction. It is easy to inadvertently use the RET instruction
instead. However, the RET instruction will not end the interrupt. Usually, using
a RET instead of a RETI will cause the illusion of the main program running
normally, but the interrupt will only be executed once. If it appears that the inter-
rupt mysteriously stops executing, verify that the routine is exiting with RETI.

Make interrupt routines small: Interrupt routines should be designed to do
as little as possible, as quickly as possible, and leave longer processing to the
main program. For example, a receive serial interrupt should read a byte from
SBUF and copy it to a temporary buffer defined by the user and exit as quickly
as possible. The main program must then handle the process of interpreting
the data that was stored in the temporary buffer. By minimizing the amount of
time spent in an interrupt, the MSC1210 spends more time in the main
program, which means additional interrupts can be handled faster when they
occur.

Advertising