当前位置: 首页 > news >正文

STM32——中断

总:STM32——学习总纲

一、什么是中断

1.1 作用与意义

1.2 STM32 GPIO 外部中断简图

二、NVIC

2.1 NVIC 基本概念

Nested vectored interrupt controller,嵌套向量中断控制器,属于内核(M3、M4、M7)

用不到很多的优先级,允许厂商裁剪 

内核中断、外部中断,都有其对应的中断服务函数,是中断的入口。被定义在中断向量表中。

 

2.1.1 中断向量表

STM32 是 32位单片机每一次取地址32位,也就是4byte。

main函数优先级最低。

STM32F1参考手册:

2.2 NVIC 相关寄存器 

2.3 NVIC 工作原理

  

2.4 STM32 中断优先级基本概念

 

2.5 STM32 中断优先级分组

IPR寄存器只是用高四位 [7:4]分配优先级。x位表示 2^x 个优先级。

举例:

2.6 STM32 NVIC使用

步骤操作寄存器HAL库
1设置中断分组AIRCR[10:8]HAL_NVIC_SetPriorityGrouping
2设置中断优先级IPRx [7:4]HAL_NVIC_SetPriority
3使能中断ISERxHAL_NVIC_EnableIRQ

除了这三个HAL库函数,还有其他不常用的NVIC函数。

一般在HAL_Init() 函数中设置分组2:NVIC_PRIORITYGROUP_2。

/*** @brief  This function is used to initialize the HAL Library; it must be the first*         instruction to be executed in the main program (before to call any other*         HAL function), it performs the following:*           Configure the Flash prefetch.*           Configures the SysTick to generate an interrupt each 1 millisecond,*           which is clocked by the HSI (at this stage, the clock is not yet*           configured and thus the system is running from the internal HSI at 16 MHz).*           Set NVIC Group Priority to 4.*           Calls the HAL_MspInit() callback function defined in user file*           "stm32f1xx_hal_msp.c" to do the global low level hardware initialization** @note   SysTick is used as time base for the HAL_Delay() function, the application*         need to ensure that the SysTick time base is always set to 1 millisecond*         to have correct HAL operation.* @retval HAL status*/
HAL_StatusTypeDef HAL_Init(void)
{/* Configure Flash prefetch */
#if (PREFETCH_ENABLE != 0)
#if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \defined(STM32F102x6) || defined(STM32F102xB) || \defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \defined(STM32F105xC) || defined(STM32F107xC)/* Prefetch buffer is not available on value line devices */__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
#endif
#endif /* PREFETCH_ENABLE *//* Set Interrupt Group Priority */HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);/* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */HAL_InitTick(TICK_INT_PRIORITY);/* Init the low level hardware */HAL_MspInit();/* Return function status */return HAL_OK;
}
/*** @brief  Sets the priority grouping field (preemption priority and subpriority)*         using the required unlock sequence.* @param  PriorityGroup: The priority grouping bits length. *         This parameter can be one of the following values:*         @arg NVIC_PRIORITYGROUP_0: 0 bits for preemption priority*                                    4 bits for subpriority*         @arg NVIC_PRIORITYGROUP_1: 1 bits for preemption priority*                                    3 bits for subpriority*         @arg NVIC_PRIORITYGROUP_2: 2 bits for preemption priority*                                    2 bits for subpriority*         @arg NVIC_PRIORITYGROUP_3: 3 bits for preemption priority*                                    1 bits for subpriority*         @arg NVIC_PRIORITYGROUP_4: 4 bits for preemption priority*                                    0 bits for subpriority* @note   When the NVIC_PriorityGroup_0 is selected, IRQ preemption is no more possible. *         The pending IRQ priority will be managed only by the subpriority. * @retval None*/
void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
/*** @brief  Sets the priority of an interrupt.* @param  IRQn: External interrupt number.*         This parameter can be an enumerator of IRQn_Type enumeration*         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32f10xx.h))* @param  PreemptPriority: The preemption priority for the IRQn channel.*         This parameter can be a value between 0 and 15*         A lower priority value indicates a higher priority * @param  SubPriority: the subpriority level for the IRQ channel.*         This parameter can be a value between 0 and 15*         A lower priority value indicates a higher priority.          * @retval None*/
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)

IRQn_Type:是中断号枚举类型,对于中断向量表中断位置(STM32f1参考手册)。

PreemptPriority:抢占优先级

SubPriority:响应优先级

/*** @brief  Enables a device specific interrupt in the NVIC interrupt controller.* @note   To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()*         function should be called before. * @param  IRQn External interrupt number.*         This parameter can be an enumerator of IRQn_Type enumeration*         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32f10xxx.h))* @retval None*/
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)

三、EXTI

3.1 EXTI 基本概念

External interrupt/event Controller,外部中断事件控制器

在H7中,称为 Extended interrupt/event Controller,扩展中断事件控制器。

3.2 EXTI 主要特性

3.3 EXTI 工作原理(F1)

3.4 F4/F7/H7 EXTI工作原理见B站视频

四、EXTI 与 IO 的映射关系

五、如何使用中断

六、通用外设驱动模型

七、HAL库中断回调处理机制介绍

八、编程实战

通过外部中断控制一个灯亮灭

http://www.dtcms.com/a/353758.html

相关文章:

  • PLC_博图系列☞基本指令”PT:加载持续时间“
  • 基于Kafka的延迟队列
  • 身份证号校验码算法
  • C++中类继承的意义
  • PMP项目管理知识点-⑮预测型项目概念辨析
  • 【Kafka】项目整合使用案例
  • 瑞芯微开发工具Linux Linux_Upgrade_Tool使用方法(镜像烧录)
  • Python 比较huggingface_hub库的hf_hub_download函数和snapshot_download函数
  • 在 .NET 8.0 中实现 JWT 刷新令牌
  • 密钥管理服务KMS介绍
  • 遗传算法:模拟自然选择的优化智慧
  • 可编辑69页PPT | 某手机品牌主数据治理项目案例
  • 神经网络学习笔记12——高效卷积神经网络架构MobileNet
  • Origin 2024 安装包下载与安装教程
  • 【算法速成课1 | 题解】洛谷P3366 【模板】最小生成树 MST(Prim Kruskal)
  • 深度学习入门:神经网络基础知识
  • YOLO11实战 第006期-基于yolo11-seg的香蕉种植园语义分割实战文档(yolo格式数据免费获取)
  • MDK-5.4.2 集成 Compiler 5 编译器
  • 基于SpringBoot的协同过滤余弦函数的美食推荐系统(爬虫Python)的设计与实现
  • 数据结构:堆(Heap)
  • 生成式AI的引擎室:深入剖析LLM内存管理与调度
  • 【解锁Photonics for AI:系统学习光学神经网络与超表面设计,成就下一代光芯片工程师】
  • python - js的引入方式、注释变量、数据类型、强制转换、自动类型转换、js运算符、分支结构、函数
  • Nginx单端口代理多个前后端服务的完整配置指南
  • 【雅思019】Canceling an appointment
  • 数据结构——算法设计的基本思想(穷举、递归、分治等)
  • 【自用】JavaSE--junit单元测试、反射、注解、动态代理
  • FreeRTOS 常见面试题与核心知识点详解
  • Redis数据持久化——RDB快照和Aof日志追加
  • 8.28 模拟