AnsweredAssumed Answered


Question asked by prieur.jean on Dec 8, 2014
Latest reply on Dec 9, 2014 by prieur.jean

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

I use a PEC11 encoder to control my STM32F4. I have no problem with it, it's working more than perflectly.

But when I replace it with a PEC16 encoder (supposed to be exactly the same electrically) 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)
    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;
    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PREAMPTION_PRIORITY_LOW;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

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;}
                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 PEC16. Do you have any ideas why?

Thanks a lot for your help.