STM32三个按键触发三个外部中断,设置成一个中断时正常,三个中断都配置,就会不工作。
时间:10-02
整理:3721RD
点击:
程序如下:
void exti_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin=k_left|k_down|k_right;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //′ò?aEXTI2μ?è????D??
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //?à??ó??è???a0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //?ìó|ó??è???a0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1?ü
NVIC_Init(&NVIC_InitStructure);
// GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //′ò?aEXTI2μ?è????D??
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //?à??ó??è???a0
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //?ìó|ó??è???a0
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1?ü
// NVIC_Init(&NVIC_InitStructure);
//
// GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);
// //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //′ò?aEXTI2μ?è????D??
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //?à??ó??è???a0
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //?ìó|ó??è???a0
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1?ü
// NVIC_Init(&NVIC_InitStructure);
//
/* éè??ía2??D??μ??£ê? */
EXTI_InitStructure.EXTI_Line=EXTI_Line2|EXTI_Line3|EXTI_Line4;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void exti_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin=k_left|k_down|k_right;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //′ò?aEXTI2μ?è????D??
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //?à??ó??è???a0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //?ìó|ó??è???a0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1?ü
NVIC_Init(&NVIC_InitStructure);
// GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //′ò?aEXTI2μ?è????D??
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //?à??ó??è???a0
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //?ìó|ó??è???a0
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1?ü
// NVIC_Init(&NVIC_InitStructure);
//
// GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);
// //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //′ò?aEXTI2μ?è????D??
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //?à??ó??è???a0
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //?ìó|ó??è???a0
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1?ü
// NVIC_Init(&NVIC_InitStructure);
//
/* éè??ía2??D??μ??£ê? */
EXTI_InitStructure.EXTI_Line=EXTI_Line2|EXTI_Line3|EXTI_Line4;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
拜托各位大神看看,哪里出错了
其他的不知道,但是下面这个
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);
// //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //′ò?aEXTI2μ?è????D??
用的是GPIO_PinSource4,但是NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; 却用的是2,应该用EXTI4_IRQn
改成了通道4也是不行,只有一个中断的时候才有用
三个都设置的时候,是三个中断都不能用,还是只有一个能用。
另外是不是中断处理程序的问题,三个不同的引脚中断,需要三个中断处理程序。
如果每次设置一个中断,都能用,功能都实现了,说明中断服务函数没问题,如果三个中断一起加进去编译,编译没错误,但是不能正常工作
每次设置一个中断,三个中断都能正常工作,中断服务函数没问题,但是如果三个中断一起加进来编译,没错误,但是不工作。
每次设置一个中断,三个中断都能正常工作,中断服务函数没问题,但是如果三个中断一起加进来编译,没错误,但是不工作。