Skip to main content
Graduate II
March 26, 2024
Solved

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

  • March 26, 2024
  • 1 reply
  • 1186 views

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!

 

    This topic has been closed for replies.
    Best answer by Sarra.S

    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! 

     

    1 reply

    Sarra.SAnswer
    ST Employee
    April 23, 2024

    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!