3 Gordon Drive, P.O.Box 1347 Rockland, Maine 04841 U.S.A.
Find Tools for Your Chip


Subscribe to our Newsletter

© 2004 Avocet Systems, Inc.
Call Us Today at 207-596-0080
Avocet Systems, Inc. : The Complete Solution for Embedded Systems Development Tools
Tech Tips
Proper Use of Interrupts

It goes without saying that there are many good reasons to use interrupts in your embedded development code. It's important however, to have your project fully thought-out before you write code using interrupts. It's easy to try to do too much under interrupt, but it's also possible to do too little. Either situation can lead to bugs which won't show up until your product is in the field.

Step one to reaching 'interrupt balance' is to prioritize your application's goals. For example, receiving all characters arriving on the serial port may be important, but it may be absolutely essential to sense each pulse on a tachometer or timer. Weighing the relative importance of each task is essential to the efficient use of interrupts.

The next issue to consider, is which task(s) will be controlled with that critical interrupt. For instance, you may need to count the pulses, flash an LED, transmit a message every 20 pulses, and fire a relay every 50 pulses. The best solution in this case, would be to count the pulses and fire the relay within the interrupt, and let the background loop take care of the transmit and LED.

For serial port interrupts there are also different levels of support you could provide. For instance, you could simply count the interrupts and receive characters in the background. This way you'll keep informed if you miss a character. Usually your best bet is to receive and queue the characters within the interrupt, and process the messages in the background. Whatever you do, don't try to receive AND process the serial data in the interrupt routine. You should never spend that much time in an interrupt.

Finally, avoid doing too much in the background. If you spend too much time calculating, and locked in wait loops in background, you may not check on the data queued up from the interrupts often enough. If your average trip through the background gets too slow, try adding a high-priority background that you pass through twenty times before taking on the slow stuff again.

In conclusion, always make good use of your interrupts in your embedded project by not wasting them on non-critical tasks.