AnsweredAssumed Answered

STM32F4 problem with Encoder Interactive od TIM HAL

Question asked by lin.hsuan_sheng on Feb 9, 2015
Latest reply on Feb 11, 2015 by lin.hsuan_sheng
Hello,
I'm learing to use STM32Cube and HAL.
Then I tried to set TIM encoder mode,but it can't work.
This is my code of initialization and function
Can someone tell me ,what mistake I made or what 


important step I missed.
Thank you for your reading and forgive my poor poor 


English.


/* TIM4 init function */
void TIM4_Init(void)
{

  TIM_Encoder_InitTypeDef sConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
     
  GPIO_InitTypeDef GPIO_InitStruct;
     
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);




  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 65535;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = 0;
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
     
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC1Filter = 0;
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC2Filter = 0;
     
  HAL_TIM_Encoder_Init(&htim4, &sConfig);
     
  HAL_TIM_Base_Init(&htim4);
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&htim4,&sMasterConfig);
     
   HAL_TIM_Encoder_MspInit(&htim4);
   HAL_TIM_Encoder_Start(&htim4,TIM_CHANNEL_1);
   HAL_TIM_Encoder_Start(&htim4,TIM_CHANNEL_2); 

}
/*QEI Get Value Function*/

#define QEI_R TIM4->CNT

int32_t Encoder_R;
int16_t E_r_V;

void read_encoder (void)
{
     Encoder_R=QEI_R;
}


 

Outcomes