cancel
Showing results for 
Search instead for 
Did you mean: 

do i need to use a general purpose register inside an interrupt?

deep_rune
Associate III

my interrupt code needs to make a decision based on a flag, which I stored in a global variable. However, the code wont compile as it doesn't recognise the variable inside the interrupt, but does everywhere else. Is this normal?

If it is normal, is there a general purpose register I can use for this information instead? I can't seem to find it in the reference manual

5 REPLIES 5

Is the variable defined in the same file as the interrupt service routine (ISR), and before the ISR?

JW

LMI2
Lead

volatile

S.Ma
Principal

say your variable is:

==> main.c ?
volatile uint8_t flag;
... code setting the file
 
==> Interrupt.c
 
extern volatile uint8_t flag;
 
void callback_ISR(void) {
 if(flag) // do something
 

The compiler may not have the declaration of the variable in a specific header file, so try to extern is where needed to see if the visibility is improved

>>my interrupt code needs to make a decision based on a flag, which I stored in a global variable. However, the code wont compile as it doesn't recognise the variable inside the interrupt, but does everywhere else. Is this normal?

No, you're doing something abnormal, perhaps an issue of scope, or you having functions/variables in different source files?

As pointed out, best to mark things which can change outside of normal code flow as volatile.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Piranha
Chief II

Variable scope is C basics and has nothing to do with STM32 particularly. One must be able of figuring this out on their own, otherwise it'll be impossible to develop anything anyway.

https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files