cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Encoder Mode, change HAL Filter after initialization

lk.dgironi
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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".

View solution in original post

6 REPLIES 6
TDK
Guru

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".

Thank you!

I've tested and I can confirm changing the filter and polarity works without de-initialization. So updating the filter and polarity bit of htim->Instance->CCMR1 and htim->Instance->CCER works.
I would like to change the Clock Division too, so change the CLKD bit of CR1 register. It seems to works updating htim->Instance->CR1, but I'm not sure. I can not find information about this on reference manual.

GOmon.1
Associate III

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);
}

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);

 

Thank you 

That is much more elegant.