2
\$\begingroup\$

I'm using an STM32L476RT. I've got encoders based on a TLE4946-2K. I can't get it to work. I'm attaching my code and config below. I'm kind of stuck at this point. I tried to lower the voltage by dividing it with resistors, but it didn't work.

Code:

static void MX_TIM5_Init(void)
{
  /* USER CODE BEGIN TIM5_Init 0 */
  /* USER CODE END TIM5_Init 0 */
    
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
    
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM5);
  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  /**TIM5 GPIO Configuration
  PA0   ------> TIM5_CH1
  PA1   ------> TIM5_CH2
  */
  GPIO_InitStruct.Pin = ENC1A_Pin|ENC1B_Pin;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
  /* USER CODE BEGIN TIM5_Init 1 */
    
  /* USER CODE END TIM5_Init 1 */
  TIM_InitStruct.Prescaler = 0;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 200;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM5, &TIM_InitStruct);
  LL_TIM_DisableARRPreload(TIM5);
  LL_TIM_SetEncoderMode(TIM5, LL_TIM_ENCODERMODE_X4_TI12);
  LL_TIM_IC_SetActiveInput(TIM5, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
  LL_TIM_IC_SetPrescaler(TIM5, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
  LL_TIM_IC_SetFilter(TIM5, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV32_N8);
  LL_TIM_IC_SetPolarity(TIM5, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
  LL_TIM_IC_SetActiveInput(TIM5, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
  LL_TIM_IC_SetPrescaler(TIM5, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
  LL_TIM_IC_SetFilter(TIM5, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV32_N8);
  LL_TIM_IC_SetPolarity(TIM5, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
  LL_TIM_SetTriggerOutput(TIM5, LL_TIM_TRGO_RESET);
  LL_TIM_DisableMasterSlaveMode(TIM5);
  /* USER CODE BEGIN TIM5_Init 2 */
    
  /* USER CODE END TIM5_Init 2 */
}

I'm checking the CNT value with a debugger - 0 change. Checking it with an if statement - 0 effect.

if (TIM5->CNT != 0)
{
  LED_ON(LED_TX);
}

Below is the signal from the encoder:

scope signal of the encoder

Both encoder signals are connected to a μC pin with an 0R resistor in series. On the encoder side there is a 10 kΩ pull-up resistor on the A and B signals.

\$\endgroup\$
2
  • \$\begingroup\$ the datasheet states that the sensor operates down to 2.7 V \$\endgroup\$
    – jsotola
    Commented Sep 21, 2020 at 1:28
  • \$\begingroup\$ the signal looks good ... pick one channel for the clock ... for example, blue channel ... on rising edge of blue, read yellow channel to determine the direction of rotation \$\endgroup\$
    – jsotola
    Commented Sep 21, 2020 at 1:31

1 Answer 1

1
\$\begingroup\$

Switched to HAL, everything is working as it was intended.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ A warm welcome to the site. I think you're using 'Answer' on this site like 'Reply' is used on other discussion sites. New information should be edited into your question, otherwise readers have to piece all of this together to understand it. As this is not an answer, it will most likely be deleted but you can delete it yourself if you like. Thanks. \$\endgroup\$
    – TonyM
    Commented Sep 21, 2020 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.