stm32外部中断实验

时间:2021-11-30 19:35:33

// 上一篇是关于串口通信的,用到GPIO的复用,将GPIO复用为usart串口;

// 此处是利用按键进行中断处理,这里配置GPIO模式为输入,因为要接收按键的状态;

//GPIO端口有很多,ABCD....但是中断只有22个,其中0~15个中断中断线与IO端口一一对应,需要配置GPIO与中断线的映射关系,(类似于端口复用)这里利用了函数:SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);

// 这里需要注意,使用的外部中断,先打开SYSCFG时钟,不然没法实现GPIO与中断线的映射。


#include "stm32f4xx.h"


void init_led(void);
void delay(void);


void key_init(void);
void exit_init(void);


void EXTI0_IRQHandler(void);                                                                       // 中断处理函数
void EXTI2_IRQHandler(void);
void EXTI3_IRQHandler(void);
void EXTI4_IRQHandler(void);


int  main(){
init_led();
key_init();
exit_init();

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
while(1){
delay();  
GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
delay();
}
}


void delay(void){
int i,j;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++)
{}
}


void init_led(void){ 
GPIO_InitTypeDef GPIO_InitStruct;


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//这里是配置LED灯
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed=GPIO_Fast_Speed;


GPIO_Init(GPIOF, &GPIO_InitStruct);
}


void  key_init(void){
GPIO_InitTypeDef GPIO_InitStruct;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//KEY0,KEY1,KEY2
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;              // 这里的输入为按键状态
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed=GPIO_Fast_Speed;


GPIO_Init(GPIOE, &GPIO_InitStruct);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//KEY0,KEY1,KEY2
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_InitStruct.GPIO_Speed=GPIO_Fast_Speed;


GPIO_Init(GPIOA, &GPIO_InitStruct);


}


void exit_init(void){
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;

key_init();

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE); 

SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource2);// GPIO与中断线的映射
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource3);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

EXTI_InitStruct.EXTI_Line=EXTI_Line2|EXTI_Line3|EXTI_Line4;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;

EXTI_Init(&EXTI_InitStruct);

EXTI_InitStruct.EXTI_Line=EXTI_Line0;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStruct);


NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn;  // 中断分组
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0X02;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=0X02;
NVIC_Init(&NVIC_InitStruct);

NVIC_InitStruct.NVIC_IRQChannel=EXTI2_IRQn;  //
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0X02;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=0X02;
NVIC_Init(&NVIC_InitStruct);

NVIC_InitStruct.NVIC_IRQChannel=EXTI3_IRQn;  //
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0X02;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=0X02;
NVIC_Init(&NVIC_InitStruct);

NVIC_InitStruct.NVIC_IRQChannel=EXTI4_IRQn;  //
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0X02;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=0X02;
NVIC_Init(&NVIC_InitStruct);

}


void EXTI0_IRQHandler(){  //WAKE_UP
delay();
if((EXTI_GetFlagStatus(EXTI_Line0))!=RESET)
{
GPIO_ResetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
delay();
delay();
// GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
}
EXTI_ClearITPendingBit(EXTI_Line0);
}




void EXTI2_IRQHandler(){  //WAKE_UP
delay();
if((EXTI_GetFlagStatus(EXTI_Line2))!=RESET)
{
GPIO_ResetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
delay();
delay();
GPIO_SetBits(GPIOF,GPIO_Pin_9);
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
void EXTI3_IRQHandler(){  //WAKE_UP
delay();
if((EXTI_GetFlagStatus(EXTI_Line3))!=RESET)
{
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
delay();
delay();
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
void EXTI4_IRQHandler(){  //WAKE_UP
delay();
if((EXTI_GetFlagStatus(EXTI_Line4))!=RESET)
{
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
delay();
delay();
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
}
EXTI_ClearITPendingBit(EXTI_Line4);
}