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.

MSP432P401R: Having trouble with TimerA0 interrupt

Part Number: MSP432P401R

Hello guys,

I am new to arm microcontrollers. and TI.  I have an MSP432 launchpad and I wrote a piece of code myself, to blink an led after specific intervals. when debugging, the TAIFG remains set. The LED turn on all the time. Where I wrong ?

#include "msp432.h"

int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;     // Stop WDT
  TA0CCTL0 = CCIE;                             // CCR0 interrupt enabled
  TA0CTL = TASSEL_2 + MC_1 + ID_3 + TAIE;           // SMCLK/8, upmode
  TA0CCR0 =  10000;                     // 12.5 Hz
  P1OUT &= 0x00;               // Shut down everything
  P1DIR &= 0x00;
  P1DIR |= BIT0 + BIT6;            // P1.0 and P1.6 pins output the rest are input


  while(1)                          //Loop forever, we work with interrupts!
  {
      while(TAIFG != 1);
      TA0CCR0 =  10000;
      TA0CTL += (~TAIFG);
      P1OUT ^= 0x01;
  }

  return 0;
}

  • Hello,

       TAIFG is not a register, but a definition found in the device header file (if you look in msp.h it points to the specific msp432p401r_classic.h).  

    /******************************************************************************
    * TIMER_A Bits (legacy section)
    ******************************************************************************/
    /* TA0CTL[TAIFG] Bits */
    #define TAIFG_OFS                                TIMER_A_CTL_IFG_OFS             /*!< TAIFG Offset */
    #define TAIFG                                    TIMER_A_CTL_IFG                 /*!< TimerA interrupt flag */

    So in line 16 TAIFG is always '1' and in line 18 you are clearing the first bit location in the timer 0 control register.

    I would recommend moving to the CMSIS style of coding (to get away from using the _classic style).  This will provide more compatibility with other devices in the portfolio and make incorporating other ARM related software more seamless.  You can find examples here:

    Regards,

    Chris

  • Hello, Thanks for the replay. I believe I understand what you have mentioned in your post. That's one of the mistakes I have done in my code. But I think there are some more mistakes too. The TAIFG is set even before executing the 8th line code. I think I missing something in my code!! Any guess ??

  • Time for CMSIS.
  • My guess is that you started the timer before setting TA0CCR0, and the timer counted to 0 a few times.
  • Sorry for the late replay and thanks for the response   &  

    I changed my while loop to while(TA0CTL != 0x02D3), and the led is blinking fine now. I am also digging into CMSIS. I want to confirm if the timer waits for the debugger to step into each instruction. In my case, the TimerA0 has overflowed just after debugging the 8th line of my code.

  • I don't know whether the MSP432 timer (SMCLK) stops at a breakpoint. Observed behavior is that different MCUs act differently in this regard.

    You can probably discern this yourself, by inserting a few __no_operation() statements in strategic places and watching TA0R/TA0CTL as you step through them.

  • Hi ,

    I think I have almost got a solution for my problem.

    #include "msp432.h"
    
    int main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                           // Stop WDT
      TA0CCTL0 = CCIE;                                    // CCR0 interrupt enabled
      TA0CCR0 =  50000;
      TA0CTL = TASSEL_2 + MC_1 + ID_3 + TAIE;             // SMCLK/8, upmode
      P1OUT &= 0x00;                                      // Shut down everything
      P1DIR &= 0x00;
      P1DIR |= BIT0 + BIT6;                               // P1.0 and P1.6 pins output the rest are input
    
    
      while(1)                                            // Loop forever, we work with interrupts!
      {
          while(TA0CTL != 0x02D3);                        // TAIFG is not a Reg. check if TAIFG is sets
          TA0CCR0 =  50000;
          TA0CTL = TASSEL_2 + MC_1 + ID_3 + TAIE;
          P1OUT ^= 0x01;
      }
    
      return 0;
    }
    
    

    The above code works fine. But why below code fails to work when two changes were made.

    #include "msp432.h"
    #define IFG 0x0001;
    
    int main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                           // Stop WDT
      TA0CCTL0 = CCIE;                                    // CCR0 interrupt enabled
      TA0CCR0 =  50000;
      TA0CTL = TASSEL_2 + MC_1 + ID_3 + TAIE;             // SMCLK/8, upmode
      P1OUT &= 0x00;                                      // Shut down everything
      P1DIR &= 0x00;
      P1DIR |= BIT0 + BIT6;                               // P1.0 and P1.6 pins output the rest are input
    
    
      while(1)                                    
      {
          while(TA0CTL & 0x0001);                      //  <----------------------- Change 
          TA0CCR0 =  50000;
          TA0CTL = TASSEL_2 + MC_1 + ID_3 + TAIE;
          TA0CTL &= 0xfff0;                                       //    <----------------------- Change
          P1OUT ^= 0x01;
      }
    
      return 0;
    }
    
    The debugger stucks at while(TA0CTL & 0x0001); even after the TAIFG is set. Why ??

  • The loop "while(TA0CTL & 0x0001);" will continue for as long as TAIFG is set. There's nothing to clear that bit in the loop so it will loop forever.

    I think instead you want to loop until it is set (ie while (TA0CTL & 0x0001) == 0)

**Attention** This is a public forum