cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H745 - Encoder Mode Not Counting

LZ_FS
Associate
I am trying to interface the STM32H747 with a quadrature encoder-equipped motor to track linear movement. When moving forward, I expect the counter to increment. When moving backwards, I expect the counter to decrement. To do this, I have configured Channels 1 and 2 for TIM5, which is in encoder mode, as follows:
 

 

/ * TIM5 init function */
void MX_TIM5_Init(void)
{
  /* USER CODE BEGIN TIM5_Init 0 */
  /* USER CODE END TIM5_Init 0 */

  TIM_Encoder_InitTypeDef sConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM5_Init 1 */

  /* USER CODE END TIM5_Init 1 */
  htim5.Instance = TIM5;
  htim5.Init.Prescaler = 0;
  htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim5.Init.Period = 4294967295;
  htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

  sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
  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;

  if (HAL_TIM_Encoder_Init(&htim5, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM5_Init 2 */

  /* USER CODE END TIM5_Init 2 */
}

 

 

GPIO pins:

 

void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* tim_encoderHandle)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(tim_encoderHandle->Instance==TIM5)
  {

    /* USER CODE BEGIN TIM5_MspInit 0 */

    /* USER CODE END TIM5_MspInit 0 */

    /* TIM5 clock enable */
    __HAL_RCC_TIM5_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /**TIM5 GPIO Configuration
    PA0_C     ------> TIM5_CH1
    PA1_C     ------> TIM5_CH2
    */

    GPIO_InitStruct.Pin = QUA_Pin|QUB_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USER CODE BEGIN TIM5_MspInit 1 */
    /* USER CODE END TIM5_MspInit 1 */
  }
}

 

 
Starting the timer as follows:

 

int main(void)
{
    ....

    MX_TIM5_Init();

    .....
    HAL_TIM_Encoder_Start(&htim5, TIM_CHANNEL_ALL);
    ....

}

 

 
I am accessing the CNT register via:

 

TIM5->CNT;

 

 
The problem I am facing is the CNT register will not update despite feeding the channels 3.3V Square Waves offset by 90 degrees. In terms of questions,
 
1) Am I understanding the capabilities of the encoder mode correctly? Can the counter increment then immediately start decrementing once the phase of the input channels changes before reaching the maximum ARR value?
 
2) What could cause this unexpected behaviour/lack of CNT updating? Is there an issue with the configuration?

Any help is appreciated!

 

 

0 REPLIES 0