cancel
Showing results for 
Search instead for 
Did you mean: 

TIMER issue 21,5kHZ instead of 600 kHZ

tingting
Associate II
Posted on October 13, 2015 at 14:53

Hi everyone,

We want to have a 600 kHz frequency timer. We use the Internal Oscillator HSI 16 Mhz.

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

And we have this configuration for the timer6. Presc=2 Period=6 If I well understood, we should have this frequency: TIM_Update_Frequency(Hz) =16000000/(2*14)=571428 But we got 21,5kHZ instead. Thanks!
4 REPLIES 4
Posted on October 13, 2015 at 15:29

Interrupting?

Not sure 600 KHz is viable with a processor running at 16 MHz. The TIM can output a signal at this rate, and you should double check that.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tingting
Associate II
Posted on October 13, 2015 at 16:12

There is no interruption.

Does it mean that my configuration of the prescaler and the period are correct?

I am using stm32l0

Posted on October 13, 2015 at 17:53

Does it mean that my configuration of the prescaler and the period are correct?

Honestly your math looks totally wrong.

Should be 16 MHz / 21 = 761.905 KHz, based on the details you provided.

((2 + 1) * (6 + 1)) = 21

Not using the L0, or HAL/Cube, don't see code setting up the timer base, or where/what you're measuring. Probably should start by confirming what frequency the thing is running at via MCO (PA8)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 13, 2015 at 17:56

Should get close to 600KHz with Prescaler = 0, Period = 26

16000000 / 27 = 592.593 KHz

Want to get closer, you need to pick a processor frequency that's an integer multiple of 600000 Hz
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..