0

I'm using a stm32F429ZGT6 with a 25MHz quartz and i have some difficulty with the speed measurement of my rotary encoder.

I can measure speed until around 35KHz but after the speed seem to reduce even though it is still increasing.

enter image description here This show the number of ticks received in a step of time. The second one is just empty (not related).

Here is how I initialize my timer and the clock.

void MX_TIM1_Init(void)
{
  TIM_Encoder_InitTypeDef sConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 0xFFFF;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  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;
  HAL_TIM_Encoder_Init(&htim1, &sConfig);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

}

void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  __PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
                              |RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

}

Does anyone have any idea why i get this result?

FyFou
  • 1
  • 3
  • 3
    It's probably for the the same reason why wheels sometimes seem to turn too slowly in movies. IOW you're probably not sampling fast enough. – Jabberwocky Oct 29 '18 at 14:21
  • That's what i though but my APB2 bus clock is at 84MHz, it should be enough – FyFou Oct 29 '18 at 14:50
  • 1
    You need to check if your sampling rate is _actually_ fast enough. BTW you didn't actually post the interrupt function that performs the sampling – Jabberwocky Oct 29 '18 at 14:54
  • 1
    The clock initialization code may be an issue, but I seriously doubt it - why would you not choose tt post the code that is actually performing the measurement I wonder.? Personally wouldn't trust any of that HAL stuff; a lot of poorly written bloatware in my experience. What is the unlabeled graph showing us exactly? It seems to bare little correlation to your description. – Clifford Oct 29 '18 at 15:04
  • This is auto-generated code by stm32cubemx, i can show you the code that measure but it's not that interesting void SYS_CoderMeasurment(void) { EncoderSpeed = TIM1->CNT; } I just call this function every 6.66 ms – FyFou Oct 29 '18 at 15:12
  • The unable graph is just because i used an old script python, it is unused. – FyFou Oct 29 '18 at 15:19
  • Have you checked the encoder signals with an oscilloscope? Perhaps the rise and/or fall times are too slow. – Ian Abbott Oct 29 '18 at 16:54
  • 1
    If your code is trying to handle interrupts at encoder rate that’s going to get sticky sooner or later as the rate increases. 35kHz is about 60us period, how long does your higher-priority ISR(s) take to execute? – DisappointedByUnaccountableMod Oct 29 '18 at 22:09
  • Thats a nice advice, gonna look at it. – FyFou Oct 30 '18 at 11:05

0 Answers0