当前位置:网站首页>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
- 2021 SASE integration strategic roadmap (I)
- If the college entrance examination goes well, I'm already graying out at the construction site at the moment
- Advanced learning of MySQL -- basics -- multi table query -- inner join
- 【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
- 509 certificat basé sur Go
- 学习光线跟踪一样的自3D表征Ego3RT
- St table
- How to set encoding in idea
- MySQL learning notes (mind map)
猜你喜欢

48 page digital government smart government all in one solution

stm32F407-------SPI通信

Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database

如何判断一个数组中的元素包含一个对象的所有属性值

Basic information of mujoco

Linear algebra of deep learning

JWT signature does not match locally computed signature. JWT validity cannot be asserted and should

alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
![[software reverse automation] complete collection of reverse tools](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[software reverse automation] complete collection of reverse tools

Jenkins' user credentials plug-in installation
随机推荐
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
Advanced learning of MySQL -- Fundamentals -- four characteristics of transactions
Configuring OSPF basic functions for Huawei devices
Advantages and disadvantages of code cloning
Leecode brush question record sword finger offer 56 - ii Number of occurrences of numbers in the array II
Service asynchronous communication
String comparison in batch file - string comparison in batch file
How to judge whether an element in an array contains all attribute values of an object
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
基於GO語言實現的X.509證書
Matlab learning notes
C9高校,博士生一作发Nature!
The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
Interface (interface related meaning, different abstract classes, interface callback)
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
mongodb客户端操作(MongoRepository)
dynamic programming
【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
stm32F407-------SPI通信