cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303 Timer - Count Mismatch

PR.19
Associate II

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?

 

3 REPLIES 3

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

JW

TDK
Guru

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".

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

JW