0
\$\begingroup\$

On internet can seen countless simple example like the one below

void __interrupt() isr()
{
  
if(TMR1IF)
   {Do something}

}

But does it work on self-defined variables? As an example, define a bool CurrentMeasurementReady. I can't find similar examples to the one below.

void __interrupt() isr()
{
  
if(TMR1IF&&CurrentMeasurementReady)
   
{Do something}

}
\$\endgroup\$
2
  • \$\begingroup\$ I would need to know the specific compiler you are using. Keil, for example but not limited to Keil by any means, can allow the linker to analyze a call tree used to overlay local variables and function arguments into static memory. So how one might achieve what you want (which I can't say you've described well enough that I feel I have precise knowledge) could vary a little. But you need to talk more about what your goal is before I can consider an answer. I see an added variable name. But I don't know if you meant one that has local scope and lifetime, by intention, or static/heap lifetimes. \$\endgroup\$
    – jonk
    Commented May 15, 2022 at 5:14
  • \$\begingroup\$ @jonk XC8 is the compiler I using and the "CurrentMeasurementReady" is a globalvariable. \$\endgroup\$
    – chuackt
    Commented May 15, 2022 at 5:26

1 Answer 1

3
\$\begingroup\$

I believe what you are asking about is the scope of variables in the interrupt service routine (ISR).

If CurrentMeasurementReady a declared inside the ISR then it’s scope will be limited to that function, and it will potentially be reinitialized every time the function is called. If CurrentMeasurementReady is declared globally then everyone has access to it and its value is persistent.

Do note that when you’re sharing global variables between the ISR and any other functions you must declare that variable as “volatile”, otherwise the compiler will optimize away certain operations and you will NOT enjoy debugging that. Eg:

volatile bool CurrentMeasurementReady;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.