2014-12-08 08:10 AM
Hello,
I have a big mystery, based on incremental rotary encoders. I use a PEC11encoderhttp://www.adafruit.com/datasheets/pecpdf
to control my STM32F4. I have no problem with it, it's working more than perflectly. But when I replace it with a PEC16encoderhttps://www.bourns.com/pdfs/pecpdf
(supposed to be exactly the sameelectrically) my code isn't working anymore. I can only get one rotation direction. This is my circuit: PA7 works with an interrupt:void
Interrupt_Config_Line7(
void
)
{
// INIT TYPEDEF
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
//___________ENCODER CHANNEL B___________________________
// Connect EXTI Line7 to PA7 pin
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource7);
// Configure EXTI Line7
EXTI_InitStructure.EXTI_Line = EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PREAMPTION_PRIORITY_LOW;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void
EXTI9_5_IRQHandler(
void
)
{
// Store if the encoder is turned completely to the left or to the right
static
uint8_t Encoder1_turned_left = 0, Encoder1_turned_right = 0;
if
(EXTI_GetITStatus(EXTI_Line7) != RESET)
{
if
(GPIO_ReadInputDataBit(GPIOA, PA6_PEC11_CHB) == 1)
{
if
((GPIO_ReadInputDataBit(GPIOA, PA6_PEC11_CHA) == 1) && (Encoder1_turned_right == 1)) {message(ENCODER_TURNED_RIGHT); Encoder1_turned_right = 0;}
else
if
((GPIO_ReadInputDataBit(GPIOA, PA6_PEC11_CHA) == 0) && (Encoder1_turned_left == 1)) {message(ENCODER_TURNED_LEFT); Encoder1_turned_left = 0;}
}
else
{
if
(GPIO_ReadInputDataBit(GPIOA, PA6_PEC11_CHA) == 1) Encoder1_turned_left = 1;
else
Encoder1_turned_right = 1;
}
EXTI_ClearITPendingBit (EXTI_Line7);
}
}
To resume, when the CHANNEL B of the encoder change of state, the interrupt is activated and the code check the CHANNEL A and channel B state to detect the rotation.
It perfect with PEC11 and not works with PEC Do you have any ideas why?
Thanks a lot for your help.
Jean
2014-12-08 08:46 AM
I have a big mystery, based on incremental rotary encoders. ... (supposed to be exactly the same electrically) my code isn't working anymore. I can only get one rotation direction.
And when you eyeball the signals with a scope do they actually behave the same?2014-12-08 09:22 AM
Yes they looks the same but it's hard to know, because it's depends of the rotation speed...
2014-12-08 11:18 AM
The order of contacts of PEC11 is A-C-B. For PEC16 it is A-B-C.
Did you change the wiring when you changed the encoder?2014-12-09 02:28 AM
Thanks Ivan !
I missed it... This is so small in the datasheet and not logical from Bourns...But now it works !