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 | 使能中断 | ISERx | HAL_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库中断回调处理机制介绍
八、编程实战
通过外部中断控制一个灯亮灭