Keil Logo


Information in this article applies to:

  • C166 Version 3.12
  • C166 Version 4.01
  • C251 Version 2.14
  • C51 Version 5.50


I don't want an ISR to destroy the contents of variables in the main program that it is interrupting.  Can I declare variables inside an interrupt routine or do all the variables that the ISR uses have to be global variables (declared outside the routine)?  

Also, what about subroutines that the ISR calls? Can they have their own local variables? (assuming the subroutines use the same register bank, of course).


Interrupts may have local variables just like normal functions. Higher levels of optimization in the compiler will move variables automatically into the registers, thus saving them when an interrupt triggers.

Use the following techniques to prevent "corruption" of your DATA space by interrupts.

  1. Use the static keyword or global variables on your interrupt variables, to allocate a space for them separate from the rest of the program, or employ the static keyword on essential variables in your functions.
  2. Use a lower level of optimization which will disable variable overlaying. You can find details on this in your manuals.
  3. Use XDATA for your "normal" locals, and DATA for your interrupt locals. This can be done either by using memory specifiers in your variable declarations or using different memory models for the different files. Details on both of these approaches can be found in your compiler manual.
  4. Make sure the memory usage of your interrupts is not greater than the registerbank it's using when it "fires".



The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Tuesday, October 24, 2006

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.