Skip to main content
MaedeMpr
Associate
May 9, 2020
Solved

How to Change PWM Polarity without DeInit Timer??

  • May 9, 2020
  • 16 replies
  • 3269 views

I have read STM32f1 Reference Manual and tested STM32CubeMx functions but have not found any solution.

Is there a way to change PWM polarity without Deinitializing Timer??

This topic has been closed for replies.
Best answer by RMcCa

If the phase of the pwm signal is unimportant, write max - val instead of val to the ccr to invert the polarity.​

16 replies

waclawek.jan
Super User
May 11, 2020

I've just noticed that you have TIMx_CR2.CCPC set:

 CCxE, CCxNE and OCxM bits are preloaded, after having been written, they are updated

only when a commutation event (COM) occurs (COMG bit set or rising edge detected on

TRGI, depending on the CCUS bit).

While RM does not mention the polarity bits (CCxP/CCxNP in TIMx_CCER), I wouldn't be surprised if they were preloaded too.

Try to clear this bit (or not set it at the beginning).

JW

MaedeMpr
MaedeMprAuthor
Associate
May 12, 2020

Yes, RM does not mention the polarity bits. Thanks

waclawek.jan
Super User
May 12, 2020

OK so have you tried whether clearing TIMx_CR2.CCPC allows you to change output polarity by flipping the CCxP bits in TIMx_CCER?

JW

AFaya.1
Associate III
March 11, 2022

__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);

Use the above function inside the interrupt this will change the timer polarity during run time.