Skip to main content
Visitor II
October 14, 2021
Solved

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

  • October 14, 2021
  • 4 replies
  • 2858 views

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

Thank you.

    This topic has been closed for replies.
    Best answer by waclawek.jan

    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

    4 replies

    Visitor II
    October 14, 2021

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

    Super User
    October 14, 2021

    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.

    Visitor II
    October 15, 2021

    thank You.​

    Super User
    October 14, 2021

    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

    ST Employee
    April 8, 2024

    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