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.

LP-MSPM0L1306: Changing Duty Cycle

Other Parts Discussed in Thread: SYSCONFIG

Hi Luke,

              We are working with PWM and timer via sysconfig tool.Now we need to load the duty cycle values in the code itself.As it is predefined in sysconfig as a single value,it can not be changed in the code.So how to change the duty cycle values in the code by defining a variable in a function.

             we tried to combine PWM and Timer peripheral.The code is compiled without error.But we are  getting output of either pwm or timer output.Kindly help us to conclude these issues as soon as possible.

  • Hi Mohankumar,

    I created a new thread and moved it here. In the future when talking about a new topic please create a new thread, this will allow other users to find solutions to the relevant topic.

    You can change the duty cycle of the PWM by calling the relevant function. The call DL_TimerG_startCounter(PWM_0_INST);

    uint16_t ccv = 30; //This is the capture compare value you want to set
    
    //Call this function to change the capture compare value
    DL_TimerG_setCaptureCompareValue(PWM_0_INST, ccv, DL_TIMER_CC_0_INDEX);

    Regards,

    Luke

  • Hi Mohankumar,

    Looking at the diagram you posted here, you can call the functions as I have shown above to change the duty cycle for example. If you have a PWM_0_INST you set the capture compare value to match your duty cycle, you can use Sysconfig to find the numbers required as well. You can then dynamically change the duty cycle based on the ADC reading.

    uint16_t adcVal = 1000;
    
    if(adcVal < 1000)
        DL_TimerG_setCaptureCompareValue(PWM_0_INST, 1800, DL_TIMER_CC_0_INDEX);
    else if(adcVal < 2000)
        DL_TimerG_setCaptureCompareValue(PWM_0_INST, 1400, DL_TIMER_CC_0_INDEX);
    else if(adcVal < 3000)
        DL_TimerG_setCaptureCompareValue(PWM_0_INST, 600, DL_TIMER_CC_0_INDEX);
    else
        DL_TimerG_setCaptureCompareValue(PWM_0_INST, 200, DL_TIMER_CC_0_INDEX);

    I suggest utilizing Sysconfig to assist in finding the duty cycle CCV based on your configurations, simply open Sysconfig and the code tab (the <> symbol top right) and click on ti_msp_dl_config.c

    Scroll down to where the PWM is initialized and you can see the function for setting the capture compare value. (line 150 here)

    When you change the duty cycle the line will update and show you the change, you can then take this and place the same value in your code.

    Regards,

    Luke

  • Hi Luke,

                     We used Timer_0 to generate   400ms timer, in that timer interrupt we added a flag to acknowledge that the timer interrupt is working and  to toggle the LED using that.We are getting the require output (i.e 400ms ON/OFF)but we could not get the updated value for the flag(i.e flag=1).So kindly help us to add the variable in the timer interrupt function. 

  • Hi Mohankumar,

    Can you share your timer ISR? also put a breakpoint in the timer ISR to verify that the timer ISR is being reached.

    Regards,

    Luke

  • Hi Luke,

              We  set a breakpoint in the ISR timer  function,it is   going inside the ISR function loop,we called LED toggle function it's working but if we set a variable(flag).we couldn't get the set value of the variable.

    void TIMER_0_INST_IRQHandler(void)
    {

    static uint16_t count = TIMER_800_MILLISECONDS_TICKS;
    switch (DL_TimerG_getPendingInterrupt(TIMER_0_INST))
    {
    case DL_TIMER_IIDX_ZERO:
    /*
    * Counter stopped to avoid a conflict with the timer reading
    * the LOAD value while it's being set
    */
    DL_TimerG_stopCounter(TIMER_0_INST);

    /*
    * Count progressively gets smaller in 0.05 s increments until reset
    * with 0.5s
    */
    uint32_t count = TIMER_800_MILLISECONDS_TICKS;

    DL_Timer_setLoadValue(TIMER_0_INST, count);
    /*
    * By default, this should load the new count value and count down
    * from there (CVAE = 0)
    */
    DL_TimerG_startCounter(TIMER_0_INST);
    DL_GPIO_togglePins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
    flag = ~ flag ;
    break;
    default:
    break;
    }
    }

  • Hi Mohankumar,

    If you want to toggle the flag each time.

    Change flag = ~flag; to flag = !flag;

    Otherwise I would explicitly set the variable to the desired value.

    Here the bit math is working against you and you could step through the disassembly to see the difference here. You could also change the operation to flag = ~(flag & 0xFF) if the variable is a char (does not work if the variable is a bool).

    Regards,

    Luke

  • Hi Luke,

                We tried your above instructions for set a variable(flag).does not work and we couldn't get the set value of the variable.

    flag=  !flag;

  • Hi Mohankumar,

    What type of variable is the flag? Is the variable within scope of the ISR?

    I used the timx_timer_mode_periodic_sleep example and placed a bool that is toggling in the ISR, below is the slightly modified code.

    #include "ti_msp_dl_config.h"
    
    bool check = false;
    
    int main(void)
    {
        SYSCFG_DL_init();
    
        NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
        DL_SYSCTL_enableSleepOnExit();
    
        DL_TimerG_startCounter(TIMER_0_INST);
    
        while (1) {
            __WFI();
        }
    }
    
    void TIMER_0_INST_IRQHandler(void)
    {
        switch (DL_TimerG_getPendingInterrupt(TIMER_0_INST)) {
            case DL_TIMER_IIDX_ZERO:
                DL_GPIO_togglePins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
                check = !check;
                break;
            default:
                break;
        }
    }
    

    Regards,

    Luke