本人在使用外部中断读取键值的时候遇见了无法进入外部中断的问题,我的问题是这样的:一开始想用STM32F103C8T6的PB12和PB14读取两个按键的状态,并且是使用外部中断读取,但是总是遇到一个按键的状态可以读取(即按键按下,GPIO口的电平发生跳变,然后进入对应的中断服务函数),但是另一个按键的状态并不能读取(即按键按下,但无法进入对应的中断服务函数)。下面是无法正常进入中断的程序,如图1所示。
#include "stm32f10x.h" // Device header
uint8_t KeyNum;
void Key_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource12|GPIO_PinSource14);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line12 | EXTI_Line14;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}uint8_t KeyNum_Get(void)
{
return KeyNum;
}void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line12) == SET)
{
KeyNum = 3;//红色按键按下
}
else if(EXTI_GetITStatus(EXTI_Line14) == SET)
{
KeyNum = 4;//绿色按键按下
}
EXTI_ClearITPendingBit(EXTI_Line12 | EXTI_Line14);
}
图1
然后我又换了另外两个GPIO口(PA4和PA6)来测试是否可以正常进入外部中断,程序的思想和结构与图1的基本一致,但是没想到竟然可以正常进入中断读取两个按键的状态,代码如下图2所示。
#include "stm32f10x.h" // Device header
uint8_t KeyNum;
void Key_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4|GPIO_PinSource6);EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line4 | EXTI_Line6;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}uint8_t KeyNum_Get(void)
{
return KeyNum;
}void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4) == SET)
{
KeyNum = 3;//红色按键按下
}
EXTI_ClearITPendingBit(EXTI_Line4); //清除外部中断4标志位
}void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line6) == SET)
{
KeyNum = 4;//绿色按键按下
}
EXTI_ClearITPendingBit(EXTI_Line6); //清除外部中断6标志位
}
图2
对比图1和图2,我们可以发现图1程序中的PB12和PB14共用一个中断服务函数,但图2中的PA4和PA6分别用的两个中断服务函数,猜测图1是外部中断线配置得有问题,所以导致不能正常进入外部中断,然后我就开始调整图1外部中断线的配置(GPIO_EXTILineConfig函数)。
将
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12|GPIO_PinSource14);
改为:
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
此时程序就可以正常进入外部中断,至于为什么出现这种情况,本人暂时还没有找到原因。