AnsweredAssumed Answered

Encoder, Direction Bit toggles with every Pulse, Count 1 or 0.

Question asked by v.jeff on Apr 16, 2012
Latest reply on Apr 18, 2012 by v.jeff
Hello, I've set up a STM32 with an incremental optical two channel encoder.
The counter direction bit toggles between 0 and 1 every count.  As a result, the counter never gets pass 1.  The counter reads 0,1,0,1,0,1...

I've tried all the counter modes, polarity settings, and channel counting settings.

Has anyone experienced this before?  Any ideas?


void ecdr_TIM2_init(void)
    //Enable bus.
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);   
    //Setting encoder inputs.
    GPIO_init(GPIO_Mode_AF, GPIO_OType_PP, GPIO_Speed_25MHz, GPIO_PuPd_NOPULL, Encoder_TIM2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource1 |  GPIO_PinSource0, GPIO_AF_TIM2);
//  NVIC_InitTypeDef NVIC_InitStructure;                    //Hold intr settings.
    //Enabling interrupts for Timer 5.
//  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);                         //Set control reg.
    TIM_TimeBaseInitTypeDef  TIM_base;
    TIM_base.TIM_Prescaler              = 1;                                    //Prescale.
//  TIM_base.TIM_CounterMode            = TIM_CounterMode_Up;   //Up-counter.  Should be adjusted be Enc interface.
    TIM_base.TIM_Period                     = 0xFFFF;                       //Auto-reload register.
//  TIM_base.TIM_ClockDivision          = 0;            //Filter sampling.
//  //Enable timer.
    TIM_EncoderInterfaceConfig(TIM2,  TIM_EncoderMode_TI12, TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);       //Enable encoder interface.
    TIM_TimeBaseInit(TIM2, &TIM_base);                                      //Set register settings.
    //TIM_PrescalerConfig(TIM2, 1, TIM_PSCReloadMode_Immediate);   
    TIM_EncoderInterfaceConfig(TIM2,  TIM_EncoderMode_TI12, TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);       //Enable encoder interface.
    TIM_Cmd(TIM2, ENABLE); 
//  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);