2026-04-18 12:46 AM
Hello,
What HAL2 API can I use to turn on/off the output of a timer? In HAL (ver 1) this could have been done like this:
TIMx->CCER &= ~TIM_CCER_CC1E; // Example for Channel 1: ~TIM_CCER_CC1E
// or
__HAL_TIM_MOE_DISABLE(&htimx);
Thank you,
-Gil
Solved! Go to Solution.
2026-04-19 7:42 PM
One line of code, modifying the timer's CCER, is everything you need.
If you prefer to switch between push-pull and open drain, write the GPIOx->OTYPER register.
In both cases, register access is faster to execute and easier to type in your source code. And it works the same in all STM32 series other than STM32F1.
2026-04-18 4:44 AM
Either HAL_TIM_OC_StopChannel
or LL_TIM_CC_DisableChannel
or LL_TIM_DisableAllOutputs.
Direct register access still works.
Which one you want depends on your code and how it was started.
2026-04-19 6:00 AM
Thank you for the reply @TDK,
For what I was doing I ended up using HAL_GPIO_Init, by simply changing the timer output_type from HAL_GPIO_OUTPUT_PUSHPULL to HAL_GPIO_OUTPUT_OPENDRAIN to disable the output and back to HAL_GPIO_OUTPUT_PUSHPULL to reenable it:
// Disable the timer output pin
hal_gpio_config_t gpio_config;
gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;
gpio_config.output_type = HAL_GPIO_OUTPUT_OPENDRAIN;
gpio_config.pull = HAL_GPIO_PULL_NO;
gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;
gpio_config.alternate = HAL_GPIO_AF_2;
HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_6, &gpio_config);
// Enable the timer output pin
hal_gpio_config_t gpio_config;
gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;
gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
gpio_config.pull = HAL_GPIO_PULL_NO;
gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;
gpio_config.alternate = HAL_GPIO_AF_2;
HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_6, &gpio_config);This worked because I was using a N-CHANNEL MOSFET at the output of the timer pin which had the drain pulled low with a resistor. By setting the STM32 the output_type to HAL_GPIO_OUTPUT_OPENDRAIN the output is unable to drive the MOSFET high so the output is practically disabled.
I did not try to set the mode to HAL_GPIO_MODE_OUTPUT and drive the pin low. That may work as well for a more general solution.
Regards,
Gil
2026-04-19 7:42 PM
One line of code, modifying the timer's CCER, is everything you need.
If you prefer to switch between push-pull and open drain, write the GPIOx->OTYPER register.
In both cases, register access is faster to execute and easier to type in your source code. And it works the same in all STM32 series other than STM32F1.
2026-04-19 11:02 PM
Hi @gbm ,
Thank you for your reply!
My hope was that CCER would work but in my application it didn't. My application uses TIM5 to generate a 38KHz clock that serves as input to TIM1 which counts TIM5 pulses. I wanted to turn off the TIM5 output to the pin when TIM1 has reached a certain count, keep counting with TIM1 while the TIM5 output is turned off and then turn TIM5's output back on when TIM1 reached another count. I used the LL_TIM_CC_DisableChannel and LL_TIM_CC_EnableChannel (which change the CCER register) functions to turn the TIM5 channel on and off. The problem is that writing to CCER also turns on/off the input to TIM1. My conclusion was that CCER doesn't just turn off the output of TIM5 to the pin; it also turns of the output to other timers. This is why I turned to GPIO_Init for help.
Your suggestion to optimize the code by writing to OTYPER works great. I used LL_GPIO_SetPinOutputType instead of GPIO_init and it is far faster and more elegant.
Thank you for the help,
-Gil