当前位置:网站首页>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
边栏推荐
- 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
- Notes of training courses selected by Massey school
- Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
- AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
- 什么是时间
- 用tkinter做一个简单图形界面
- Js+svg love diffusion animation JS special effects
- Advanced learning of MySQL -- basics -- multi table query -- subquery
- How to set encoding in idea
- Attention slam: a visual monocular slam that learns from human attention
猜你喜欢
Are you ready to automate continuous deployment in ci/cd?
uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
stm32F407-------DAC数模转换
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
Threejs image deformation enlarge full screen animation JS special effect
Data processing of deep learning
Idea automatically imports and deletes package settings
Mujoco finite state machine and trajectory tracking
Hero League | King | cross the line of fire BGM AI score competition sharing
随机推荐
一行代码实现地址信息解析
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
. Bytecode structure of class file
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
Mujoco produces analog video
Testers, how to prepare test data
Deep learning environment configuration jupyter notebook
代码克隆的优缺点
[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
用tkinter做一个简单图形界面
509 certificat basé sur Go
Leetcode (547) - number of provinces
What is time
Leetcode(547)——省份数量
Stm32f407 ------- DAC digital to analog conversion
Js+svg love diffusion animation JS special effects
Attention SLAM:一种从人类注意中学习的视觉单目SLAM
37頁數字鄉村振興智慧農業整體規劃建設方案
X.509 certificate based on go language
Learning notes 5: ram and ROM