MSP432 Timer_A0 interrupt flag TAIFG remains set

I have written a program to blink the onboard blue LED of MSP432 Launchpad using Timer_A0. The program compiles successfully, but stucks at the while loop during debugging. The LED is ON all the time. I am using Code Composer Studio for development. Any idea guys?

#include "msp.h"

int main(void)
{
    P2DIR = 0x04;
    P2OUT = 0x00;

    TIMER_A0->CCR[0] = 50000;
    TIMER_A0->CTL = TASSEL_2 | MC_1 | ID_3 | TAIE;  // Timer 0 initialization

  while(1) {
      while(TAIFG != 1);
      TIMER_A0->CCR[0] = 50000;
      TA0CTL += (~TAIFG);                                        
      P2OUT ^= 0x04;
  }
  return 0;
}
1 Like

The TAIFG is a macro defined somewhere in the msp432p401r_classic.h, it’s not at all a register. So the line TA0CTL += (~TAIFG); doesn’t make any sense, because TAIFG is always set ( look for msp432p401r_classic.h file )

Thanks for the replay. I thought TAIFG was a register. I made two changes to my code, one works fine and other one not.

#include "msp.h"

int main(void)
{
    P2DIR = 0x04;
    P2OUT = 0x00;

    TIMER_A0->CCR[0] = 50000;
    TIMER_A0->CTL = TASSEL_2 | MC_1 | ID_3 | TAIE;  // Timer 0 initialization

  while(1) {
      while(TA0CTL != 0x02D3);            //------------------->  Change
      TIMER_A0->CCR[0] = 50000;
      TA0CTL += (~TAIFG);                                        
      P2OUT ^= 0x04;
  }
  return 0;
}

The above piece of code works fine, but why the below one failed to blink. The below piece of code stuck at while((TIMER_A0->CTL & TA_IFG)); while debugging.

#include "msp.h"

int main(void)
{
    P2DIR = 0x04;
    P2OUT = 0x00;

    TIMER_A0->CCR[0] = 50000;
    TIMER_A0->CTL = TASSEL_2 | MC_1 | ID_3 | TAIE;  // Timer 0 initialization

  while(1) {
      while((TIMER_A0->CTL & TA_IFG));         //------------------->  Change
      TIMER_A0->CCR[0] = 50000;
      TA0CTL += (~TAIFG);                                        
      P2OUT ^= 0x04;
  }
  return 0;
}

The problem is within the while loop. Whenever the timer overflows (TAIFG = 1 ), it loops forever. Instead, you may use something like this:

 while(1) {
      while((TIMER_A0->CTL & TA_IFG)==0);   //Makes condition false, when TAIFG = 1
      TIMER_A0->CCR[0] = 50000;
      TA0CTL += (~TAIFG);                                        
      P2OUT ^= 0x04;
  }

This force the loop to terminate whenever the the timer overflows.

Regards.

2 Likes

Thanks!! @Mr.Tesla I should have been more careful with while loop.