一.EXTI简介
1.EXTI:外部中断/事件控制器
何为EXTI的外部?
2.EXTI框图
3.控制EXTI的寄存器
二.编程部分
1.步骤:
- 初始化EXTI线所涉及到的GPIO:开启按键的GPIO -->选择引脚 -->选择引脚模式(输入还是输出) -->选择上拉/下拉与否 -->取地址,使用结构体
- 初始化EXTI:使能SYSCFG时钟,使用EXTI必须使能SYSCFG时钟 -->连接中断源到KEY引脚 -->设置好初始化结构体参数:EXTI Line :中断/事件线–EXTI Mode EXTI模式(中断/事件)–EXTI Trigger :触发(上/下/上下)–EXTI_LineCmd:使能或者失能(IMR/EMR)
- 配置NVIC:
- 编写中断服务函数
中断初始化设置:
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE); //使能SYSCFG时钟
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0); //PA0,WK_UP连接中断线0
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource4); //PE4,KEY3连接中断线4
KEY_Init();
EXTI_InitStruct.EXTI_Line=EXTI_Line4;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling; //KEY3下降沿触发
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt; //中断模式
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStruct);
EXTI_InitStruct.EXTI_Line=EXTI_Line0;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising; //WK_UP上升沿触发,板子上的KEY2
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt; //中断模式
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStructure.NVIC_IRQChannel=EXTI4_IRQn; //外部中断4
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x03; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x02; // 响应优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure); //配置NVIC
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn; //外部中断0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02; //抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x02; // 响应优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
}
本文是观看野火视频学习总结文章。