//外部中断初始化函数
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);//外部中断,需要使能AFIO时钟
KEY_Init();//初始化按键对应io模式
//GPIOB.0 中断线以及中断初始化配置
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
}
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE);//使能PORTA,PORTB,PORTC时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PB0,WK_UP
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置成下拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB0
}
void EXTI0_1_IRQHandler(void)
{
for(EXTI_DELAY=0;EXTI_DELAY<1000;EXTI_DELAY++);
if(WK_UP==1)
{
LED2_TOGGLE(); 闪灯
}
EXTI_ClearFlag(EXTI_Line0); //清除EXTI0线路挂起位
}
//外部中断初始化函数
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);//外部中断,需要使能AFIO时钟
KEY_Init();//初始化按键对应io模式
//GPIOB.0 中断线以及中断初始化配置
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
}
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE);//使能PORTA,PORTB,PORTC时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PB0,WK_UP
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置成下拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB0
}
void EXTI0_1_IRQHandler(void)
{
for(EXTI_DELAY=0;EXTI_DELAY<1000;EXTI_DELAY++);
if(WK_UP==1)
{
LED2_TOGGLE(); 闪灯
}
EXTI_ClearFlag(EXTI_Line0); //清除EXTI0线路挂起位
}
举报