STM32------外部中断

article/2025/9/12 4:48:34

文章目录

  • 前言
  • 一、异常和中断
    • 1.概述
    • 2.异常类型
      • 1)系统异常
      • 2)中断
    • 3.中断控制
  • 二、ARM Cortex-M4 的中断体系
    • 1.定义
    • 2.中断引脚
  • 三、代码思路
    • 1.8051单片机
    • 2.STM32
  • 四、函数接口
    • 1.为引脚选择使用哪个中断
    • 2.配置外部中断
    • 3.中断优先级配置
    • 4.获取外部中断状态
    • 5.清空外部中断标志位
  • 五、中断优先级
    • 1.概述
    • 2.抢占优先级与响应优先级区别
      • 1)高抢占优先级是可以打断正在进行的低抢占优先级的中断。抢占优先级若相同,则不会出现抢占的过程。
      • 2)抢占优先级相同的中断,高响应优先级不可以打断低响应优先级的中断。
      • 3)抢占优先级相同的中断,当两个中断同时发生的情况下,哪个响应优先级高,哪个先执行。
      • 4)抢占优先级相同且响应优先级相同的中断,假如同时发生,会按照硬件内部固定的优先级执行,如下图。
      • 5)无论是抢占优先级还是响应优先级,优先级数值越小,就代表优先级越高。
  • 六、中断服务函数
  • 七、源码例子
  • 总结


前言

STM32------外部中断


提示:以下是本篇文章正文内容,下面案例可供参考

一、异常和中断

1.概述

《Cortex M3与M4权威指南》章节4.5 P104
Exceptions are events that cause changes to program flow. When one happens, the processor suspends the current executing task and executes a part of the program called the exception handler. After the execution of the exception handler is completed, the processor then resumes normal program execution. In the ARM architecture, interrupts are one type of exception. Interrupts are usually generated from peripheral or external inputs, and in some cases they can be triggered by software. The exception handlers for interrupts are also referred to as Interrupt Service Routines (ISR)。

Each exception source has an exception number. Exception numbers 1 to 15 as system exceptions, and exceptions 16 and above are for interrupts. The design of the NVIC in the Cortex-M3 and Cortex-M4 processors can support up to 240 interrupt inputs. However, in practice the number of interrupt inputs imple mented in the design is far less, typically in the range of 16 to 100. In this way the silicon size of the design can be reduced, which also reduces power consumption.

Nested vectored interrupt controller (NVIC)
The NVIC is a part of the Cortex-M processor. It is programmable and its registers are located in the System Control Space (SCS) of the memory map
The NVIC handles the exceptions and interrupt configurations, prioritization, and interrupt masking. The NVIC has the following features:
• Flexible exception and interrupt management
• Nested exception/interrupt support
• Vectored exception/interrupt entry
• Interrupt masking

2.异常类型

1)系统异常

在这里插入图片描述

2)中断

在这里插入图片描述

3.中断控制

After reset, all interrupts are disabled and given a priority-level value of 0. Before using any interrupts, you need to:
Set up the priority level of the required interrupt (this step is optional)
Enable the interrupt generation control in the peripheral that triggers the interrupt
Enable the interrupt in the NVIC

In most typical applications, this is all you need to do. When the interrupt triggers, the corresponding Interrupt Service Routine (ISR) will execute (you might need to clear the interrupt request from the peripheral within the handler). The name of the ISR can be found inside the vector table inside the startup code, which is also provided by the microcontroller vendor.The name of the ISR needs to match the name used in the vector table so that the linker can place the starting address of the ISR into the vector table correctly.

二、ARM Cortex-M4 的中断体系

1.定义

中断,意味着中途打断现在干的事情,要立即处理紧急的事件。
现实的例子:手机玩游戏的时候,突然来电话。在编程当中还常遇到实时接收数据的请求,都使用中断服务函数,示例如下:
在这里插入图片描述

2.中断引脚

多达 140 个 GPIO(STM32F405xx/07xx 和 STM32F415xx/17xx)通过以下方式连接到 16 个外部中断/事件线。
例如:PA0占用了EXTI0,其他PB0~PI0是不能使用的。
在这里插入图片描述
引脚编号决定了对应哪个外部中断。

三、代码思路

1.8051单片机

外部中断的触发方式:低电平触发、下降沿触发 IT0=1
允许外部中断引脚申请中断请求 EX0=1
优先级的配置
中断服务函数

2.STM32

端口A硬件时钟使能
SYSCFG硬件时钟使能
配置引脚的工作模式
将引脚连接到外部中断

中断触发方式:电平触发、边沿触发
允许外部中断引脚申请中断请求
优先级的配置
中断服务函数
注:
中断服务函数是不能被调用,编写格式不能随意编写,这是它特有的存在形式。不同的硬件平台,其编写方法是不一样的。

四、函数接口

1.为引脚选择使用哪个中断

/*** @brief  Selects the GPIO pin used as EXTI Line.* @param  EXTI_PortSourceGPIOx : selects the GPIO port to be used as source for*          EXTI lines where x can be (A..K) for STM32F42xxx/43xxx devices, (A..I) *          for STM32F405xx/407xx and STM32F415xx/417xx devices or (A, B, C, D and H)*          for STM32401xx devices.  *            * @param  EXTI_PinSourcex: specifies the EXTI line to be configured.*           This parameter can be EXTI_PinSourcex where x can be (0..15, except*           for EXTI_PortSourceGPIOI x can be (0..11) for STM32F405xx/407xx*           and STM32F405xx/407xx devices and for EXTI_PortSourceGPIOK x can   *           be (0..7) for STM32F42xxx/43xxx devices. *             * @retval None*/
void SYSCFG_EXTILineConfig(uint8_t EXTI_PortSourceGPIOx, uint8_t EXTI_PinSourcex)

2.配置外部中断

/*** @brief  Initializes the EXTI peripheral according to the specified*         parameters in the EXTI_InitStruct.* @param  EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure*         that contains the configuration information for the EXTI peripheral.* @retval None*/
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)

3.中断优先级配置

/*** @brief  Initializes the NVIC peripheral according to the specified*         parameters in the NVIC_InitStruct.* @note   To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()*         function should be called before. * @param  NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains*         the configuration information for the specified NVIC peripheral.* @retval None*/
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)

4.获取外部中断状态

/*** @brief  Checks whether the specified EXTI line is asserted or not.* @param  EXTI_Line: specifies the EXTI line to check.*          This parameter can be EXTI_Linex where x can be(0..22)* @retval The new state of EXTI_Line (SET or RESET).*/
ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)

5.清空外部中断标志位

/*** @brief  Clears the EXTI's line pending bits.* @param  EXTI_Line: specifies the EXTI lines to clear.*          This parameter can be any combination of EXTI_Linex where x can be (0..22)* @retval None*/
void EXTI_ClearITPendingBit(uint32_t EXTI_Line)

五、中断优先级

中断优先级的一个意义:出现多个中断同时触发,但是不能同时处理,所以先后顺序之分,要根据实际上的运行环境优先处理重要的中断。

1.概述

TM32 对中断优先级进行分组,共 5 组,组 0~4,这些分组是用于指定当前M4支持多少个抢占优先级和多少个响应优先级。同时,对每个中断设置一个抢占优先级和一个响应优先级。函数原型如下:

/*** @brief  Configures the priority grouping: pre-emption priority and subpriority.* @param  NVIC_PriorityGroup: specifies the priority grouping bits length. *   This parameter can be one of the following values:*     @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority    //不支持抢占优先级*                                4 bits for subpriority             //支持16个响应优先级*     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority    //支持2个抢占优先级*                                3 bits for subpriority             //支持8个响应优先级*     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority    //支持4个抢占优先级*                                2 bits for subpriority             //支持4个响应优先级*     @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority    //支持8个抢占优先级*                                1 bits for subpriority             //支持2个响应优先级*     @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority    //支持16个抢占优先级*                                0 bits for subpriority             //不支持响应优先级* @note   When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible. *         The pending IRQ priority will be managed only by the subpriority. * @retval None*/
void NVIC_PriorityGroupConfig(uint32_t, NVIC_PriorityGroup)

只要开机初始化一次就可以了。

2.抢占优先级与响应优先级区别

1)高抢占优先级是可以打断正在进行的低抢占优先级的中断。抢占优先级若相同,则不会出现抢占的过程。

2)抢占优先级相同的中断,高响应优先级不可以打断低响应优先级的中断。

3)抢占优先级相同的中断,当两个中断同时发生的情况下,哪个响应优先级高,哪个先执行。

4)抢占优先级相同且响应优先级相同的中断,假如同时发生,会按照硬件内部固定的优先级执行,如下图。

在这里插入图片描述

5)无论是抢占优先级还是响应优先级,优先级数值越小,就代表优先级越高。

六、中断服务函数

中断服务函数要简单、高效完成,以下的delay函数是为了方便观察中断现象,在实际项目开发过程,是不会这么做的。
在这里插入图片描述

七、源码例子

外部中断源码例子


总结

以上就是今天要讲的内容,本文仅仅简单介绍了STM32------外部中断的使用,而STM32的其他一些使用模块,请各位大神移步本博主其他文章或是关注博主等待后续发布。


http://chatgpt.dhexx.cn/article/98xiDm1Q.shtml

相关文章

STM32-外部中断配置

STM32-外部中断配置 外部中断配置的整体图: 步骤如下: 配置RCC,把我们这里涉及到的外设的时钟都打开,不打开时钟外设是没有办法工作的。配置GPIO,选择我们的端口为输入模式。配置AFIO,选择我们用的这一路GPIO,连接到后面的EXIT。配置EXTI,选…

STM32中断

中断系统 中断:在主程序运行过程中,出现了特定的中断触发条件(中断源),使得单片机暂停当前正在运行的的程序,转而去处理中断程序,处理完成后又返回原来被暂停的位置继续运行。中断优先级&#…

STM32学习:外部中断

文章目录 一、STM32外部中断1、基本知识2、中断设计流程 二、硬件配置1、proteus仿真电路搭建2、CubeMX硬件配置3、配置中断1、寻找中断虚函数2、中断回调函数编写3、proteus仿真测试1、主频配置2、仿真运行 一、STM32外部中断 1、基本知识 1、外部中断(EXTI&…

STM32外部中断的配置方法及步骤

阅读思路:STM32外部中断概述–>外部中断一般配置过程 1、外部中断概述 STM32的每个 I/O口都可以作为外部中断的输入,中断线0~15对应外部I/O口的中断输入,每个外部中断线可以独立配置触发方式(上升沿,下降沿或者双边…

STM32外部中断的关闭和打开

注:库函数没有现成的函数打开和关闭外部中断 在某些时候我们希望暂时的关闭某条外部中断,以免造成误触发,此时可以使用下面的操作: 关闭: EXTI->IMR & ~(EXTI_Linex); 开启: EXTI->IMR | EXTI…

STM32-外部中断学习笔记

中断分类 STM32的EXTI控制器支持19 个外部中断/ 事件请求。每个中断设有状态位,每个中断/ 事件都有独立的触发和屏蔽…

STM32外部中断解析

文章目录 前言一、外部中断是什么二、STM32F103的外部中断三、外部中断的中断号四、HAL库的外部中断初始化流程总结 前言 本篇文章将带大家了解STM32F103的外部中断。 一、外部中断是什么 外部中断是单片机实时地处理外部事件的一种内部机制。当某种外部事件发生时&#xff…

STM32 外部中断详解(原理+配置代码)

本文介绍了STM32基于标准外设库的外部中断配置,以及基于参考手册如何更加寄存器配置外部中断 文章目录 1 前言2 STM32的外部中断3 中断服务函数的映射关系4 外部中断的配置5 寄存器的操作5.1 硬件中断选择5.2 硬件事件选择5.3 软件中断/事件的选择 6 总结 1 前言 打…

STM32-外部中断

1 外部中断概述 EXTI(External interrupt/event controller)—外部中断/事件控制器,和在STM32NVIC中断优先级管理(中断向量表)中讲述的CM3内核的外部中断不同。特指的是,在中断向量表中的EXTI的外部中断。S…

STM32基础之中断--外部中断

EXTI简介 EXTI(External interrupt/event controller)—外部中断/事件控制器,管理了控制器的 19个中断/事件线。每个中断/事件线都对应有一个边沿检测器,可以实现输入信号的上升沿检测和下降沿的检测。EXTI 可以实现对每个中断/事…

stm32外部中断

中断 中断概念程序中断是指在计算机执行现行程序的过场中,出现某些急需处理的异常情况或特殊请求,CPU暂停中断现行程序,而专区对这些异常情况或特殊情况进行处理,在处理完毕后CPU又自动返回到现行程序的断点处,继续执…

【STM32学习笔记】(13)——外部中断详解

EXTI 简介 EXTI(External interrupt/event controller)—外部中断/事件控制器,管理了控制器的 20 个中断/事件线。每个输入线可以独立地配置输入类型(脉冲 或挂起)和对应的触发事件(上升沿或下降沿或者双边沿都触发)。EXTI 可以实现对每个中断…

STM32 外部中断

目录 一、简单介绍外部中断 二、外部中断结构图 三、外部中断配置 一、简单介绍外部中断 中断即打断,实至CPU再执行当前程序时,由于系统出现了某种需要处理的紧急情况,CPU暂停正在执行的程序,转而去执行另一段特殊程序来处…

(十四)STM32——外部中断(EXTI)

目录 学习目标 内容 介绍 配置 代码 运行结果 总结 学习目标 本节内容我们将介绍一下外部中断,STM32F4 的每个 IO 都可以作为外部 中断的中断输入口,这点也是 STM32F4 的强大之处。本节我们将使用 STM32F4 的 IO 口作为外部中断输入,…

STM32-外部中断详解

目录 前言 一、外部中断是什么? 二、STM32外部中断概述 1. STM32的外部中断线 2. 外部中断线与IO引脚对应关系 3. 中断向量与服务函数 三、STM32外部中断库函数配置 1. 常用库函数 2. 库函数配置一般步骤 总结 前言 本章讲解STM32的另一个重要的外…

矩阵卷积运算的具体过程,很简单

最近在看图像处理,卷积运算这一块也查了很多,但是感觉都写的太复杂,我这里简单的写一下卷积到底是一个什么计算过程。 假设有一个卷积核h,就一般为3*3的矩阵: 有一个待处理矩阵x: h*x的计算过程分为三步 …

一、什么是卷积

CNN基础: 实现对图像的高准确率识别离不开一种叫做卷积神经网络的技术。卷积神经网络主要是应用于计算机视觉任务当中,但是他处理的任务不只是局限于图像,其实语音识别是也可以使用卷机神经网络。 什么是卷积? 我们人眼看到的和…

线性卷积

持续更新&#xff0c;敬请指正 1. 线性卷积的基本理论 线性卷积是对线性移不变&#xff08;LSI&#xff09;系统的输入输出关系的描述&#xff0c;体现系统的特性。 线性卷积的表达式为 一般情况&#xff0c;现实的系统为因果系统&#xff0c;有k<0时&#xff0c;恒有h(k)…

图像的卷积

图像的卷积 写在前面什么是卷积图像的卷积操作怎么将卷积运用到我们的图像处理上呢&#xff1f;图像卷积没什么用的卷积核 通过卷积实现高斯滤波什么是高斯滤波怎么使用卷积实现高斯滤波 写在结尾 写在前面 前段时间因为一个二维码识别项目&#xff0c;接触到了很多图像处理的…

对卷积的定义和意义的通俗解释

对卷积的困惑 卷积这个概念&#xff0c;很早以前就学过&#xff0c;但是一直没有搞懂。教科书上通常会给出定义&#xff0c;给出很多性质&#xff0c;也会用实例和图形进行解释&#xff0c;但究竟为什么要这么设计&#xff0c;这么计算&#xff0c;背后的意义是什么&#xff0…