Skip to main content
MaedeMpr
Associate
May 9, 2020 10:29 AM UTC
Solved

How to Change PWM Polarity without DeInit Timer??

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.
RMcCa
RMcCaBest answer
Senior II
May 10, 2020 6:11 PM UTC

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 5:26 AM UTC

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 4:54 AM UTC

Yes, RM does not mention the polarity bits. Thanks

waclawek.jan
Super User
May 12, 2020 4:17 PM UTC

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
Mar 11, 2022 2:01 PM UTC

__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.