cancel
Showing results for 
Search instead for 
Did you mean: 

PWM off

EGonc.3
Associate II

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

3 REPLIES 3
TDK
Guru

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

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.

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