stm32四个按键扫描的代码程序,要求有消抖,非延时
时间: 2024-04-16 17:26:18 浏览: 94
下面是一个带有消抖功能的STM32四个按扫描代码程序,使用了外部中断来实现,而不是延时方式:
```c
#include "stm32f4xx.h"
#define KEY1_PIN GPIO_Pin_0
#define KEY2_PIN GPIO_Pin_1
#define KEY3_PIN GPIO_Pin_2
#define KEY4_PIN GPIO_Pin_3
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void Delay(uint32_t delay);
int main(void)
{
GPIO_Configuration();
EXTI_Configuration();
while (1)
{
// 主循环中执行其他操作
// 检测按键是否按下
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
// 处理按键1的操作
EXTI_ClearITPendingBit(EXTI_Line0);
}
else if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
// 处理按键2的操作
EXTI_ClearITPendingBit(EXTI_Line1);
}
else if (EXTI_GetITStatus(EXTI_Line2) != RESET)
{
// 处理按键3的操作
EXTI_ClearITPendingBit(EXTI_Line2);
}
else if (EXTI_GetITStatus(EXTI_Line3) != RESET)
{
// 处理按键4的操作
EXTI_ClearITPendingBit(EXTI_Line3);
}
// 主循环中执行其他操作
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
// 配置按键引脚为输入模式
GPIO_InitStructure.GPIO_Pin = KEY1_PIN | KEY2_PIN | KEY3_PIN | KEY4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
// 配置外部中断线0-3对应的引脚
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource1);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource2);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource3);
// 配置外部中断线0-3为下降沿触发
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// 配置外部中断线0-3的中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Delay(uint32_t delay)
{
while (delay--);
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
Delay(10000); // 消抖延时
if (GPIO_ReadInputDataBit(GPIOA, KEY1_PIN) == 0)
{
// 处理按键1的操作
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
Delay(10000); // 消抖延时
if (GPIO_ReadInputDataBit(GPIOA, KEY2_PIN) == 0)
{
// 处理按键2的操作
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line2) != RESET)
{
Delay(10000); // 消抖延时
if (GPIO_ReadInputDataBit(GPIOA, KEY3_PIN) == 0)
{
// 处理按键3的操作
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI3_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line3) != RESET)
{
Delay(10000); // 消抖延时
if (GPIO_ReadInputDataBit(GPIOA, KEY4_PIN) == 0)
{
// 处理按键4的操作
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
```
这段代码使用了外部中断来实现按键扫描,以避免使用延时方式进行消抖。首先,在`GPIO_Configuration`函数中,配置了按键引脚为输入模式。然后,在`EXTI_Configuration`函数中,配置了外部中断线0-3对应的引脚,并设置触发方式为下降沿触发。同时,还配置了外部中断的优先级。在中断服务函数中,使用了一个简单的延时函数来进行消抖,并通过读取引脚状态来判断按键是否按下,然后执行相应的操作。请根据你的实际硬件连接和需求进行适当修改。
阅读全文