cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 in Encoder Mode but TIM1->CNT does not change

debug
Associate III

Hi,

I'm interested in using Timer 1 on my STM32F439ZI to decode the quadrature signal from an encoder but TIM1->CNT doesn't appear to change. I'm not sure what is missing, the TIM1 config looks like:

static void MX_TIM1_Init(void)
{
  /* USER CODE BEGIN TIM1_Init 0 */
  /* USER CODE END TIM1_Init 0 */

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

  /* USER CODE BEGIN TIM1_Init 1 */
  /* USER CODE END TIM1_Init 1 */

  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 65535;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC1Filter = 10;
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  sConfig.IC2Filter = 10;
  if (HAL_TIM_Encoder_Init(&htim1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN TIM1_Init 2 */
  /* USER CODE END TIM1_Init 2 */
}

and the *** config is like:

debug_0-1711431610771.png

And I'm just trying to read a value from the timer with (encoder_previous is a uint16_t):

 

if (TIM1->CNT != encoder_previous) {
  encoder_previous = TIM1->CNT;
}

 

which is located in the infinite loop in the StartDefaultTask function but it does not appear to change ever. Does the timer need to be started after initialization or am I missing something in my config?

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Sarra.S
ST Employee

Hello  @debug, sorry for the delayed answer,

I assume you're starting the timer and enabling the clock for TIM1 in the RCC

Maybe check that the encoder signals are reaching the timer, and check the hardware connections with an oscilloscope! 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

1 REPLY 1
Sarra.S
ST Employee

Hello  @debug, sorry for the delayed answer,

I assume you're starting the timer and enabling the clock for TIM1 in the RCC

Maybe check that the encoder signals are reaching the timer, and check the hardware connections with an oscilloscope! 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.