大家好,
我想在 IRQ 处理程序中禁用定时器中断,然后在主循环中再次启用。然而,在我重新启用中断(设置 UIE 位)后几乎立即触发了中断。我在下面附上了我的代码。如果我禁用并启用计数器而不仅仅是中断,那么代码将正确运行。这是为什么?我试图设置
tiM_CR1_URS 但无济于事。
- void MX_TIM2_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- htim2.Instance = TIM2;
- htim2.Init.Prescaler = 0;
- htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim2.Init.Period = 3333;
- 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();
- }
- }
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- MX_TIM2_Init();
- HAL_TIM_Base_Start_IT(&htim2);
- uint16_t c;
- while (1)
- {
- if(int_count == 1)
- {
- int_count = 0;
- HAL_GPIO_WritePin(GPIOA, TX_pin, 0);
- __HAL_TIM_SET_COUNTER(&htim2, 0);
- __HAL_TIM_ENABLE_IT(&htim2, TIM_IT_UPDATE);
- }
- c = 0xFFFF;
- while(c)
- {
- c--;
- }
- }
- }
- void TIM2_IRQHandler(void)
- {
- if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_UPDATE) != RESET)
- {
- if (__HAL_TIM_GET_IT_SOURCE(&htim2, TIM_IT_UPDATE) != RESET)
- {
- __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_UPDATE);
- if(int_count == 0)
- {
- __HAL_TIM_DISABLE_IT(&htim2, TIM_IT_UPDATE);
- HAL_GPIO_WritePin(GPIOA, TX_pin, 1);
- int_count = 1;
- }
- }
- }
- return;
- }
P/S:附带问题:当我调试代码时,我注意到当我使用 __HAL_TIM_CLEAR_FLAG 清除中断标志时,即使代码显示标志已清除,但在查看定时器寄存器时我仍然看到 UIF 位已设置。有谁知道为什么?