这里是基于正点原子开发板的学习记录。
首先你要加入固件库
stm32f10x_exti.h 和 stm32f10x_exti.c
1、STM32中每个io口都可以作为外部中断的中断输入口。
2、STM32F103的中断控制器支持19个外部中断/事件请求:
线 0~15:对应外部 IO 口的输入中断。
线 16:连接到 PVD 输出。
线 17:连接到 RTC 闹钟事件。
线 18:连接到 USB 唤醒事件。
不同滴事件咱就滴选不同滴线。
GPIO 的管教 GPIOx.0~GPIOx.15(x=A,B,C,D,E,F,G)分别对应中断线 0~15。
这样我们就可以吧所有IO口控制起来啦。
所以呢简简单单,咱们就配置GPIO与中断线配置起来就行了啊
运用函数GPIO_EXTILineConfig()
例如:GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
这里我们先写exti.h
#ifndef __EXTI_H
#define __EXIT_H
#include "sys.h"
void EXTIX_Init(void);//ÍⲿÖжϳõʼ»¯¡
#endif
再写exti.c文件
#include "exti.h"
#include "led.h"
#include "key.h"
#include "delay.h"
#include "usart.h"
#include "beep.h"
#include "stm32f10x_exti.h"
//ÏÈд¶ÔÄÇЩÏß×öÖжÏ
//ÔÙдÖж϶ÔÓ¦µÄÓÅÏȼ¶
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
KEY_Init(); //°´¼ü¶Ë¿Ú³õʼ»¯
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//ʹÄܸ´Óù¦ÄÜʱÖÓ
//ÏÖÔÚ¶ÔGPIOE.2 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
///ͬÑùµÄAÀïÃæÅäÖÃÒ»´Î¾ÍÐУ¬ÅäÖúóAÀïÃæÓõ½µÄÏ߶¼ÊÇϽµÑØ´¥·¢
EXTI_InitStructure.EXTI_Line=EXTI_Line2; //KEY2
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//¶ÔGPIOE.3
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
EXTI_InitStructure.EXTI_Line=EXTI_Line3;
EXTI_Init(&EXTI_InitStructure);
//¶ÔGPIOE.4
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_Init(&EXTI_InitStructure);
//GPIOA.0 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ÉÏÉýÑسý·¨ PA0 WE_UP
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);
//ÕâÀïµÄ0¶ÔӦǰÃæдµÄÖжϵÄEXTI_Line0
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //ʹÄÜ°´¼üWK_UPËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
//ÕâÀïµÄ0¶ÔӦǰÃæдµÄÖжϵÄEXTI_Line2
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //ʹÄÜ°´¼üKEY2ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //×ÓÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
//ÕâÀïµÄ0¶ÔӦǰÃæдµÄÖжϵÄEXTI_Line3
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //ʹÄÜ°´¼üKEY1ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //×ÓÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
//ÕâÀïµÄ0¶ÔӦǰÃæдµÄÖжϵÄEXTI_Line4
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; //ʹÄÜ°´¼üKEY0ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //×ÓÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure); //³õʼ»¯NVIC¼Ä´æÆ÷
}
//ÍⲿÖжÏ0
void EXTI0_IRQHandler(void)
{
delay_ms(10);//Ïû¶¶
if(KEY3==1) //WK_UP°´¼ü
{
BEEP=!BEEP;
}
EXTI_ClearITPendingBit(EXTI_Line0); //Çå³ýLINE0µÄÖжϱêÖ¾Îλ
}
//ÍⲿÖжÏ2
void EXTI2_IRQHandler(void)
{
delay_ms(10);
if(KEY2==0) //°´¼üKEY2
{
LED0=!LED0;
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
//ÍⲿÖжÏ3
void EXTI3_IRQHandler(void)
{
delay_ms(10);//
if(KEY1==0) //°´¼üKEY1
{
LED1=!LED1;
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
void EXTI4_IRQHandler(void)
{
delay_ms(10);
if(KEY0==0) //°´¼üKEY0
{
LED0=!LED0;
LED1=!LED1;
}
EXTI_ClearITPendingBit(EXTI_Line4);
代码中文乱码,所以有图片,
- 第一个函数中断函数
第二个是中断线的标志,取值范围EXTI_Line0~EXTI_Line15
第一个是中断模式,可以选中断 EXTI_Mode_Interrupt 和事 件 EXTI_Mode_Event
第三个是设置触发模式是下降沿触发 EXTI_Trigger_Falling
,和上 升沿触发 EXTI_Trigger_Rising
第四个就是使能
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
了不得啊兄弟,这里我们就已经掌握了第一步了啊。
- 第二个函数
既然是中断,那肯定要设置中断优先级了啊。
设置中断线0的中断优先级。
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能按键外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级 2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //子优先级 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //中断优先级分组初始化
这里写的EXTI0对应前面写的中断里面的EXTI_Line0
也就是说we_up——对应GPIOA.0——对应中断GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
——对应优先级EXTI0_IRQn
- 第三个函数就很简单了
版权声明:本文为CSDN博主「摆烂大王」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45370400/article/details/122942985
这里是基于正点原子开发板的学习记录。
首先你要加入固件库
stm32f10x_exti.h 和 stm32f10x_exti.c
1、STM32中每个io口都可以作为外部中断的中断输入口。
2、STM32F103的中断控制器支持19个外部中断/事件请求:
线 0~15:对应外部 IO 口的输入中断。
线 16:连接到 PVD 输出。
线 17:连接到 RTC 闹钟事件。
线 18:连接到 USB 唤醒事件。
不同滴事件咱就滴选不同滴线。
GPIO 的管教 GPIOx.0~GPIOx.15(x=A,B,C,D,E,F,G)分别对应中断线 0~15。
这样我们就可以吧所有IO口控制起来啦。
所以呢简简单单,咱们就配置GPIO与中断线配置起来就行了啊
运用函数GPIO_EXTILineConfig()
例如:GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
这里我们先写exti.h
#ifndef __EXTI_H
#define __EXIT_H
#include "sys.h"
void EXTIX_Init(void);//ÍⲿÖжϳõʼ»¯¡
#endif
再写exti.c文件
#include "exti.h"
#include "led.h"
#include "key.h"
#include "delay.h"
#include "usart.h"
#include "beep.h"
#include "stm32f10x_exti.h"
//ÏÈд¶ÔÄÇЩÏß×öÖжÏ
//ÔÙдÖж϶ÔÓ¦µÄÓÅÏȼ¶
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
KEY_Init(); //°´¼ü¶Ë¿Ú³õʼ»¯
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//ʹÄܸ´Óù¦ÄÜʱÖÓ
//ÏÖÔÚ¶ÔGPIOE.2 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
///ͬÑùµÄAÀïÃæÅäÖÃÒ»´Î¾ÍÐУ¬ÅäÖúóAÀïÃæÓõ½µÄÏ߶¼ÊÇϽµÑØ´¥·¢
EXTI_InitStructure.EXTI_Line=EXTI_Line2; //KEY2
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//¶ÔGPIOE.3
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
EXTI_InitStructure.EXTI_Line=EXTI_Line3;
EXTI_Init(&EXTI_InitStructure);
//¶ÔGPIOE.4
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_Init(&EXTI_InitStructure);
//GPIOA.0 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ÉÏÉýÑسý·¨ PA0 WE_UP
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);
//ÕâÀïµÄ0¶ÔӦǰÃæдµÄÖжϵÄEXTI_Line0
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //ʹÄÜ°´¼üWK_UPËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
//ÕâÀïµÄ0¶ÔӦǰÃæдµÄÖжϵÄEXTI_Line2
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //ʹÄÜ°´¼üKEY2ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //×ÓÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
//ÕâÀïµÄ0¶ÔӦǰÃæдµÄÖжϵÄEXTI_Line3
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //ʹÄÜ°´¼üKEY1ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //×ÓÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
//ÕâÀïµÄ0¶ÔӦǰÃæдµÄÖжϵÄEXTI_Line4
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; //ʹÄÜ°´¼üKEY0ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //×ÓÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure); //³õʼ»¯NVIC¼Ä´æÆ÷
}
//ÍⲿÖжÏ0
void EXTI0_IRQHandler(void)
{
delay_ms(10);//Ïû¶¶
if(KEY3==1) //WK_UP°´¼ü
{
BEEP=!BEEP;
}
EXTI_ClearITPendingBit(EXTI_Line0); //Çå³ýLINE0µÄÖжϱêÖ¾Îλ
}
//ÍⲿÖжÏ2
void EXTI2_IRQHandler(void)
{
delay_ms(10);
if(KEY2==0) //°´¼üKEY2
{
LED0=!LED0;
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
//ÍⲿÖжÏ3
void EXTI3_IRQHandler(void)
{
delay_ms(10);//
if(KEY1==0) //°´¼üKEY1
{
LED1=!LED1;
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
void EXTI4_IRQHandler(void)
{
delay_ms(10);
if(KEY0==0) //°´¼üKEY0
{
LED0=!LED0;
LED1=!LED1;
}
EXTI_ClearITPendingBit(EXTI_Line4);
代码中文乱码,所以有图片,
- 第一个函数中断函数
第二个是中断线的标志,取值范围EXTI_Line0~EXTI_Line15
第一个是中断模式,可以选中断 EXTI_Mode_Interrupt 和事 件 EXTI_Mode_Event
第三个是设置触发模式是下降沿触发 EXTI_Trigger_Falling
,和上 升沿触发 EXTI_Trigger_Rising
第四个就是使能
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
了不得啊兄弟,这里我们就已经掌握了第一步了啊。
- 第二个函数
既然是中断,那肯定要设置中断优先级了啊。
设置中断线0的中断优先级。
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能按键外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级 2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //子优先级 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //中断优先级分组初始化
这里写的EXTI0对应前面写的中断里面的EXTI_Line0
也就是说we_up——对应GPIOA.0——对应中断GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
——对应优先级EXTI0_IRQn
- 第三个函数就很简单了
版权声明:本文为CSDN博主「摆烂大王」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45370400/article/details/122942985
暂无评论