Sie sind auf Seite 1von 3

Interrupt Use Best Practices

http://www.embedded.com/design/programming-languages-andtools/4397803/Interrupts-short-and-simple--Part-1---Good-programmingpractices
Controller must be divided among tasks such that they seem to be executed
in parallel
Must decide the background/main process, regular interrupt for other tasks,
and asynchronous interrupts for communications events

Considerations
o Decide main process
Main has the lowest priority since any interrupt can disrupt it
o Prioritize interrupts
Important, urgency, and frequency decide priority
Decide if asynchronous or synchronous operation is
desired
o Keep ISRs short
ISRs should only execute critical code (not calculations for
example)
The rest of the task can be given to the main process by setting
flag variables
Flag variables are global variables, which can be bits
o Use finite state machines
When the function of the ISR depends on the state
Define can be used instead of variables to make code
readable/fast (C style)
Static state variables (set by the main process) can be declared
inside for speed
If using switch with default set if there are more than three
conditions
o Know when global variables are modified or read
Global variables should only be modified in one place
Global variables should only be read in one place in the main
process
Disable and then re-enable interrupts when reading more bits
than the width of the CPU (16-bit)
Consider that if multi-width variables are not read right away
(before a new conversion), they may be corrupted by combining
old and new readings
1: Ensure that new readings are not completed before
ISRs are served
2: Disable the peripheral interrupt before reading and reenable
3: Clear the ADC interrupt and wait for new data
o Understand local variables
Avoid nesting interrupts as this may overflow the stack to avoid
corruption
Some compilers overlay variables when there are not common
variables, saving all variables to a fixed location, overwriting
them, instead of the stack
Use static keywords on local variables of ISRs if ISRS
Use a volatile keyword in front of global variables
Use a lower level of optimization that will disable
overlaying
Use a different dataspace for ISR local variables

o
o

Make sure that ISRs do not use more memory than the
register bank used by the compiler if the compiler uses
register banks instead of RAM
Make sure buffers are read at once by disable/re-enable
Do not allow communications buffers to overflow
In I2C, add new bytes received, but check for capacity, using
ACK if full
Check endian, padding of variables smaller than the processor width,
and packing
Do not use too many functions in an ISR and use macros instead to
conserve CPU time
Use define, \ for new lines, and curly braces as usual
Functions can be used inside macros, but for this project, only
use macros to change registers quickly

Das könnte Ihnen auch gefallen