ARM Technical Support Knowledge Articles

INTERRUPTS AND LOCAL VARIABLES

Applies to: C51 C Compiler

Answer

Information in this article applies to:


QUESTION

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).

ANSWER

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".

MORE INFORMATION

Article last edited on: 2006-10-24 11:44:12

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential