Skip to main content
PR.19
Associate II
July 27, 2023
Question

STM32F303 Timer - Count Mismatch

  • July 27, 2023
  • 3 replies
  • 962 views

Hi

Im using TIM6 on the STM32F303 MCU with clock 64 MHz and PSC 63999.

If the ARR set to 65000 and in non- interrupt mode, the below code

 

HAL_TIM_Base_Start(&htim6);
HAL_Delay(500);
HAL_TIM_Base_Stop(&htim6);

 

results in CNT value of ~500 as expected.

However in interrupt mode and using the below code and callback,

 

 HAL_TIM_Base_Start_IT(&htim6);
 HAL_Delay(500);
 HAL_TIM_Base_Stop_IT(&htim6);


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
 /* Prevent unused argument(s) compilation warning */
 if(htim->Instance==TIM6)
 {
	 ms_timer_count++;
 }
}

 

the ms_timer_count is at 251. The timer in this case is configured to generate interrupt at 1ms (ARR=1).

The expected result for ms_timer_count is ~500.

What is wrong with the code/configuration?

 

This topic has been closed for replies.

3 replies

waclawek.jan
Super User
July 27, 2023

You've changed ARR and PSC in that second case, I presume. How exactly?

JW

TDK
July 27, 2023

You're off by 1 on your ARR values. Counting starts at 0 and ends at the ARR value, so:

64MHz / (PSC + 1) / (ARR + 1) = 64MHz / (63999 + 1) / (1 + 1) = 500 Hz (not 1kHz).

Note that setting ARR=0 doesn't allow the timer to run, so you'll need to set PSC=31999, ARR=1.

"If you feel a post has answered your question, please click ""Accept as Solution""."
waclawek.jan
Super User
July 27, 2023

Oh, I overlooked the (ARR=1). That indeed explains it.

JW