使用STM32F103ZET6单片机,设置了四个按键为外部中断,为什么按下其中一个按键,会触发两个不同的中断呢
备注:程序如下
#include "exti.h"
/*
函数功能:EXIT初始化
函数形参:NONE
函数返回值:NONE
备注: PA0---EXIT0---UP
PE2---EXIT2---LEFT
PE3---EXIT3---DOWN
PE4---EXIT4---RIGHT
日期:2023/3/13
*/
void Exti_Init(void)
{
//1.配置PA0---EXIT0---UP
Key_Init();//按键初始化
RCC->APB2ENR |= (0x1 << 0);//打开EXIT0对应的AFIO时钟
AFIO->EXTICR[0] &= ~(0xF << 0);//PA0映射到EXIT0
EXTI->RTSR &= ~(0x1 << 0);
EXTI->FTSR |= (0x1 << 0);
EXTI->SWIER &= ~(0x1 << 0);
EXTI->EMR &= ~(0x1 << 0);
EXTI->IMR |= (0x1 << 0);
NVIC_SetPriority(EXTI0_IRQn, NVIC_EncodePriority(7-2, 1, 0));
NVIC_EnableIRQ(EXTI0_IRQn);
// Key_Init();//按键初始化
// RCC->APB2ENR |= (0x1 << 0);//打开EXIT2对应的AFIO时钟
AFIO->EXTICR[0] &= ~(0xF << 8);
AFIO->EXTICR[0] |= (0x4 << 8);//PE2映射到EXIT2
EXTI->RTSR |= (0x1 << 2);
EXTI->FTSR &= ~(0x1 << 2);
EXTI->SWIER &= ~(0x1 << 2);
EXTI->EMR &= ~(0x1 << 2);
EXTI->IMR |= (0x1 << 2);
NVIC_SetPriority(EXTI2_IRQn, NVIC_EncodePriority(7-2, 1, 1));
NVIC_EnableIRQ(EXTI2_IRQn);
// Key_Init();//按键初始化
// RCC->APB2ENR |= (0x1 << 0);//打开EXIT3对应的AFIO时钟
AFIO->EXTICR[0] &= ~(0xF << 12);
AFIO->EXTICR[0] |= (0x4 << 12);//PE3映射到EXIT3
EXTI->RTSR &= ~(0x1 << 3);
EXTI->FTSR |= (0x1 << 3);
EXTI->SWIER &= ~(0X1 << 3);
EXTI->EMR &= ~(0x1 << 3);
EXTI->IMR |= (0x1 << 3);
NVIC_SetPriority(EXTI3_IRQn, NVIC_EncodePriority(7-2, 1, 2));
NVIC_EnableIRQ(EXTI3_IRQn);
// Key_Init();//按键初始化
// RCC->APB2ENR |= (0x1 << 0);//打开EXIT4对应的AFIO时钟
AFIO->EXTICR[1] &= ~(0xF << 0);
AFIO->EXTICR[1] |= (0x4 << 0);//PE4映射到EXIT4
EXTI->RTSR |= (0x1 << 4);//屏蔽上升沿触发
EXTI->FTSR &= ~(0x1 << 4);//开启下降沿触发
EXTI->SWIER &= ~(0x1 << 4);//屏蔽软件中断
EXTI->EMR &= ~(0x1 << 4);//屏蔽事件
EXTI->IMR |= (0x1 << 4);//开启中断屏蔽
NVIC_SetPriority(EXTI4_IRQn, NVIC_EncodePriority(7-2, 1, 3));//设置优先级
NVIC_EnableIRQ(EXTI4_IRQn);//EXIT4使能
}
//编写PA0---EXIT0---UP中断服务函数
void EXTI0_IRQHandler(void)
{
if(!!(EXTI->PR & (0x1 << 0)) == 1) //判断是否是EXIT0请求
{
Delay_ms(10);
EXTI->PR |= (0x1 << 0); //写1清除标志
printf("UP 下降沿触发成功\r\n");
}
}
//编写PE2---EXIT2---LEFT中断服务函数
void EXTI2_IRQHandler(void)
{
if(!!(EXTI->PR & (0x1 << 2)) == 1) //判断是否是EXIT2请求
{
Delay_ms(10);
EXTI->PR |= (0x1 << 2); //写1清除标志
printf("LEFT 上升沿触发成功\r\n");
}
}
//编写PE3---EXIT3---DOWN中断服务函数
void EXTI3_IRQHandler(void)
{
if(!!(EXTI->PR & (0x1 << 3)) == 1) //判断是否是EXIT3请求
{
Delay_ms(10);
EXTI->PR |= (0x1 << 3); //写1清除标志
printf("DOWN 下降沿触发成功\r\n");
}
}
//编写PE4---EXIT4---RIGHT中断服务函数
void EXTI4_IRQHandler(void)
{
if(!!(EXTI->PR & (0x1 << 4)) == 1) //判断是否是EXIT4请求
{
Delay_ms(10);
EXTI->PR |= (0x1 << 4); //写1清除标志
printf("RIGHT 下降沿触发成功\r\n");
}
}