Skip to main content
Zarck.zek
Associate II
June 12, 2017
Solved

Timer PWM change period on runtime

  • June 12, 2017
  • 2 replies
  • 1429 views
Posted on June 12, 2017 at 11:17

Hi everyone,

In my project, I need to generate a PWM with some parameters which need to be changed during runtime.

Here are the parameters : 

- Duty cycle

- Period

- Voltage level (I think this one is not possible... maybe there is an alternative way you may know?)

First of all, I generate the PWM without troubles.

I am able to change the duty cycle on runtime using __HAL_TIM_SET_COMPARE(__HANDLE__, __CHANNEL__, __COMPARE__) macro

Now, it is possible to change the period while runtime using this kind of macro? I can't find it.

Thank you very much,

Marc

#pwm #timer
This topic has been closed for replies.
Best answer by waclawek.jan
Posted on June 12, 2017 at 11:23

/**

  * @brief  Sets the TIM Autoreload Register value on runtime without calling

  *         another time any Init function.

  * @param  __HANDLE__: TIM handle.

  * @param  __AUTORELOAD__: specifies the Counter register new value.

  * @retval None

  */

#define __HAL_TIM_SET_AUTORELOAD(__HANDLE__, __AUTORELOAD__)                  \

                        do{                                                  \

                            (__HANDLE__)->Instance->ARR = (__AUTORELOAD__);  \

                            (__HANDLE__)->Init.Period = (__AUTORELOAD__);    \

                          } while(0U)

2 replies

waclawek.jan
waclawek.janBest answer
Super User
June 12, 2017
Posted on June 12, 2017 at 11:23

/**

  * @brief  Sets the TIM Autoreload Register value on runtime without calling

  *         another time any Init function.

  * @param  __HANDLE__: TIM handle.

  * @param  __AUTORELOAD__: specifies the Counter register new value.

  * @retval None

  */

#define __HAL_TIM_SET_AUTORELOAD(__HANDLE__, __AUTORELOAD__)                  \

                        do{                                                  \

                            (__HANDLE__)->Instance->ARR = (__AUTORELOAD__);  \

                            (__HANDLE__)->Init.Period = (__AUTORELOAD__);    \

                          } while(0U)
Zarck.zek
Zarck.zekAuthor
Associate II
June 12, 2017
Posted on June 12, 2017 at 14:19

Right...

I missed that... So this implies to enable 'AutoReloadPreload' register.

Anyway, thank you very much.

Marc