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.

Using Interval timer (watchdog) to turn LEDs on and off - MSP430f2131

I want to use the watchdog timer to blinks some LEDs on our board. I have set it up and it works fine, except I want the LEDs to blink under certain conditions. I have declared a global variable to be used within the watchdog interrupt to tell when to blink the LEDs and when not to blink the LEDs. My problem is that I can not change the variable after the watchdog timer has been initialized. Actually the variables can be changed within the interrupt routine, but not outside the routine. I sthis true?

  • Hi,

    If you declare the variable as global you should be able to modify it anywhere in the code. Could you share the code?

    Bes regards,

    AES

  • Hi,

    This is in main *************************************************************************

      FA_Blink = FALSE;
       
      delay(small);
      MSP_Init();
      WDTCTL = WDT_MDLY_32;     // Set Watchdog Timer interval to ~30ms
      IE1 |= WDTIE;             // Enable WDT interrupt
      _BIS_SR(GIE);             // Enable interrupt

      FA_Blink = TRUE;

    while (8)
    {
    }

    ************************************************************************************

    watchdog interrupt****************************************************************

    #pragma vector=WDT_VECTOR
    __interrupt void watchdog_timer(void)
    {
      timer_tries++;
      if(timer_tries >= 10)
      {
        if(BLINK_On)
        {
          //turn it off
          if (FA_Blink)
          {
            sbi(LED_write, FA_LED);
          }           
          BLINK_On = FALSE;
        }
        else
        {
          //turn it on
          if (FA_Blink)
          {
            cbi(LED_write, FA_LED);
          }           
          BLINK_On = TRUE;
        }
      Write_Epld_GigaMediaLinX(0x05, LED_write); 
      timer_tries = 0;
      }
    }

    *************************************************************************************

    Here are the global defines ******************************************

    typedef unsigned char BOOL;
    #define TRUE 1;
    #define FALSE 0;

    BOOL FA_Blink;
    *************************************************************************

    During initialization I define FA_Blink as FALSE - no blink.  Then initialize the watchdog timer. Then change FA_Blink to TRUE, but I do not get the LED to blink. Now if I reverse the sequence and Init FA_Blink to TRUE, init the watchdog and then change FA_Blink to FALSE. I do get the LED to BLINK.

    Let me know if there is anything you need to know.

    Thanks, John

     

  • On first glance, it seems you're doing things right.

    But there is something non-obvious: teh compiler does not know about LPMs and interrupts.
    So it doe snot knwo that out-of -a-sudden, some code elsewhere is executed. Form teh compiler sight, you write something to FA_Blink, but never use it. So any writes to FA-Blink are delayed or simply dropped. Form teh code flow, any change of FA_Blink has no effect, asit is not used inside main and the code flow never leaves main (to execute any function that could use the variable).

    Teh ISR uses it, but teh ISR is not called from main, so this is unimportant to the compiler.

    To tell the compiler to still treat a global variable as if it would have side-effects, you'll have to declare it with the volatile keyword. Then the compiler will compile any access to this variable exactly as you wirite it, with no optimizations, reorderings, not keeping it in a register or such. And suddenly your ISR will see it changing when main changes it.
    (on local non-static variables, the volatil keyword ahs no effect, as the compiler knows for sure that nobody else except the current function can possibly know the position of the variable. So the volatile keyword is simply ignored on most compilers for local variables)

  • Thank you! I added volatile and it works.