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: Using PWM and built-in timers to fade LED

Part Number: MSP430F5529

Tool/software: Code Composer Studio

I am trying to use Timer_A to operate a fading LED with PWM.  This is my first time using the timers on this board, and I am having an issue getting this to work.  Can you guys tell me what I am doing wrong?  Thank you for your time in advance.  Here is my code thus far:

#include <msp430.h>

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 = WDTPW + WDTHOLD;                 // Stop watchdog timer

    P1DIR |= BIT0;
    P1SEL |= BIT0;

    TA0CCR0 = 1000-1;
    TA0CCTL1 = OUTMOD_7;
    TA0CCR1 = 0;

    TA0CTL = TASSEL_1 + MC_1;

    volatile signed int i = 0;

    while(1){

        for(i = 1; i < 999; i += 10){
            TA0CCR1 = i;
            _delay_ms(10);
        }

        for(i = 999-1; i > 0; i -= 10){
            TA0CCR1 = i;
            _delay_ms(10);
        }
    }
}

  • 1) You're programming TA0.1, but that's over on P1.2,  not P1.0. [Ref data sheet (SLAS590N) Table 6-46]. The simplest fix is probably to add "P1SEL |= BIT2;" and jumper JP8 (LED side) to the P1.2 pin.

    2) Your PWM period is 1000/32768=30.5ms, so you're changing CCR1 three times each cycle. This will sort-of work but you may see glitches. I suggest setting CCR0=32768/100=328 (10ms delay->100Hz).

    [Edit: Added reference.]

  • That was good advice, thank you!  I have updated my code as follows and jumpered JP8 to P1.2:

    #include <msp430.h>
    
    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 = WDTPW + WDTHOLD;                 // Stop watchdog timer
    
        P1DIR |= BIT2;
        P1SEL |= BIT2;
    
        TA0CCR0 = 350;
        TA0CCTL1 = OUTMOD_7;
        TA0CCR1 = 0;
    
        TA0CTL = TASSEL_1 + MC_1;
    
        volatile signed int i = 0;
    
        while(1){
    
            for(i = 1; i < 309; i += 5){
                TA0CCR1 = i;
                _delay_ms(50);
            }
    
            for(i = 309-1; i > 0; i -= 5){
                TA0CCR1 = i;
                _delay_ms(50);
            }
        }
    }
    

    These changes have made it so I get a smooth fade in that takes almost exactly 3s followed by a smooth fade out that takes almost exaclty 3s.  Thank you very much for your help.

  • I will note that I occasionally see a little flicker in the LED.  I am not sure what causes this.

**Attention** This is a public forum