2023-07-26 11:56 PM
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?
2023-07-27 01:47 AM
You've changed ARR and PSC in that second case, I presume. How exactly?
JW
2023-07-27 03:39 AM
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.
2023-07-27 04:07 AM
Oh, I overlooked the (ARR=1). That indeed explains it.
JW