exti.c
#include "exti.h"
#include "key1.h"
#include "delay.h"
#include "bsp-led2.h"
void EXTI_GPIO_Config(void)
{
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //抢占优先级1
KEY_GPIO_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //PE4 Key0 使能复用功能时钟
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4); //IO口与中断的映射关系
EXTI_InitStruct.EXTI_Line=EXTI_Line4; //GPIOx.4映射到EXTI4
EXTI_InitStruct.EXTI_LineCmd=ENABLE; //使能EXTI
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt; //选择模式
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling; //下拉触发
EXTI_Init(&EXTI_InitStruct); //根据端口初始化外设EXTI寄存器
NVIC_InitStruct.NVIC_IRQChannel=EXTI4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE; //使能NVIC
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1; //主优先级为1
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1; //子优先级为1
NVIC_Init(&NVIC_InitStruct); //配置中断分组,使能中断
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //PE3 Key1
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);
EXTI_InitStruct.EXTI_Line=EXTI_Line3;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel=EXTI3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStruct);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //PA0 Key2
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitStruct.EXTI_Line=EXTI_Line0;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=3;
NVIC_Init(&NVIC_InitStruct);
}
void EXTI4_IRQHandler (void) //key0
{
delay_ms(10); //消抖
if(KEY0==0) //判断中断状态,是否发生
{
PBout(5)=!PBout(5); //检测到有按键按下,改变LED状态
}
EXTI_ClearITPendingBit(EXTI_Line4); //清除中断标志位
}
void EXTI3_IRQHandler (void) //key1
{
delay_ms(10);
if(KEY1==0)
{
PEout(5)=!PEout(5);
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
void EXTI0_IRQHandler (void) //key3
{
delay_ms(10);
if(KEY3==0)
{
PEout(5)=!PEout(5);
PBout(5)=!PBout(5);
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
exti.h
#ifndef _EXTI_H
#define _EXTI_H
#include "stm32f10x.h"
void EXTI_GPIO_Config(void);
#endif
主函数
#include "bsp-led2.h"
#include "stm32f10x.h"
#include "key1.h"
#include "delay.h"
#include "sys.h"
#include "rccclkconfig.h"
#include "exti.h"
int main(void)
{
LED_GPIO_Config(); //LED端口初始化
delay_init(); //延迟函数初始化
EXTI_GPIO_Config(); //EXTI端口初始化
KEY_GPIO_Config(); //按键初始化
while(1)
{
//空循环
}
}