cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with Encoder Interface on STM32F429 Discovery board

quangphubk129
Associate
Posted on October 01, 2014 at 08:29

Hi everyone,

I want to use my encoder with 32bit general purpose timer TIM2 on my STM32F429 Discovery board. I've connected pin PA0 ( TIM2_CH1) and pin PA1 ( TIM2_CH2) to my encoder, but it didn't work. The encoder count value returned only 0 and 1. Then I changed pin PA1 to PB3 ( TIM2_CH2), and it works normally. I've tested on my friend's STM32F4 Discovery board, it also works normally when I connected PA0 and PA1 to my encoder. But I don't know why it doesn't work with my board.

Here's my code:

void RCC_Config()

{

   // Enable GPIO clock

   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB,ENABLE);

   // Enable SYS clock

   //RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);

   //Enable TIM

   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

}

void GPIO_Config()

{

   GPIO_InitTypeDef GPIO_InitStructure;

  

   //Encoder chanel

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

   

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

   

   //Connect Timer Pins to AF Pins

   GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);

   GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);

}

void Encoder_Config()

{

   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

 

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;

  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

   

   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

   TIM_ARRPreloadConfig(TIM2, ENABLE);

   TIM_SetAutoreload(TIM2,0xFFFFFFFF);

   

   //Debounce filter

//   TIM_ICInitStruct.TIM_Channel=TIM_Channel_1|TIM_Channel_2;

//   TIM_ICInitStruct.TIM_ICFilter=0x3;

//   TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;

//   TIM_ICInit(TIM2, &TIM_ICInitStruct);

   

   // set them up as encoder inputs

  TIM_EncoderInterfaceConfig (TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

   // turn on the timer/counters

   TIM_ClearFlag(TIM2,TIM_FLAG_Update);

   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

   TIM_SetCounter(TIM2,0);

  TIM_Cmd (TIM2, ENABLE);

}

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

  NVIC_Structure.NVIC_IRQChannel = TIM2_IRQn;

  NVIC_Structure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_Structure.NVIC_IRQChannelSubPriority = 0;

  NVIC_Structure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_Structure);

}

void TIM2_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

  {

      if((TIM2->CR1)&0x10)  // true = giam

         NumEncoder -= 0xFFFFFFFF;

      else

         NumEncoder += 0xFFFFFFFF;  

   

      TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

  }

}

Can anyone help me? I would appreciate any help in determining what my errors are.  Thank you! And sorry for my bad English.

2 REPLIES 2
Posted on October 01, 2014 at 11:23

Check SB2

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
quangphubk129
Associate
Posted on October 02, 2014 at 10:01

Hi clive1,

Thanks for your answer but I think I've still not understood. SB2 on my board is currently being soldered. Do you mean that I have to use soldering iron to de-solder SB2?