How can I speed up my interrupt service routine?

You can improve the speed of an Interrupt Service Routine (ISR) by keeping the code contained in the ISR to a minimum. The more registers used by the ISR code, the more context save and restoration will be required and added by the compiler. In addition to the code you write in the ISR, there is the code the compiler produces to switch context. This is executed immediately after an interrupt occurs and immediately before the interrupt returns, so this time must be included in the time taken to process an interrupt. This code is optimal in that only registers used in the ISR will be saved by this code. Thus, fewer registers used in your ISR will mean potentially less context switch code to be executed.

Mid-range devices have only a few registers that are used by the compiler, and there is little context switch code. Even fewer registers are considered for saving when compiling for an enhanced mid-range device.

Generally, simpler code will require fewer resources than more complicated expressions. Consider having the ISR simply set a flag and return. The flag can then be checked in the main line code to handle the interrupt. This has the advantage of moving the complicated interrupt-processing code out of the ISR so that it no longer contributes to its register usage.

Use the assembly list file to see which registers are being used by the compiler in the interrupt code.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.