先记录几个知识点:
1、STM32的任何一个引脚均可配置成外部中断。
2、PA0/PB0/PC0...等均可任选一个连接到中断线0,PA3/PB2/PC3.....等均可任何一个连接到中断线3。。。。其余pin同理
3、EXTI_Line8的中断服务函数是EXTI9_5_IRQHandler,也即这个服务函数要对[5,9]这5个中断线进行服务,如何判断到底是哪个中断线引发的中断,以8号线为例,可通过这条语句查询:if(EXTI_GetITStatus(EXTI_Line8) != RESET)
下面是一段STM32F407把PA8连接到外部中断线8的例子,可实现跳边沿中断。
再强调一遍,一旦把PA8连接到了外部中断线8,那么PB8/PC8/PD8....就不允许再连接到外部中断线8了。
#define EXTI8_RCC_GPIOA RCC_AHB1Periph_GPIOA
#define EXTI8_GPIOx GPIOA
#define EXTI8_GPIO_Pin_x GPIO_Pin_8
#define EXTI8_PortSourceGPIOx EXTI_PortSourceGPIOA
#define EXTI8_PinSourcex EXTI_PinSource8
#define EXTI8_EXTI_Linex EXTI_Line8
void EXTI8_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
RCC_AHB1PeriphClockCmd(EXTI8_RCC_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = EXTI8_GPIO_Pin_x;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(EXTI8_GPIOx, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI8_PortSourceGPIOx, EXTI8_PinSourcex); //关联GPIOx 和 中断线
EXTI_InitStructure.EXTI_Line = EXTI8_EXTI_Linex;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//上升沿下降沿均中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//外部中断56789的服务函数
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line8) != RESET)//判定是几号线引发的中断
{
EXTI_ClearITPendingBit(EXTI8_EXTI_Linex);
if(Bit_SET == GPIO_ReadInputDataBit(EXTI8_GPIOx, GPIO_Pin_8))//上升沿
{
}
else//下降沿触发
{
}
}
}
先记录几个知识点:
1、STM32的任何一个引脚均可配置成外部中断。
2、PA0/PB0/PC0...等均可任选一个连接到中断线0,PA3/PB2/PC3.....等均可任何一个连接到中断线3。。。。其余pin同理
3、EXTI_Line8的中断服务函数是EXTI9_5_IRQHandler,也即这个服务函数要对[5,9]这5个中断线进行服务,如何判断到底是哪个中断线引发的中断,以8号线为例,可通过这条语句查询:if(EXTI_GetITStatus(EXTI_Line8) != RESET)
下面是一段STM32F407把PA8连接到外部中断线8的例子,可实现跳边沿中断。
再强调一遍,一旦把PA8连接到了外部中断线8,那么PB8/PC8/PD8....就不允许再连接到外部中断线8了。
#define EXTI8_RCC_GPIOA RCC_AHB1Periph_GPIOA
#define EXTI8_GPIOx GPIOA
#define EXTI8_GPIO_Pin_x GPIO_Pin_8
#define EXTI8_PortSourceGPIOx EXTI_PortSourceGPIOA
#define EXTI8_PinSourcex EXTI_PinSource8
#define EXTI8_EXTI_Linex EXTI_Line8
void EXTI8_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
RCC_AHB1PeriphClockCmd(EXTI8_RCC_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = EXTI8_GPIO_Pin_x;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(EXTI8_GPIOx, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI8_PortSourceGPIOx, EXTI8_PinSourcex); //关联GPIOx 和 中断线
EXTI_InitStructure.EXTI_Line = EXTI8_EXTI_Linex;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//上升沿下降沿均中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//外部中断56789的服务函数
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line8) != RESET)//判定是几号线引发的中断
{
EXTI_ClearITPendingBit(EXTI8_EXTI_Linex);
if(Bit_SET == GPIO_ReadInputDataBit(EXTI8_GPIOx, GPIO_Pin_8))//上升沿
{
}
else//下降沿触发
{
}
}
}
举报