void Key_Init(void) { GPIO_InitTypeDef g; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); g.GPIO_Pin=GPIO_Pin_4; g.GPIO_Mode=GPIO_Mode_IPU; GPIO_Init(GPIOE,&g); } void exti_Init(void) { Key_Init(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4); EXTI_InitTypeDef e; e.EXTI_Line=EXTI_Line4; e.EXTI_Mode=EXTI_Mode_Interrupt; e.EXTI_Trigger=EXTI_Trigger_Falling; e.EXTI_LineCmd=ENABLE; EXTI_Init(&e); NVIC_InitTypeDef n; n.NVIC_IRQChannel=EXTI4_IRQn; n.NVIC_IRQChannelPreemptionPriority=1; n.NVIC_IRQChannelSubPriority=0; n.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&n); } void EXTI4_IRQHandler(void) { EXTI_ClearITPendingBit(EXTI_Line4); }
外部中斷的配置步驟以及注意事項
Step1.按鍵初始化 略
Step2.EXTI_Init初始化外部中斷
Step3.使用中斷初始化函數初始化中斷NVIC
注意 中斷號位於stm32f10x.h文件中
Step4.編寫中斷服務函數
注意 中斷服務函數名位於啟動文件中
尤其注意
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);不要忘掉 不要把GPIO_PinSource4 寫錯