当前位置:网站首页>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
边栏推荐
- [daily problem insight] prefix and -- count the number of fertile pyramids in the farm
- Deep understanding of distributed cache design
- C Primer Plus Chapter 14 (structure and other data forms)
- @TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
- [software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving
- Levels - UE5中的暴雨效果
- Chapter II proxy and cookies of urllib Library
- Configuring the stub area of OSPF for Huawei devices
- Distributed cache
- 三维扫描体数据的VTK体绘制程序设计
猜你喜欢
Mujoco second order simple pendulum modeling and control
Hero League | King | cross the line of fire BGM AI score competition sharing
Threejs image deformation enlarge full screen animation JS special effect
[Niuke classic question 01] bit operation
Chapter II proxy and cookies of urllib Library
48 page digital government smart government all in one solution
Dr selection of OSPF configuration for Huawei devices
基於GO語言實現的X.509證書
三维扫描体数据的VTK体绘制程序设计
JS+SVG爱心扩散动画js特效
随机推荐
Set (generic & list & Set & custom sort)
Chapter II proxy and cookies of urllib Library
Common shortcuts to idea
stm32F407-------SPI通信
Trace tool for MySQL further implementation plan
. Bytecode structure of class file
[C language] dynamic address book
Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
.class文件的字节码结构
随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
Attention slam: a visual monocular slam that learns from human attention
Stm32f407 ------- DAC digital to analog conversion
Command line kills window process
Leetcode(547)——省份数量
Zynq transplant ucosiii
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
Value Function Approximation
dynamic programming
Js+svg love diffusion animation JS special effects