AMT102-V rotary encoder with STM32F407
#define EXTI1_IRQ_PRIO 7
TIM_HandleTypeDef htim1;
int32_t enc1_index = 0;
void enc1_index_callback(void)
{
if (__HAL_TIM_DIRECTION_STATUS(&htim1)==1)
{
enc1_index--;
}
else
{
enc1_index++;
}
__HAL_TIM_SetCounter(&htim1, 0); // reset Counter
HAL_TIM_Encoder_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_Encoder_Start(&htim1,TIM_CHANNEL_2);
}
void EXTI1_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_1) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_1);
enc1_index_callback();
}
}
void init_encoder(){
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_TIM3_CLK_ENABLE();
TIM_Encoder_InitTypeDef sConfig;
sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
sConfig.IC1Filter = 0;
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
sConfig.IC2Filter = 0;
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* Enable and set EXTI Line1 Interrupt to the lowest priority */
HAL_NVIC_SetPriority(EXTI1_IRQn, EXTI1_IRQ_PRIO, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
htim1.Instance = TIM3;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xffff;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
HAL_TIM_Encoder_Init(&htim1, &sConfig);
__HAL_TIM_SetCounter(&htim1, 0);
HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_2);
}I'm trying to get an AMT102-V rotary encoder working together with the TIM3 on my STM32F407 MCU.
For some reason, the timer is not detecting the encoder pulses. The signal looks good on the oscilloscope, the only thing that I see, there is a bit of noise in the low output state of the encoder.
To verify my input pin works, manually pulled the index pin, connected with my EXT1 interrupt, to ground and it was correctly triggered.
Interestingly, when the encoder output is transitioning from high to low, the interrupt is not triggered.
On the electronics side, the only thing between the STM and the encoder is 120R resistor, which shouldn't make any problems.
Am I configuring the GPIOs correctly? What could be the problem?
Thanks,
Alex
