Skip to main content
Graduate II
December 16, 2025
Question

STM32G0: Encoder mode + Output Compare

  • December 16, 2025
  • 1 reply
  • 52 views

I am using STM32G071RBT6. I am trying to implement encoder mode + output compare mode using same timer. Right now initially I get OC interrupt even when Tim_CCR3IF flag is 0 and once it get set it remains as it is. 

- Should I combine encoder and OC mode or
- should I use another timer

    This topic has been closed for replies.

    1 reply

    Super User
    December 16, 2025

    In Encoder Mode, the counter is clocked by encoder events, which are, in general, not periodic. In OC mode, one usually wants some periodic output, but your use case may vary. One timer == one clock, is that ok for you?

    Maybe the forum needs more details, see How to write your question to maximize your chance... - STMicroelectronics Community

     hth

    KnarfB

    Graduate II
    December 16, 2025

    @KnarfB 
    In my case the timer counter goes from 0 to 2000 and I want an interrupt when it is at 500. I tried using Output Compare No Output mode but did not accomplish it.
    - First I just toggled an LED and it worked perfectly. I even checked in debug mode and temp_tim_count always remain constant or 500 which was desired count when we needed interrupt.

    void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
    {
    	if((htim->Instance == TIM3) && (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4))
    	{
    		temp_tim_count = __HAL_TIM_GET_COUNTER(&htim3);
    		HAL_GPIO_TogglePin(MP_LED_3_GPIO_Port, MP_LED_3_Pin);
    	}
    }

    - After I wanted to generate PWM pulse using timer 17 which is in one-pulse mode. I debugged the code and the pulse was being generated but not at a particular time but more. While debugging I looked at temp_tim_count and it only went up to 3 and starts again from 0.

    void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
    {
    	if((htim->Instance == TIM3) && (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4))
    	{
    		temp_tim_count = __HAL_TIM_GET_COUNTER(&htim3);
    		HAL_GPIO_TogglePin(MP_LED_3_GPIO_Port, MP_LED_3_Pin);
    		Strobe_trigger();
    	}
    }
    
    void Strobe_trigger(void)
    {
     /* Disable timer */
     __HAL_TIM_DISABLE(&htim17);
    
     /* Reset counter */
     __HAL_TIM_SET_COUNTER(&htim17, 0);
    
     /* Clear update + compare flags */
     __HAL_TIM_CLEAR_FLAG(&htim17,
     TIM_FLAG_UPDATE | TIM_FLAG_CC1);
    
     /* Start one-pulse */
     __HAL_TIM_ENABLE(&htim17);
    }

     - The issue is only when I lines to generate PWM pulse otherwise output compare mode is working fine.