cancel
Showing results for 
Search instead for 
Did you mean: 

Timer PWM change period on runtime

Zarck.zek
Associate II
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
1 ACCEPTED SOLUTION

Accepted Solutions
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)

View solution in original post

2 REPLIES 2
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
Associate II
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