EXTI—外部中断/事件控制器
EXTI(External interrupt/event controller)—外部中断/事件控制器,管理了控制器的 20 个中断/事 件线。每个中断/事件线都对应有一个边沿检测器,可以实现输入信号的上升沿检测和下降沿的 检测。EXTI可以实现对每个中断/事件线进行单独配置,可以单独配置为中断或者事件,以及触 发事件的属性
EXTI 功能框
中断/事件线
硬件设计:
软件设计:
创建两个文件Key.c和Key.h,用来存放按键驱动程序即相关宏定义。
编程要点:
(1)初始化用来产生中断的GPIO。
(2)初始化EXTI。
(3)配置NVIC。
(4)编写中断服务函数。
Key.c文件
#include "Key.h"
#include "Led.h"void Key_Init(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); //开启GPIOE时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //开启AFIO时钟GPIO_InitTypeDef GPIO_InitStruct;GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; //上拉输入GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; //引脚4GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOE,&GPIO_InitStruct);GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4); //选择外部中断线 AFIOEXTI_InitTypeDef EXIT_InitStruct; //定义外部中断结构体EXIT_InitStruct.EXTI_Line = EXTI_Line4; //选择外部中断线EXIT_InitStruct.EXTI_LineCmd = ENABLE; //使能外部中断EXIT_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式EXIT_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发EXTI_Init(&EXIT_InitStruct); //初始化外部中断/*配置NVIC为优先级组*/NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //配置NVICNVIC_InitTypeDef NVIC_InitStruct;NVIC_InitStruct.NVIC_IRQChannel = EXTI4_IRQn; /*配置中断源:按键0*/NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; /*配置抢占优先级:1*/NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; /*配置子优先级:1*/NVIC_Init(&NVIC_InitStruct);
}void Key1_Init(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //开启AFIO时钟GPIO_InitTypeDef GPIO_InitStruct;GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; //上拉输入GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOE,&GPIO_InitStruct);GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3); ///*选择EXTI的信号源*/EXTI_InitTypeDef EXIT_InitStruct; //定义外部中断结构体EXIT_InitStruct.EXTI_Line = EXTI_Line3; //选择外部中断线EXIT_InitStruct.EXTI_LineCmd = ENABLE; //使能外部中断EXIT_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式EXIT_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发EXTI_Init(&EXIT_InitStruct); //初始化外部中断/*配置NVIC为优先级组*/NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //配置NVICNVIC_InitTypeDef NVIC_InitStruct;NVIC_InitStruct.NVIC_IRQChannel = EXTI3_IRQn; /*配置中断源:按键1*/NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; /*配置抢占优先级:1*/NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; /*配置子优先级:2*/NVIC_Init(&NVIC_InitStruct);}void EXTI4_IRQHandler(void)
{/*确认是否产生了EXTI Line中断*/if(EXTI_GetITStatus(EXTI_Line4) != RESET) {LED_ON(); //按键按下灯亮//清除中断标志位EXTI_ClearITPendingBit(EXTI_Line4);}}void EXTI3_IRQHandler(void)
{if(EXTI_GetITStatus(EXTI_Line3) != RESET) //不等于0条件为真{LED_OFF(); //按键按下灯灭EXTI_ClearITPendingBit(EXTI_Line3);}
Key.h文件
#ifndef __KEY_H
#define __KEY_H#include "stm32f10x.h" // Device headervoid Key_Init(void);
void Key1_Init(void);#endif
Led.c文件
#include "Led.h"void LED_Init(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOE, &GPIO_InitStructure);GPIO_SetBits(GPIOE, GPIO_Pin_5);}void LED_ON() //灯亮
{GPIO_ResetBits(GPIOE, GPIO_Pin_5);}void LED_OFF() //灯灭
{GPIO_SetBits(GPIOE, GPIO_Pin_5);}
Led.h文件
#ifndef __LED_H
#define __LED_H#include "stm32f10x.h" // Device headervoid LED_Init(void);
void LED_ON(void);
void LED_OFF(void);#define LED1_TOGGLE {GPIOE->ODR ^=GPIO_Pin_5;} //绿灯状态翻转 异或操作#endif
main.c文件
#include "stm32f10x.h" // Device header
#include "Led.h"
#include "Key.h"int main(void)
{LED_Init();Key_Init();Key1_Init();while(1){}}