2023-08-25 1:40 AM - edited 2023-08-25 1:41 AM
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.
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.
Solved! Go to Solution.
2023-08-25 6:33 AM
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.
2023-08-25 6:33 AM
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.
2023-08-28 6:53 AM
Thank you!
2023-08-31 5:36 AM
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.
2024-01-04 9:52 PM - edited 2024-01-04 9:56 PM
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);
}
2024-01-05 12:02 AM
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);
2024-01-05 2:18 AM
Thank you
That is much more elegant.