cancel
Showing results for 
Search instead for 
Did you mean: 

Dual Quadrature encoders

pisaacs9
Associate II
Posted on January 31, 2013 at 08:04

I am trying to read a paid of quadrature encoders, I have managed to get one to read using code copied from this forum but I have been un able to read the second one, I have tried it on different timers (1,3,4) and on different pins but it just doesnt read, can anyone tell me what im doing wrong?

Any help would be greatly appreciated.

  // Encoder 1

    /* Enable GPIO clock */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);

   

  // PC.6,PC.7 for encoder

  GPIO_InitTypeDef GPIO_InitStructurePan;

  GPIO_InitStructurePan.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructurePan.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_InitStructurePan.GPIO_Mode = GPIO_Mode_AF;

  GPIO_Init(GPIOC,&GPIO_InitStructurePan);

   

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8);

   

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructurePan;

  TIM_TimeBaseStructInit(&TIM_TimeBaseStructurePan);

   

   

  TIM_TimeBaseStructurePan.TIM_Period = 0xffff;

  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructurePan);

  TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

   

  TIM8->CNT = 0;

  TIM_Cmd(TIM8, ENABLE);

   // Tilt Encoder 

    /* Enable GPIO clock */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);

   

  // PB.6,PB.7 for encoder

  GPIO_InitTypeDef GPIO_InitStructureTilt;

  GPIO_InitStructureTilt.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructureTilt.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_InitStructureTilt.GPIO_Mode = GPIO_Mode_AF;

  GPIO_Init(GPIOB,&GPIO_InitStructureTilt);

   

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM4);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM4);

   

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructureTilt;

  TIM_TimeBaseStructInit(&TIM_TimeBaseStructureTilt);

   

   

  TIM_TimeBaseStructureTilt.TIM_Period = 0xffff;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructureTilt);

  TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

   

  TIM4->CNT = 0;

  TIM_Cmd(TIM4, ENABLE);

1 REPLY 1
Posted on January 31, 2013 at 09:35

This should be the APB1 routine

   RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);

You just enabled pins 6 and 7, now 4 and 5?

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM4);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM4);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..