This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Variable declared as volatile changing value in ISR

Other Parts Discussed in Thread: MSP430F2274

Hello,

I'm using MSP430F2274 and I would like to have a variable that is controlled in timer A0 ISR. The variable should take only three values - 0, 1, and 2 with each interrupt of timer A0. Instead it is getting other values and the program gets stuck. I found this with the debugger but I think this is what happens when the micro-controller runs on its own. Is the low power mode causing this problem? How can I change the code so that the variable doesn't change and the controller still goes to LPM1 between TimerA interrupts. Any advice would be highly appreciated.

This is the code that I think is relevant:

...

volatile int pulse_state = 0;

void main (void)
{

...

}

#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A0 (void)

{

    switch (pulse_state)

      { 

case 0:

...

pulse_state = 1;

__bis_SR_register(LPM1_bits + GIE);
break;

case 1:

...

pulse_state = 2;

__bis_SR_register(LPM1_bits + GIE);
break;

case 2:

...

pulse_state = 0;

__bis_SR_register(LPM1_bits + GIE);
break;

    }

}

  • Maybe I am mistaken but it seems that you are returning to low power mode from inside the interrupt? So the return interrupt doesn't execute and eventually your stack overflows?

  • The code, as you posted it, won't change piulse_state to anythign other than 0,1 or 2.

    However, your program will crash with a stack overflow soon.

    You enter LPM from inside the ISR, which leaves the return address of the ISR on the stack as well as any registers that have been saved. And sicne the only ISR that will wake from LPM is the ISR itself, it will never return. So after a few stacked interrupt calls, teh stack hwil grow larger than your ram, overwriting all variables (including pulse_state) with return values and register saves until it starts overwriting the system registers and finally resetting the device by access violations.

    Never ever go into LPM inside an ISR. Never. Under no circumstances.
    And never set GIE inside an ISR except you know exactly what you're doing, This is for very, very experienced coders only.

    When an ISR is called while the processor is in LPM, it will fall back into LPM automatically once the ISR exits. Except if the ISR calls the intrinsic to exit LPM on ISR exit.

**Attention** This is a public forum