Skip to main content
navarro
Associate
December 2, 2010
Question

Encoder Problem

  • December 2, 2010
  • 5 replies
  • 1127 views
Posted on December 02, 2010 at 11:47

Encoder Problem

    This topic has been closed for replies.

    5 replies

    mehmet.karakaya
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    hello , do you have oscilloscope ?

    can you look at the A B signals of encoder on the oscilloscope ?

    ( becouse I have the same code like you and I can read encoder with this code )

    second ) is the compiler giving any warnings during compilation ?

    18278441
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Hi,Thiago Navarro

    I copyed your code into my project and it works.Please check if your encoder working properly.By the way,there seems a strange problem with this code,though TIM2 is configed into COUNTUP mode,the TIM2->CNT value keeps decreasing instead of increasing.  

    From: Thiago Navarro

    Posted: Thursday, December 02, 2010 11:47 AM

    Subject: Encoder Problem

    Hey, I am trying to work this encoder interface, but I haven´t had good results. When I  debug my code , roll the encoder and watch the TIM2->CNT, its value is always (0 or 65535) or (0 or 1). Here is my code.

    #include ''stm32f10x_conf.h''

    #include ''misc.h''

    void RCC_Configuration(void);

    void GPIO_Configuration(void);

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    void TIM2init(void) {

      /* Time base configuration */

      TIM_TimeBaseStructure.TIM_Period = 0xffff;

      TIM_TimeBaseStructure.TIM_Prescaler = 0;

      TIM_TimeBaseStructure.TIM_ClockDivision = 0;

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

      TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising );

      TIM_Cmd(TIM2, ENABLE);

    }

    void GPIO_Configuration(void)

    {

      GPIO_InitTypeDef GPIO_InitStructure;

      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

      GPIO_Init(GPIOA, &GPIO_InitStructure);

    }

    void RCC_Configuration(void)

    {

      /* TIM2 clock enable */

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

      /* GPIOC clock enable */

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    }

    int main (void){

    RCC_Configuration();

    GPIO_Configuration();

    TIM2init();

    while(1){

    uint16_t current = TIM2->CNT;

    }

    }

    If someone had any ideas, I will be very glad!!

    thanks

    navarro
    navarroAuthor
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Forget this last question, Of course its doesn´t matter!! I think everything can be a problem... =/

    navarro
    navarroAuthor
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Hey, Thanks for the 

    answers.

     I did what you said, but It look like to work normal. I do not understand why it is happening!!

    There aren´t warnings in my code. Let I ask something... Do I have to roll in a fixed velocity?? Because I am rolling with my hand.

    Thanks again!!

    navarro
    navarroAuthor
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    YEAHHHHHH!!! I got it!! I don´t why its worked but worked =] I only changed the TIM2 to TIM3 ( and the Pins ). Anybody knows why?!

    Thanks =DD