1

I am using the following code to blink LEDs using a timer interrupt:

#include <msp430.h>

#define LED1 BIT0                       //define LED1 as bit 0 (0x00)
#define LED2 BIT6                       //define LED2 as bit 6 (0x40)

int main(void)
{
    //stop watchdog timer
    WDTCTL = WDTPW | WDTHOLD;

    //P1 initialization code
    P1OUT &= 0x00;                      //clear all bits on P1
    P1DIR |= (LED1|LED2);               //set P1.0 and P1.6 to output direction

    //Timer_0A3 initialization
    TA0CCR0 = 12e3;                     //count limit (16 bit)
    TA0CCTL0 = 0x10;                    //enable counter interrupts, set bit 4 high
    TA0CTL = TASSEL_1 + MC_1;           //Timer A0 with ACLK @ 12KHz (TASSEL_1), count UP (MC_1)


    //low power mode
    _BIS_SR(LPM0_bits + GIE);           //LPM0 (low power mode) with interrupts enabled
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer0_A0 (void)       //service routine for Timer0 A0 interrupt
{
    P1OUT ^= (LED1|LED2);               //toggle P1.0 using exclusive-OR
}

And guess what, it works! But only when I'm running the debugger. When I close the debugger, it stops what it's doing and whichever LED was lit at the time remains lit. Resetting the board doesn't help at all.

After a bit of research online, I have tried going to Tools -> Debugger Options -> MSP430 Debugger Options -> Clock Control to uncheck the box for my corresponding timer. I have these options: []ACLK []SMCLK []TACLK. I have tried every combination of checks/unchecks and nothing seems to allow my interrupts to function after the debugger stops running.

If I had to guess, I would say that the timer is running, but somehow the interrupt flags aren't getting set properly. Any idea what is going on here?

austincrft
  • 343
  • 1
  • 2
  • 13
  • Looks to me like you're probably not activating BIT3. Do you have your physical wires connected to the right pins? – Scott Solmer Jul 11 '14 at 16:15
  • It's all being done on the LaunchPad Development Board. No wiring needed. BIT3 is attached to a debounce switch which isn't being used right now. I'll remove it from the code. – austincrft Jul 11 '14 at 16:22

5 Answers5

1

Clear your timer interrupt flag in the interrupt handler:

#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer0_A0 (void)       //service routine for Timer0 A0 interrupt
{
    TA0CTL &= ~TAIFG;                   // Reset interrupt
    P1OUT ^= (LED1|LED2);               //toggle P1.0 using exclusive-OR
}
benpro
  • 4,325
  • 4
  • 19
  • 17
  • 1
    Still experiencing the same error, but this was probably something I needed to do anyway. – austincrft Jul 11 '14 at 16:52
  • It might should have been "TA0CCTL0 &= ~CCIFG;" – benpro Jul 11 '14 at 17:35
  • 1
    Actually, I was able to get your code--as is--to run correctly on a MSP430F5438. Barring any CPU variations, I'm led to believe that something else is going on. – benpro Jul 11 '14 at 17:47
  • You got it to run outside of debug mode? Then I've got no idea what's happening. Are you using Code Composer Studio? – austincrft Jul 11 '14 at 18:00
  • Yes. When these kinds of unexplained problems occur, I begin by inserting other hardware type indicators, such as setting other hardware pins high to trace the execution of the program. It may be that execution never reaches the end of the program due to some fault or that LPM0 is being exited for some reason. – benpro Jul 14 '14 at 13:16
0

This sounds like you are running your application from RAM. Double check your linker configuration and make sure you are targeting Flash if you want to run without the debugger.

Additionally, do you need to clear the any timer interrupt flag? It may be that with the debugger connected, low power mode is not used, and the timer interrupt continually triggers. When in low power mode, it is possible that if an interrupt flag is not cleared that it will not wake the processor again when the timer attempts to set the interrupt flag again.

rjp
  • 1,760
  • 13
  • 15
  • I can't seem to find the linker config in Code Composer Studio. Do you know where this is located so I can check it? Another interesting thing is that I commented out the `_BIS_SR(LPM0_bits + GIE);` low power mode enable and it gives me the same no-interrupt result even with the debugger. Is it possible that when the debugger isn't running it isn't correctly entering the low power mode? – austincrft Jul 11 '14 at 16:44
  • I've only ever used IAR EW430 for MSP430 work, so I'm not familiar with CCS. @benpro's answer is probably the correct one. – rjp Jul 11 '14 at 16:45
0

I found out that the line of code BCSCTL3 |= LFXT1S_2; needed to be added in between TA0CTL = TASSEL_1 + MC_1; and _BIS_SR(LPM0_bits + GIE);. According to the datasheet, this is a Basic Clock System Control that needs to be set. It has something to do with the crystal oscillator, but I don't entirely understand what it does. If anyone can offer me any more insight, that would be great.

austincrft
  • 343
  • 1
  • 2
  • 13
0

I had the same problem. After a while I realized that problem was in the ACLK. When code is running from the debugger, proper feeding ACLK from VLO arranges debugger. But in the opposite case you have to set the source for ACLK manually by setting LFXT1Sx = 10b, in C:

BCSCTL3 = LFXT1S_2;

Do it somewhere at the beginning of main().

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1

Try to add:

  • First

while (1);

before _BIS_SR(LPM0_bits + GIE);

  • Second

while (1);

after _BIS_SR(LPM0_bits + GIE);

Ruslan Gerasimov
  • 1,752
  • 1
  • 13
  • 20
  • The `while(1)` before `_BIS_SR(LPM0_bits + GIE);` made the lights stop blinking altogether. The `while(1)` after `_BIS_SR(LPM0_bits + GIE);` gives me the same error. – austincrft Jul 14 '14 at 11:50