Skip to main content
lk.dgironi
Associate III
August 25, 2023
Solved

Timer Encoder Mode, change HAL Filter after initialization

  • August 25, 2023
  • 2 replies
  • 3530 views

Hi all,

I'm using a Timer in Encoder Mode on STM32F103, using the HAL framework on STM32CubeIDE.

I would like to change encoder filter feature (IC1F[3:0]: Input capture 1 filter) depending on the behaviour selected in my MCU operation mode (so IC1Filter and IC2Filter) after the initialization of the timer has been made, so after HAL_TIM_Encoder_Init is called.

  1. Can I do this with no problem
  2. Do I have to Call HAL_TIM_Encoder_DeInit, set the values, then call HAL_TIM_Encoder_Init again if changing one of the values
  3. Do I have to restart the MCU after changing the values, and set it before the first timer initialization (so HAL_TIM_Encoder_Init)

I'm thinking on something like this: if there's no need to reinitialize the timer.

void TIM_Encoder_SetFilters(TIM_HandleTypeDef *htim, uint32_t IC1Filter, uint32_t IC2Filter)
{
  assert_param(IS_TIM_IC_FILTER(IC1Filter));
  assert_param(IS_TIM_IC_FILTER(IC2Filter));
  uint32_t ccmr1 = htim->Instance->CCMR1;
  ccmr1 &= ~(1U << 4) & ~(1U << 5) & ~(1U << 6); // clear bits
  ccmr1 &= ~(1U << 12) & ~(1U << 13) & ~(1U << 14); // clear bits
  ccmr1 &= (IC1Filter << 4U) | (IC2Filter << 12U); // set bits
  htim->Instance->CCMR1 = tmpccmr1;
}

Thanks for your help.

Best answer by TDK

The RM doesn't list any restrictions for changing this value. If there was a restriction, it should be listed there. I would try it, and if it fails, disable the channel before changing the setting.

HAL doesn't need to know that you've changed the setting. Although de-initializing and re-initializing the new settings is definitely the "HAL" way to do it.

You definitely don't need to reset the MCU.

2 replies

TDK
TDKBest answer
Super User
August 25, 2023

The RM doesn't list any restrictions for changing this value. If there was a restriction, it should be listed there. I would try it, and if it fails, disable the channel before changing the setting.

HAL doesn't need to know that you've changed the setting. Although de-initializing and re-initializing the new settings is definitely the "HAL" way to do it.

You definitely don't need to reset the MCU.

"If you feel a post has answered your question, please click ""Accept as Solution""."
lk.dgironi
Associate III
August 28, 2023

Thank you!

GOmon.1
Associate II
January 5, 2024

Hi all Is there an accepted way to zero the Encoder value in the timer as I am writing a 1 axis DRO using TouchGFX

and I have tried doing the following which works but seems dirty...?


void screenView::Zero_Z_Encoder_Value()
{
HAL_TIM_Encoder_Stop_IT(&htim3, TIM_CHANNEL_ALL);
//HAL_TIM_Encoder_Init(&htim3, &sConfig);
MX_TIM3_Init();
HAL_TIM_Encoder_Start_IT(&htim3,TIM_CHANNEL_ALL);
}

lk.dgironi
Associate III
January 5, 2024

Hello GOmon.1,

I'm using the macro below, which sets the CNT register of the timer instance to zero

__HAL_TIM_SET_COUNTER(&htim3, 0);

 

GOmon.1
Associate II
January 5, 2024

Thank you 

That is much more elegant.