I'm new in stm32, I recently create a project to make a quick pulse on a GPIO pin every second, just to determine the time accuracy by the logic analyzer, however, I always get the time around 0.999,935s instead of one second, I tried everything I searched on the internet, but nothing work, please give a help.
My code was mostly generated by STM32CubeMX, these are the configs of CubeMX
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
HAL_TIM_Base_Start_IT(&htim2);
while (1) { }
}
.
.
.
static void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 799;
htim2.Init.CounterMode = TIM_COUNTERMODE_DOWN;
htim2.Init.Period = 9999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
}
.
.
.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
GPIOB->BSRR = (uint32_t)GPIO_PIN_2 << 16u; // reset bit
}
The time between 2 and 3 is always around 0.999935s how can I fix this error to exactly one second???