Setting up TIM1 in Encoder Mode
Hi,
I am trying to setup the TIM1 of the STM32F410CBT microcontroller so that TIM1->CNT can measure Channel A and Channel B of a Quadrature Encoder Interface of a motor (the Encoder's datasheet is here, and it came attached with this motor here).
I have played with the TIM1 in Encoder Mode for a bit (based on several tutorials out there), and have scoped out the encoder's Channels A and B to see what the Quadrature Encoder signals look like. Here is a reference image:
In the image above, purple is Channel A, green is Channel B, and yellow indicates STEP signals (going into a motor driver that will increment the motor), and the blue signal indicates DIR (direction of rotation, where high indicates counterclockwise rotation, and low indicates clockwise rotation). The DIR output from the microcontroller also goes into the Motor Driver.
Here is my initialization code for setting up TIM1 in Encoder Mode:
/* TIM1 init function */
void MX_TIM1_Init(void)
{
TIM_Encoder_InitTypeDef sConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
sConfig.IC1Filter = 10;
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
sConfig.IC2Filter = 10;
if (HAL_TIM_Encoder_Init(&htim1, &sConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
}I would like some clarification on some of the configurations above, particularly the ICxPolarity, ICxSelection, ICxPrescaler, and ICxFilter. I have also set it up as TIM_ENCODERMODE_TI12 as it receives input from two channels, and this is the same as setting up SMS (slave mode selection) into SMS = 011: Encoder Mode 3 in the Reference Manual.
- Is the ICxPolarity used to sample rising edge/falling edge signals of Channel A or B? Since I am using TIM_ICPOLARITY_RISING, my TIM1 will only consider rising edges on both channels A and B to affect TIM1->CNT, that holds the position of the motor?
- What is ICxSelection used for? I copied the description provided by the HAL library, but I still don't understand what it refers too. I am also using only IC1 and IC2 am I not?
#define TIM_ICSELECTION_DIRECTTI TIM_CCMR1_CC1S_0 /*!< TIM Input 1, 2, 3 or 4 is selected to be
connected to IC1, IC2, IC3 or IC4, respectively */
#define TIM_ICSELECTION_INDIRECTTI TIM_CCMR1_CC1S_1 /*!< TIM Input 1, 2, 3 or 4 is selected to be
connected to IC2, IC1, IC4 or IC3, respectively */
#define TIM_ICSELECTION_TRC TIM_CCMR1_CC1S /*!< TIM Input 1, 2, 3 or 4 is selected to be connected to TRC */- Is the ICxPrescaler used as a divisor for the number of rising edge (or falling edge or both events) that passes through Channels A or B? If, for example, I configured TIM1 to consider only rising edges, and the Divisor/Prescaler is 2, does that mean that Channel A or B needs to have two rising edges on them so that TIM1 would report a step increase of 1?
- If I use an ICxFilter of 10 for both channels, does that mean that TIM1 needs to see 10 samples of the same value on either channels for it to be considered valid? Is this used for debouncing the quadrature signals (if the signals are noisy)?
I also have a question that is not related to understanding the parameters/configurations above. If I pass one rising edge signal to the STEP input port of the Motor Driver that drives the Motor itself, TIM1->CNT actually increments by two and not by one. Is there a reason why the TIM1->CNT register increases by two with a single step increase?
Any advice would be greatly appreciated. Thank you!