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.

CCS/MSP430F5529: Timer Glitches

Part Number: MSP430F5529

Tool/software: Code Composer Studio

I have created some code that makes an LED fade in and out smoothly over a total time period of 6 seconds.  It does this repeatedly forever unless an interrupt is thrown.  This code operates perfectly except for one thing: I occasionally get small glitches with the LED.  Every now and then it will flash as it is fading.  I suspect this has to do with the way I have set up the timers (or the loop timing).  I am not well-versed enough in this processor yet to precisely surmise my mistake, but if one of you awesome people wants to take a look, I would appreciate it.

#include <msp430.h>

volatile signed int i = 0;
volatile signed int j = 305;
volatile signed int k = 1;
volatile signed int l = 1;

void _delay_ms(volatile unsigned int length)
{
    volatile unsigned int delay = 0;
    for(delay = 0; delay < length; delay++)
    {
        _delay_cycles(1000);
    }
}

int main(void)
{
    WDTCTL = WDT_MDLY_32;   // 32ms interval (default)
    _EINT();                // enable interrupts
    SFRIE1 |= WDTIE;        // Enable WDT interrupt

    //**NOTE: This setup requires putting a jumper between LED1 and P1.2 on the board**

    P1DIR |= BIT2;      // Set up Timer A 0.1(P1.2) for output
    P1SEL |= BIT2;      // P1.2 special function
    SFRIE1 |= WDTIE;    // Enable WDT interrupt

    P1DIR &= ~BIT1;     // set P1.1 as input (SW2)
    P1REN |= BIT1;      // enable pull-up resistor
    P1OUT |= BIT1;      // required for proper IO
    P1IE |= BIT1;       // enable interrupt at P1.1
    P1IES |= BIT1;      // enable hi->lo edge for interrupt
    P1IFG &= ~BIT1;     // clear any erroneous interrupt flag

    P2DIR &= ~BIT1;     // set P2.1 as input (SW1)
    P2REN |= BIT1;      // enable pull-up resistor
    P2OUT |= BIT1;      // required for proper IO
    P2IE |= BIT1;       // enable interrupt at P2.1
    P2IES |= BIT1;      // enable hi->lo edge for interrupt
    P2IFG &= ~BIT1;     // clear any erroneous interrupt flag

    TA0CCR0 = 328;
    TA0CCTL1 = OUTMOD_7;
    TA0CCR1 = 0;

    TA0CTL = TASSEL_1 + MC_1;

    while(1)
    {
        if(l == 1)
        i += k;
        else
        i = i;
        _delay_ms(10);
        if(i <= 0)
            k = k*-1;
        if(i >= j)
            k = k*-1;
    }
}

#pragma vector = WDT_VECTOR
__interrupt void watchdog_timer(void)
{
    TA0CCR1 = i;
}

// this ISR handles the SW1 key press
#pragma vector = PORT2_VECTOR
__interrupt void PORT2_ISR(void)
{
    // let us clear the flag
    P2IFG &= ~BIT1;

    //debouncing section
    __delay_cycles(25000);

    // if SW1 is not pressed, return
    if((P2IN&BIT1)!=0x00)
    {
        return;
    }
    // Pause fading
    if(l == 1)
    {
        l = 0;
    }
}

// this ISR handles the SW2 key press
#pragma vector = PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
    // let us clear the flag
    P1IFG &= ~BIT1;

    //debouncing section
    __delay_cycles(25000);

    // if SW2 is not pressed, return
    if((P1IN&BIT1)!=0x00)
    {
        return;
    }
    // Continue fading
    if(l == 0)
    {
        l = 1;
    }
}

**Attention** This is a public forum