Skip to main content
azura817
Associate
February 9, 2015
Question

STM32F4 problem with Encoder Interactive od TIM HAL

  • February 9, 2015
  • 2 replies
  • 634 views
Posted on February 09, 2015 at 15:24

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;

}

 

    This topic has been closed for replies.

    2 replies

    waclawek.jan
    Super User
    February 9, 2015
    Posted on February 09, 2015 at 15:57

    > htim4.Init.Prescaler = 65535;

    > htim4.Init.Period = 0;

    I don't speak nor understand the Cube gobbledygook, but if this translates directly to TIMx_PSC and TIMx_ARR settings respectively, it is incorrect and should be the other way round.

    Read the TIm2-TIM5 chapter of RM0090.

    JW
    azura817
    azura817Author
    Associate
    February 11, 2015
    Posted on February 11, 2015 at 07:47

    hello waclawek:

    You're right. My setting Prescaler and Period ,It is incorrect and should be the other way round. I revise this mistake and it can work. Thank you.