Reading Encoder pulses using external interrupts
I'm using STM32L451VCI6 MCU to read pulses from a rotary encoder attached to the shaft of a stepper motor using external interrupts. I'm incrementing a counter at rising edge of interrupt.
According to spec of the encoder, I should be able to read 400 pulses per revolution. but for lower speeds, the count pulses are more than expected. Am I reading the pulses wrong?
This is how I run the motor
HAL_GPIO_WritePin(GPIOA, Direction_Pin, GPIO_PIN_RESET);
for(int i=0;i<200;i++)
{
HAL_GPIO_WritePin(GPIOA, M1_STP_Pin, GPIO_PIN_SET);
delay_us(900);
HAL_GPIO_WritePin(GPIOA, M1_STP_Pin, GPIO_PIN_RESET);
delay_us(900);
}
void delay_us (uint32_t us)
{
__HAL_TIM_SET_COUNTER(&htim1,0);
while (((uint16_t)__HAL_TIM_GET_COUNTER(&htim1)) < us);
//uint32_t p = TIM1->CNT;
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == ENC_A_Pin)
{
if(HAL_GPIO_ReadPin(GPIOA, ENC_A_Pin) == GPIO_PIN_SET)
{
counter++;
}
}
}
For delays less than 900us, I'm getting 400 counts/rotation. But not for 900 or greater. Any help is appreciated
