cancel
Showing results for 
Search instead for 
Did you mean: 

INCREMENTAL ENCODER problem

jean_prieur
Associate III
Posted on December 08, 2014 at 17:10

Hello,

I have a big mystery, based on incremental rotary encoders. I use a PEC11encoder

http://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 PEC16encoder

https://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: 0690X00000602z1QAA.jpg 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
4 REPLIES 4
Posted on December 08, 2014 at 17:46

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?
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
Posted on December 08, 2014 at 18:22

Yes they looks the same but it's hard to know, because it's depends of the rotation speed...

ivani
Associate II
Posted on December 08, 2014 at 20:18

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?
jean_prieur
Associate III
Posted on December 09, 2014 at 11:28

Thanks Ivan !

I missed it... This is so small in the datasheet and not logical from Bourns...

But now it works !