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.

MSP430FR4133: NA

Part Number: MSP430FR4133
Other Parts Discussed in Thread: MSPWARE

i am trying to fade an led using msp430fr4133 using pwm and interrupts but i am not getting anything my PWM is working and led is blinking instead of fading its should dim and glow following is my code i am MSPware driver lib can anyone suggest any improvements in my code.

#include "driverlib.h"

void initTimers(void);

int main(void) {

WDT_A_hold(WDT_A_BASE);

GPIO_setAsOutputPin( GPIO_PORT_P1, GPIO_PIN0 ); // Red LED (LED2)
GPIO_setOutputLowOnPin( GPIO_PORT_P1, GPIO_PIN0 );
///GPIO_setAsOutputPin( GPIO_PORT_P4, GPIO_PIN0 ); // Green LED (LED2)
//GPIO_setOutputLowOnPin( GPIO_PORT_P4, GPIO_PIN0 );

//Setting output pins

GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P8, GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
//GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1, GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);


PMM_unlockLPM5();

initTimers();

__bis_SR_register( GIE ); // Enable interrupts globally

while(1) {
__no_operation(); // Placeholder for while loop (not required)
}

return (0);
}

void initTimers(void)
{
// Set up the interrupt using CCROIFG to toggle red LED1
// Set up the interrupt using TA1IFG toggle green LED2
Timer_A_initUpModeParam initUpParam = { 0 };
initUpParam.clockSource = TIMER_A_CLOCKSOURCE_ACLK; // Use ACLK (slower clock)
initUpParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1; // Input clock = ACLK / 1 = 32KHz
initUpParam.timerPeriod = 0xFFFF/2; // Half the time
initUpParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_ENABLE; // Enable TAR -> 0 interrupt
initUpParam.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE; //Enable compare interrupt
initUpParam.timerClear = TIMER_A_DO_CLEAR; // Clear TAR & clock divider
initUpParam.startTimer = true; // Don't start the timer, yet

Timer_A_initUpMode( TIMER_A1_BASE, &initUpParam );

//CCR2 configuration
Timer_A_initCompareModeParam initCcr2Param ={0};
initCcr2Param.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_2;
initCcr2Param.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
initCcr2Param.compareOutputMode = TIMER_A_OUTPUTMODE_TOGGLE_RESET;
initCcr2Param.compareValue = 0x0AB0;

Timer_A_initCompareMode( TIMER_A1_BASE, &initCcr2Param );

Timer_A_clearTimerInterrupt( TIMER_A1_BASE );
Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0 + TIMER_A_CAPTURECOMPARE_REGISTER_2); //Clear CCROIFG

Timer_A_startCounter(
TIMER_A1_BASE,
TIMER_A_UP_MODE
);
}

  • Hello!

    In order to use PWM to brighten and dim an LED you need to sweep the duty cycle from 0% to 100% and then back down to 0% in equal time steps. Please refer to the example code provided for MSP430FR4133 in the TI Resource Explorer called msp430fr413x_ta0_16.c. In this example, a PWM signal is outputted to some GPIO pins. All you would need to do, is slightly modify the example code in order to change the duty cycle in TA0CCR1 or TA0CCR2 depending on which GPIO you're using so that instead of being a constant value, it goes from 0 to the value in TA0CCR0 and then back down to zero. You can put this in a loop to brighten and dim the LED continuously.

    Best of luck!

    Best regards,

    Matt Calvo

  • Hello,

    If this issue is now resolved please go ahead and select "Resolved" so that we can close out this thread for tracking purposes.

    Thanks and best regards,

    Matt Calvo
  • i have tried i did not understand can help in that regard 

  • Can you help me in this regard change the PWm in my code so that it can work i am using driver lib
    not #include libraries
  • Hello,

    Please use the code inserting link to properly add your code so that I can take a look at it in the correct formatting. Have you attempted to upload the code example I referenced in the previous reply? Uploading the code example to CCS from the Resource Explorer is really easy and is a great starting point to finishing up your LED dimming project.

    Best regards,

    Matt
  • Hello,

    I have written an example code that uses PWM to brighten and dim the LED on the MSP430FR4133 Launchpad. The only hardware modification you would have to do is use a jumper wire to connect P1.6 to the LED1 annode pin that way you can see the PWM dimmer working.

    #include <msp430.h>
    
    #define PWM_PERIOD (20000)
    
    int direction = 0;  //1 is up and 0 is down
    
    int main(void)
    {
        WDTCTL = WDTPW | WDTHOLD;                 // Stop WDT
    
        P1DIR |= BIT6;                     // P1.6 output
        P1SEL0 |= BIT6;                    // P1.6 options select
        
        // Disable the GPIO power-on default high-impedance mode to activate
        // previously configured port settings
        PM5CTL0 &= ~LOCKLPM5;
    
        TA0CCR0 = PWM_PERIOD-1;                         // PWM Period
        TA0CCTL2 = OUTMOD_7;                      // CCR2 reset/set
        TA0CCR2 = PWM_PERIOD-1;                            // CCR2 PWM duty cycle
        TA0CTL = TASSEL__SMCLK | MC__UP | TACLR;  // SMCLK, up mode, clear TAR
    
        while(1){
            if(TA0CCTL0 && CCIFG){  //If the interrupt flag is set
                if(direction){  //if counting up
                    if(TA0CCR2 == (PWM_PERIOD-1)){
                        direction = 0;
                    }
                    else{
                    TA0CCR2++;
                    }
                }
                else{   //if counting down
                    if(TA0CCR2 == 0){
                        direction = 1;
                    }
                    else{
                        TA0CCR2--;
                    }
                }
    
            }
        }
    
    
    }

    Please try this code on your MSP430FR4133 Launchpad and hopefully this helps you to understand how the timer module can be used to output PWM signals! 

    Best regards,

    Matt Calvo

  • Hello!

    If my above support and code example helped you with your problem please go ahead and select that the above post resolved your issue so that we can close this thread out. Thanks!

    Best regards,

    Matt Calvo
  • Hello,

    I am going to go ahead and close out and log this thread for tracking purposes. Should anything arise feel free to comment on this post again or make a new E2E post. Thanks!

    Best regards,

    Matt Calvo
  • Hi Matthew,

     Your above example code is working fine in my Launchpad, But the thing is i need to control the brightness using a Potentiometer for that what are the changes to be done in this code. I tried the Timer compare mode and PWM for this but i am not able to get it done... can you please help me with this..?

    #include <msp430.h>

    #define PWM_PERIOD (20000)

    int direction = 0; //1 is up and 0 is down

    int main(void)
    {
    WDTCTL = WDTPW | WDTHOLD; // Stop WDT

    P1DIR |= BIT6; // P1.6 output
    P1SEL0 |= BIT6; // P1.6 options select

    // Disable the GPIO power-on default high-impedance mode to activate
    // previously configured port settings
    PM5CTL0 &= ~LOCKLPM5;

    TA0CCR0 = PWM_PERIOD-1; // PWM Period
    TA0CCTL2 = OUTMOD_7; // CCR2 reset/set
    TA0CCR2 = PWM_PERIOD-1; // CCR2 PWM duty cycle
    TA0CTL = TASSEL__SMCLK | MC__UP | TACLR; // SMCLK, up mode, clear TAR

    while(1){
    if(TA0CCTL0 && CCIFG){ //If the interrupt flag is set
    if(direction){ //if counting up
    if(TA0CCR2 == (PWM_PERIOD-1)){
    direction = 0;
    }
    else{
    TA0CCR2++;
    }
    }
    else{ //if counting down
    if(TA0CCR2 == 0){
    direction = 1;
    }
    else{
    TA0CCR2--;
    }
    }

    }
    }


    }

  • The code I provided uses a loop to increment and decrement the value in TA0CCR2 which in turn brightens and dims the LED connected to the PWM output. If you really want to use the MCU to measure voltage from a potentiometer, then you would just use the ADC to measure the varying voltage from the pot (learn.parallax.com/.../adc-and) and in turn change the value in TA0CCR2 accordingly.

    There is a much easier option for purely dimming the LED connected to the GPIO output and it is actually all done off chip. Here is a link which helps show you how to implement it (electronics.stackexchange.com/.../using-a-variable-resistor-to-dim-an-led)

    Best regards,

    Matt Calvo
  • Thank you Matthew for the link . I will try it and get back to you.

**Attention** This is a public forum