当前位置:网站首页>Part IV: STM32 interrupt control programming
Part IV: STM32 interrupt control programming
2022-07-07 00:51:00 【Heavy vehicle water】
1. The concept of interruption
Interruption refers to the period when the computer is working , There is a program that needs to stop running immediately , Instead, deal with unexpected situations . After an interruption ,CPU Will execute the code written in advance to handle interrupts , After processing, return to the interrupted position to continue execution .
Writing an interrupt program requires precautions :
a. Interrupt handler execution events should be as short as possible ( Can't sleep and delay ), Give way to other programs CPU.
b. The shared data accessed in the interrupt program must be protected .
Stm32 Interrupt the process ,Stm32 in , The interrupt source generates an interrupt signal , Submit to vector interrupt controller (NVIC), The interrupt controller will do further processing and then submit it to CPU,CPU Execute interrupt handler interrupt handler
2. Vector interrupt controller (NVIC)
(1)NVIC Introduce
NVIC External interrupt ( The interrupt signal comes from the outside of the chip ), Altogether 23 An external interrupt source , Include 16 individual GPIO Break and 7 Other interrupts .
(2) Interrupt priority ( The smaller the number is. , The higher the priority ) This needs to be Particular attention .
stm32 The interrupt priority of is divided into 2 class : Preemption priority and response priority
preemption : Interrupts with high preemption priority can interrupt interrupts with low preemption priority
Response priority : Those with high response priority cannot interrupt those with low response priority , When two interrupts with the same preemption priority occur at the same time ,CPU Give priority to interrupts with high response priority . Preemption priority and response priority together occupy at most 8 Bits or 4 Bit byte , The number of bits occupied by configuring two priorities is called interrupt priority grouping .
above NVIC Configuration of , Through library functions NVIC_Init() To achieve
Priority grouping is through NVIC_PriorityGroupConfig To configure the , This function can only be called at the beginning of the program , And can only be called once
NVIC Configuration function
void NVIC_Init (NVIC_InitTypeDef* NVIC_InitStruct);
// Pass in NVIC Initialize structure
typedef struct
{
uint8_t NVIC_IRQChannel; /*!< Interrupt channel @ref IRQn_Type */
uint8_t NVIC_IRQChannelPreemptionPriority; /*!< preemption */
uint8_t NVIC_IRQChannelSubPriority; /*!< Response priority */
FunctionalState NVIC_IRQChannelCmd; /*!< Can make / prohibit ENABLE or DISABLE */
} NVIC_InitTypeDef;
Priority grouping configuration function
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
Parameters :
* @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
* 4 bits for subpriority
* @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
* 3 bits for subpriority
* @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
* 2 bits for subpriority
* @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
* 1 bits for subpriority
* @arg NVIC_PriorityGroup_4:4 bits for pre-emption priority
* 0 bits for subpriority
3. External interrupt source EXIT
(1)EXIT Main characteristics ( Common features when writing programs )
Every interrupt / Each event line has its own trigger and mask
Each interrupt line has a dedicated status bit
Support up to 23 Software events / Interrupt request
(2)EXIT Processing block diagram
(3) External interrupt source mapping
stay stm32 in , Altogether 16 individual GPIO interrupt ,stm32 Medium GPIO Pin , Altogether 16(A~I) Group pin , every last GPIO Can be configured as an external interrupt , Different interrupt trigger sources are divided into different groups by different pin numbers , This is also stm32 One of the strengths of .
PA0,PB0,PC0, ... ...PH0,PI0 It is a group of interrupt sources , And so on , Altogether 16 Group interrupt source , Each group can only have one interrupt trigger source working , that , The whole interrupt source does more work, that is 16 External interrupts .STM32F103 Interrupt controller based on ⽀ a 19 External interrupts / Event request . Each interrupt is provided with a status bit , Every interrupt / Events are unique ⽴ Trigger and shielding settings .STM32F103 Of 19 An external interrupt is :
trunk 0~15: Corresponding to the outside IO The input is interrupted .
trunk 16: Connect to PVD Output .
trunk 17: Connect to RTC Alarm clock event .
trunk 18: Connect to USB Wake Events
Be careful :GPIO When set as external interrupt, it needs to be configured as interrupt mode
——————————————————————————————————————————————————————————————————————————————————————
4. Programming to realize GPIO interrupt , The main steps are as follows .
Add interrupt library function file in the project
(1) Configure interrupt priority grouping , In the following function , Preemption priority and response priority account for 4 position
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
Parameters : // Preemption priority 0 position , Response priority accounts for 4 position
* @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority *
4 bits for subpriority
* @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority *
3 bits for subpriority
* @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority *
2 bits for subpriority
* @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority *
1 bits for subpriority
* @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority *
0 bits for subpriority
——————————————————————————————————————————-
(2) External interrupt source initialization
(3)GPIO The clock and SYSCFG Clock initialization
RCC_AHB1... // initialization GPIO The clock , Omit here
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // initialization SYSCFG The clock
——————————————————————————————————————————
(4)GPIO Configured as input mode
GPIO_init(); // Omit here
- Set up GPIO Relationship with external interrupt line
Void SYSCFG_EXITLineConfig(uint8_t EXIT_PortSourceGPIOx,uint8_t EXIT_PinSourcex);
Parameters :EXIT_PortSourceGPIOx Which group of pins
EXIT_PinSourcex Which foot
———————————————————————————————————————————
(5) Initialize external interrupt
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);
Parameters :
typedef struct
{
uint32_t EXTI_Line; /*!< Which external break line @ref EXTI_Lines */
EXTIMode_TypeDef EXTI_Mode; /*!< Mode selection event / interrupt @ref EXTIMode_TypeDef */
EXTITrigger_TypeDef EXTI_Trigger; /*!< Trigger edge selection @ref EXTITrigger_TypeDef */
FunctionalState EXTI_LineCmd; /*!< Can make / prohibit ENABLE or DISABLE */
} EXTI_InitTypeDef;
——————————————————————————————————————————
NVIC Vector interrupt controller initialization
(6) initialization NVIC
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
// Pass in NVIC Initialize structure
typedef struct
{ uint8_t NVIC_IRQChannel; /*!< Interrupt channel @ref IRQn_Type */
uint8_t NVIC_IRQChannelPreemptionPriority; /*!< preemption */
uint8_t NVIC_IRQChannelSubPriority; /*!< Response priority */
FunctionalState NVIC_IRQChannelCmd; /*!< Can make / prohibit ENABLE or DISABLE */
} NVIC_InitTypeDef;
———————————————————————————————————————————
(7) Implement interrupt handling function
The function name is found in the exception vector table of the startup file , such as :EXTI0_IRQHandler
The interrupt handling function has no parameters and no return value
The interrupt flag must be cleared in the interrupt handler
———————————————————————————————————————————
(8) Clears the interrupt flag bit
void EXTI_ClearITPendingBit(uint32_t EXTI_Line);
// Incoming initialized external interrupt
边栏推荐
- 集合(泛型 & List & Set & 自定义排序)
- On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
- JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
- Deep understanding of distributed cache design
- 【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
- X.509 certificate based on go language
- JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
- 深入探索编译插桩技术(四、ASM 探秘)
- 做微服务研发工程师的一年来的总结
- St table
猜你喜欢
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
Five different code similarity detection and the development trend of code similarity detection
英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
Article management system based on SSM framework
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
Mujoco second order simple pendulum modeling and control
建立自己的网站(17)
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
Learn to use code to generate beautiful interface documents!!!
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
随机推荐
Rails 4 asset pipeline vendor asset images are not precompiled
build. How to configure the dependent version number in the gradle file
uniapp中redirectTo和navigateTo的区别
X.509 certificate based on go language
Distributed cache
Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
Advanced learning of MySQL -- Fundamentals -- concurrency of transactions
Data analysis course notes (V) common statistical methods, data and spelling, index and composite index
Advanced learning of MySQL -- basics -- transactions
[C language] dynamic address book
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
腾讯云 WebShell 体验
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
如何判断一个数组中的元素包含一个对象的所有属性值
以机房B级建设标准满足等保2.0三级要求 | 混合云基础设施
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
Slow database query optimization
Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing