cancel
Showing results for 
Search instead for 
Did you mean: 

Change Period/Pulse of HRTIM based SimplePWM

C-Coder
Associate II

Custom board, STM32H747 device, Simple PWM

 

Using the code generated by MX, and adding a call to HAL_HRTIM_SimplePWMStart, I can generate a nice simple PWM output signal. If I change the initial values for period/pulse, rebuild, and program to the device, I can see the changed output signal. Now I want to change the period and/or the pulse after it has started. I've tried this a few ways. one is:

 

    	// Update the period/pulse stored in the structs above...

    	__HAL_HRTIM_SETPERIOD(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, g_TimeBaseCfg.Period);
    	__HAL_HRTIM_SETCOMPARE(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, HRTIM_COMPAREUNIT_1, g_SimplePWMChannelCfg.Pulse);
    	HAL_HRTIM_SoftwareUpdate(&hhrtim, HRTIM_TIMERINDEX_TIMER_A);

 

I've also tried doing:

    	// Update the period/pulse stored in the structs above...

    	if (HAL_HRTIM_SimplePWMStop(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, HRTIM_OUTPUT_TA1) != HAL_OK)
    	{
    		Error_Handler();
    	}

    	if (HAL_HRTIM_TimeBaseConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, &g_TimeBaseCfg) != HAL_OK)
    	{
    	  Error_Handler();
    	}
    	if (HAL_HRTIM_SimplePWMChannelConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, HRTIM_OUTPUT_TA1, &g_SimplePWMChannelCfg) != HAL_OK)
    	{
    	  Error_Handler();
    	}
    	if (HAL_HRTIM_SimplePWMStart(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, HRTIM_OUTPUT_TA1) != HAL_OK)
    	{
    		Error_Handler();
    	}

 

Regardless of which method I try, the resulting output signal is still using the same period and pulse. Can any one name the one or hundred simple things I'm missing?

 

Thank you,

C-Coder

3 REPLIES 3
damon467
Visitor

<p>To change the period or pulse of an HRTIM-based SimplePWM, you’ll need to modify the relevant timer registers in your STM32 firmware. Specifically, you can adjust the <code>PERx</code> register to change the period and the <code>CMPx</code> register to modify the pulse width. Make sure to reconfigure the prescaler and clock settings if needed to achieve the desired frequency. If you’re using STM32CubeMX or HAL libraries, you can update the configuration in the initialization code and restart the timer. For more precise control, consider using low-level register access. Let me know if you need a code example!</p>

damon467
Visitor

<p>Changing the period and pulse width in HRTIM for SimplePWM requires careful handling of the timer settings. You can update the <code>PERx</code> register for the period and the <code>CMPx</code> register for the duty cycle. If you’re using STM32CubeIDE, you can leverage the HRTIM configuration tools to simplify this process. Additionally, ensure that the timer is stopped before making changes to avoid glitches. After updating the registers, restart the timer to apply the new settings. If you’re still facing issues, double-check the clock configuration and prescaler values to ensure they align with your desired output. Happy coding!</p>

@damon467 Please don't post AI generated answers.

Why predominantly AI-generated posts and replies o... - STMicroelectronics Community

If you feel a post has answered your question, please click "Accept as Solution".