当前位置:网站首页>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
边栏推荐
- 48 page digital government smart government all in one solution
- Learn self 3D representation like ray tracing ego3rt
- 学习光线跟踪一样的自3D表征Ego3RT
- Policy Gradient Methods
- 【vulnhub】presidential1
- 代码克隆的优缺点
- 基於GO語言實現的X.509證書
- 详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
- Set (generic & list & Set & custom sort)
- Mujoco produces analog video
猜你喜欢
Set (generic & list & Set & custom sort)
AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info
The programmer resigned and was sentenced to 10 months for deleting the code. Jingdong came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
【vulnhub】presidential1
基于SSM框架的文章管理系统
St table
Are you ready to automate continuous deployment in ci/cd?
Distributed cache
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
随机推荐
Win10 startup error, press F9 to enter how to repair?
stm32F407-------DAC数模转换
[user defined type] structure, union, enumeration
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
基於GO語言實現的X.509證書
基于GO语言实现的X.509证书
.class文件的字节码结构
Advanced learning of MySQL -- basics -- multi table query -- subquery
Rails 4 asset pipeline vendor asset images are not precompiled
Telerik UI 2022 R2 SP1 Retail-Not Crack
[force buckle]41 Missing first positive number
QT tutorial: creating the first QT program
How to judge whether an element in an array contains all attribute values of an object
Leetcode (547) - number of provinces
How to get started and improve test development?
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
STM32开发资料链接分享
Jenkins' user credentials plug-in installation
在jupyter中实现实时协同是一种什么体验
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should