IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> STM32中断的使用 -> 正文阅读

[嵌入式]STM32中断的使用

NVIC向量中断控制器

嵌套向量中断控制器(NVIC)和处理器核的接口紧密相连,可以实现低延迟的中断处理和高效地处理晚到的中断

//官方文档
typedef struct			//结构体
{
  uint8_t NVIC_IRQChannel;                    /*!< Specifies the IRQ channel to be enabled or disabled.
                                                   This parameter can be a value of @ref IRQn_Type 
                                                   (For the complete STM32 Devices IRQ Channels list, please
                                                    refer to stm32f10x.h file) */

  uint8_t NVIC_IRQChannelPreemptionPriority;  /*!< Specifies the pre-emption priority for the IRQ channel
                                                   specified in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  uint8_t NVIC_IRQChannelSubPriority;         /*!< Specifies the subpriority level for the IRQ channel specified
                                                   in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  FunctionalState NVIC_IRQChannelCmd;         /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel
                                                   will be enabled or disabled. 
                                                   This parameter can be set either to ENABLE or DISABLE */   
} NVIC_InitTypeDef;

NVIC_PriorityGroupConfig //设置优先级分组:先占优先级和从优先级
NVIC_Init //根据 NVIC_InitStruct 中指定的参数初始化外设 NVIC 寄存器

EXTI外部中断/事件控制器

//官方文档
typedef struct  //结构体
{
  uint32_t EXTI_Line;               /*!< Specifies the EXTI lines to be enabled or disabled.
                                         This parameter can be any combination of @ref EXTI_Lines */
   
  EXTIMode_TypeDef EXTI_Mode;       /*!< Specifies the mode for the EXTI lines.
                                         This parameter can be a value of @ref EXTIMode_TypeDef */

  EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines.
                                         This parameter can be a value of @ref EXTIMode_TypeDef */

  FunctionalState EXTI_LineCmd;     /*!< Specifies the new state of the selected EXTI lines.
                                         This parameter can be set either to ENABLE or DISABLE */ 
}EXTI_InitTypeDef;

EXTI_Init //根据 EXTI_InitStruct 中指定的参数初始化外设 EXTI 寄存器

外部中断服务函数:

GPIO引脚中断标志位中断处理函数
PA0~PG0EXTI0EXTI0_IRQHandler
PA1~PG1EXTI1EXTI1_IRQHandler
PA2~PG2EXTI2EXTI2_IRQHandler
PA3~PG3EXTI3EXTI3_IRQHandler
PA4~PG4EXTI4EXTI4_IRQHandler
PA5~PG5EXTI5EXTI9_5_IRQHandler
~
PA10~PG10EXTI10EXTI15_10_IRQHandler
~
PA15~PG15EXTI15
  • 中断线 0-4 每个中断线对应一个中断函数,中断线 5-9 共用中断函数 EXTI9_5_IRQHandler,中断线 10-15 共用中断函数 EXTI15_10_IRQHandler

代码示例

//bsp_exti.c
#include "bsp_exti.h"
#include <misc.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_exti.h>
#include <stm32f10x_rcc.h>

void PA0_EXTI0_Configuration(void)
{
    //定义结构体
    NVIC_InitTypeDef NVIC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;

    //开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);                     //设置优先级分组
    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;                    //选择中断类型 外部中断线0
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;                  //响应先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                     //使能外设NVIC
    NVIC_Init(&NVIC_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 						
	GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);         //选择GPIO管脚用作外部中断线路

    EXTI_InitStructure.EXTI_Line = EXTI_Line0;                          //使能外部线路0
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;                 //请求模式 中断请求
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;             //触发模式为下降沿触发
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                           //使能外设EXTI
    EXTI_Init(&EXTI_InitStructure);     
    

    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;                    //选择中断类型 外部中断线0
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;                  //响应先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                     //使能外设NVIC
    NVIC_Init(&NVIC_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 						
	GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource5);         //选择GPIO管脚用作外部中断线路

    EXTI_InitStructure.EXTI_Line = EXTI_Line5;                          //使能外部线路0
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;                 //请求模式 中断请求
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;             //触发模式为下降沿触发
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                           //使能外设EXTI
    EXTI_Init(&EXTI_InitStructure);  
}

void EXTI0_IRQHandler(void)
{
    //检测指定的EXTI线路中断请求发生与否 消抖
    if(EXTI_GetITStatus(EXTI_Line0) != RESET)
    {
        GPIOA->ODR ^= GPIO_Pin_1;
        EXTI_ClearITPendingBit(EXTI_Line0);//清除中断标志位
    }


}

void EXTI9_5_IRQHandler(void) 
{
    //检测指定的EXTI线路中断请求发生与否 消抖
    if(EXTI_GetITStatus(EXTI_Line5) != RESET)
    {
        GPIOA->ODR ^= GPIO_Pin_4;
        EXTI_ClearITPendingBit(EXTI_Line5);//清除中断标志位
    }

}
//bsp_exti.h
#ifndef __BSP_EXTI_H__
#define __BSP_EXTI_H__

#include <stm32f10x.h>

void PA0_EXTI0_Configuration(void);

#endif
//效果 PA0 PA5按键响应中断并取反相应LED电平
//main.c
#include "Led_Key.h" //见上篇
#include "bsp_exti.h"
#include "stm32f10x_gpio.h"

int main(void)
{
	Led_Configuration();
	Key_Configuration();
	PA0_EXTI0_Configuration();
	while(1)
	{
		
	}
	
}

…Thank you for reading…______________________Heisenberg_Poppings

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2021-08-07 21:47:58  更:2021-08-07 21:48:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 21:28:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码