Skip to main content
EGonc.3
Associate
August 4, 2021
Question

PWM off

Hi

I would like to know if the following code can be used to set the PWM to a constant 0.

TIM_OC_InitTypeDef sConfigOC = {0};

sConfigOC.OCMode = TIM_OCMODE_PWM1;

sConfigOC.Pulse = 0;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

if ( HAL_TIM_PWM_ConfigChannel ( &htim12, &sConfigOC, TIM_CHANNEL_2 ) != HAL_OK )

If that is wrong, could someone let me know what is the right code? thanks

This topic has been closed for replies.

3 replies

TDK
August 4, 2021

Seems right. If not, flip the polarity.

Did you try it?

The code you linked doesn't actually start the PWM. Presumably you are starting it and the associated timer elsewhere.

"If you feel a post has answered your question, please click ""Accept as Solution""."
MattKefford
Associate III
August 4, 2021

Have you already tried HAL_TIM_PWM_Stop() and did that not work? I would've thought just disabling PWM and perhaps set the pin low would be ok.

waclawek.jan
Super User
August 4, 2021

HAL_TIM_PWM_Stop() most probably (I am not going to look it up) clears TIMx_CCER.CCxE and that effectively threestates the pin, which is different from setting it to 0.

JW