1. 程式人生 > >stm32外部中斷實驗

stm32外部中斷實驗

// 上一篇是關於串列埠通訊的,用到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);
}