我有几个 GPIO 引脚配置为输入和按钮。按钮的中断效果很好(我需要添加一些去抖动)。但有时当按下按钮时,或者如果我只是用手指触摸插头引脚,其他 gio 中断就会触发。
gio 引脚配置有下拉电阻并设置为在上升沿触发。
为简单起见,我删除了我正在使用的大部分 gio 引脚,只显示了一个,但我为输入配置了以下内容。
PA4、PA7、PA10、PB6、PB8、PB9
// Enable GPIO Peripheral clock
RCC->AHB1ENR |= BLINK_RCC_MASKx(BLINK_PORT_NUMBER);
GPIO_InitTypeDef GPIO_InitStruct;
/*Configure GPIO pin : PC13 - Button input*/
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
//Init LED
/*Configure GPIO pin : PA5 - LD2 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}
void EXTI9_5_IRQHandler(void)
{
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_7) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_7);
//Do something
#ifdef DEBUG
trace_printf("PA7 interrupt. Tick count: %u Counts\n", xTaskGetTickCount());
#endif
}
void EXTI15_10_IRQHandler(void)
{
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_10) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_10);
//Do something
#ifdef DEBUG
trace_printf("PA10 interrupt. Tick count: %u Counts\n", xTaskGetTickCount());
#endif
}
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_13) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_13);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
trace_printf("User Button Pushed. Tick count: %u Counts\n", xTaskGetTickCount());
}
}