为什么 HAL_TIM_PeriodElapsedCallback 在 HAL_TIM_Base_Start_IT 被调用后立即被调用?
Why HAL_TIM_PeriodElapsedCallback gets called immediately after HAL_TIM_Base_Start_IT called?
我有这个定时器设置
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 80-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_DOWN;
htim2.Init.Period = 4294967295;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
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();
}
然后我像这样设置计数器值并启动计时器
__HAL_TIM_SET_COUNTER(&htim2, 10000000);
HAL_TIM_Base_Start_IT(&htim2);
当我第一次这样做时 HAL_TIM_PeriodElapsedCallback
立即被调用。只有后续调用才能按预期工作。为什么会这样以及如何避免这种情况?
必须在 运行 HAL_TIM_Base_Start_IT
之前从 SR 寄存器清除 TIM_IT_UPDATE 位。使用 HAL:__HAL_TIM_CLEAR_IT(&htim2 ,TIM_IT_UPDATE)
我有这个定时器设置
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 80-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_DOWN;
htim2.Init.Period = 4294967295;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
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();
}
然后我像这样设置计数器值并启动计时器
__HAL_TIM_SET_COUNTER(&htim2, 10000000);
HAL_TIM_Base_Start_IT(&htim2);
当我第一次这样做时 HAL_TIM_PeriodElapsedCallback
立即被调用。只有后续调用才能按预期工作。为什么会这样以及如何避免这种情况?
必须在 运行 HAL_TIM_Base_Start_IT
之前从 SR 寄存器清除 TIM_IT_UPDATE 位。使用 HAL:__HAL_TIM_CLEAR_IT(&htim2 ,TIM_IT_UPDATE)