gpt4 book ai didi

c - STM32 HAL GPIO中断计数太多

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:34 26 4
gpt4 key购买 nike

我遇到了以下问题:我的电子板上有 2 个输入:

#define TOR1_IN_uC_Port         GPIOC
#define TOR1_IN_uC_Pin GPIO_PIN_5
#define TOR2_IN_uC_Port GPIOE
#define TOR2_IN_uC_Pin GPIO_PIN_6

我有这个初始化:

GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct.Pin = TOR1_IN_uC_Pin;
HAL_GPIO_Init(TOR1_IN_uC_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = TOR2_IN_uC_Pin;
HAL_GPIO_Init(TOR2_IN_uC_Port, &GPIO_InitStruct);

HAL_NVIC_SetPriority(EXTI9_5_IRQn, 2, 1);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);

我有这两个中断函数:

void EXTI9_5_IRQHandler(void)
{
if(__HAL_GPIO_EXTI_GET_IT(TOR1_IN_uC_Pin) != RESET)
{
HAL_GPIO_EXTI_IRQHandler(TOR1_IN_uC_Pin);
__HAL_GPIO_EXTI_CLEAR_IT(TOR1_IN_uC_Pin);
}
if(__HAL_GPIO_EXTI_GET_IT(TOR2_IN_uC_Pin) != RESET)
{
HAL_GPIO_EXTI_IRQHandler(TOR2_IN_uC_Pin);
__HAL_GPIO_EXTI_CLEAR_IT(TOR2_IN_uC_Pin);
}
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == TOR1_IN_uC_Pin)
Input1.Count++;
else if (GPIO_Pin == TOR2_IN_uC_Pin)
Input2.Count++;
}

我在输入端插入了一个 10Hz 的 GBF,但计数器计数太多。当我在串口上打印计数器时,它显示大约 5kHz

当我手动读取输入的状态时,我无法验证输入是否过多。

最佳答案

我也遇到过这个问题,过了一段时间才注意到。这是因为您清除 IRQ 标志非常接近中断返回。创建 tail-chain打断自己。

void EXTI9_5_IRQHandler(void)
{
if(__HAL_GPIO_EXTI_GET_IT(TOR2_IN_uC_Pin) != RESET)
{
HAL_GPIO_EXTI_IRQHandler(TOR2_IN_uC_Pin);
__HAL_GPIO_EXTI_CLEAR_IT(TOR2_IN_uC_Pin);
}
__DMB(); // add this
}

您必须等待总线完成清除操作。 DMB instruction对此有帮助。

Data Memory Barrier acts as a memory barrier. It ensures that all explicit memory accesses that appear in program order before the DMB instruction are observed before any explicit memory accesses that appear in program order after the DMB instruction. It does not affect the ordering of any other instructions executing on the processor.

关于c - STM32 HAL GPIO中断计数太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43914915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com