cancel
Showing results for 
Search instead for 
Did you mean: 

How to Change PWM Polarity without DeInit Timer??

MaedeMpr
Associate II

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??

16 REPLIES 16
RMcCa
Senior II

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

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

I've tried but doesn't work as this. Thanks.

I tried this and it looks good. Thanks. It's solved my problem.

MaedeMpr
Associate II

Yes, RM does not mention the polarity bits. Thanks

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

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