文章目录
- 前言
- 一、异常和中断
- 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的其他一些使用模块,请各位大神移步本博主其他文章或是关注博主等待后续发布。