cancel
Showing results for 
Search instead for 
Did you mean: 

How to disable PWM output but enable its complementary PWM output?

ausera uirwaij
Associate III

however, pwm1 doesn't output pulses. I think timer1 stop comparing output if calling Stop() function. Anyone has idea to implement this?

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

Which STM32?

Note, that disabling CCxE puts the given pin to Hi-Z. Also note that the complementary channel does not output inverted version of the waveform if CCxE is cleared.

Because of these minor but surprising features, it may be easier just swap the pins' setting in GPIO_MODER from AF to Out and vice versa.

I don't think you should seek Cube/HAL incantations for these things as they are beyond what Cube/HAL authors deem as "normal usage".

JW

View solution in original post

5 REPLIES 5
S.Ma
Principal

Just don't connect the pwm output signal of the timer to any mcu pin....

TDK
Guru

HAL_TIM_PWM_Stop stops the timer. You can see this in the source code.

If you just want to disable that channel, clear the CC1E bit:

TIM1->CCER &= ~TIM_CCER_CC1E;

If you want to start output on CH1N, and the timer is already started, enable the CC1NE bit:

TIM1->CCER |= TIM_CCER_CC1NE;

If you really want to use HAL, you can use TIM_CCxChannelCmd/TIM_CCxNChannelCmd.

If you feel a post has answered your question, please click "Accept as Solution".

Which STM32?

Note, that disabling CCxE puts the given pin to Hi-Z. Also note that the complementary channel does not output inverted version of the waveform if CCxE is cleared.

Because of these minor but surprising features, it may be easier just swap the pins' setting in GPIO_MODER from AF to Out and vice versa.

I don't think you should seek Cube/HAL incantations for these things as they are beyond what Cube/HAL authors deem as "normal usage".

JW

thank You.​

STM_Thirty2
ST Employee

For the sake of completion and correctness I would like to clarify that the output for the complementary PWM channel can indeed be managed separately from the main output. This can be achieved by utilizing the following HAL functions, which are designed to selectively activate or deactivate the relevant bits:

To turn off the main channel and turn on the complementary channel, you can use the following code snippets:

Using HAL Functions:

// Turn off main channel (TIM_CHANNEL_1)
TIM_CCxChannelCmd(TIM1, TIM_CHANNEL_1, TIM_CCx_DISABLE); // use TIM_CCx_DISABLE to enable

// Turn on complementary channel (TIM_CHANNEL_1)
HAL_TIMEx_OCN_Start(&htim1, TIM_CHANNEL_1); // use HAL_TIMEx_OCN_Start to stop

Using Direct Register Manipulation:

// Enable main output (Channel 1)
TIM1->CCER |= TIM_CCER_CC1E;

// Disable complementary output (Channel 1N)
TIM1->CCER &= ~TIM_CCER_CC1NE;

Note that TIM_CCxChannelCmd expects a TIM_TypeDef *TIMx
while HAL_TIMEx_OCN_Star expects a  TIM_HandleTypeDef *htim

If you feel a post has answered your question, please click "Accept as Solution"