当前位置:网站首页>STM32 external interrupt Usage Summary
STM32 external interrupt Usage Summary
2022-06-13 01:57:00 【richardgann】
STM32 Interrupts are divided into external interrupts , Timer interrupt 、 Serial interrupt . Although the name is different , But the principles are the same , In this section, we will mainly introduce external interrupts and the writing of interrupt functions .
Follow the steps below to write functions and configure functions to complete the configuration of external interrupt functions .
First step : To configure GPIO, Set to interrupt mode .
Second parts : Configure interrupt function
1、 Can make IO Port multiplexing function , Select the pin with interrupt .
2、 Set external interrupt structure members .
3、 Set interrupt priority .
4、 Configure interrupt service function .
Example : Interrupt function triggered by key
//GPIO initialization
void Init_LED(void)
{
GPIO_InitTypeDef GPIO_InitStructure; // Define a GPIO Structural variable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOG, ENABLE);
// Enable each port clock ,
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_13; // On board LED Number D2、D5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_Init(GPIOD, &GPIO_InitStructure);
} Interrupt function writing void Init_EXTIX_TI(void)
{
EXTI_InitTypeDef EXTI_InitStructure; // Define a EXTI Structural variable
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); // Can make IO Reuse function , It is important to use the interrupt function !!!
/* Pin selection */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);// configure port C Of 13 The pin is the interrupt source important !! On board label INT2
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource0); // configure port E Of 0 The pin is the interrupt source important !! On board label INT1
/* Set the members of the external interrupt structure */
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; // The interrupt mode is interrupt mode
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // Falling edge trigger
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line13;
EXTI_InitStructure.EXTI_LineCmd = ENABLE; // Enable medium break
EXTI_Init(&EXTI_InitStructure); // Initialize interrupt register according to parameters
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set interrupt priority group 2
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; // Set the interrupt source as PC13
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // The priority of interrupts is 2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // The sub-priority is 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // To interrupt
NVIC_Init(&NVIC_InitStructure); // Initialize interrupt register according to parameters
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // Set the interrupt source as PE0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // The priority of interrupts is 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // The sub-priority is 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // To interrupt
NVIC_Init(&NVIC_InitStructure); // Initialize interrupt register according to parameters
}Finally, configure the interrupt service function ( Entry function 、 Interrupt handling function )
The last step , It's also an important step , Configure interrupt service function , There are also entry functions . To put it bluntly , Is what you want your interrupt to do .
/*************************************************************
Be careful , The interrupt service procedure is simple , But when writing the entry function , Notice how the function name is written , Only as follows 3 There are two ways of naming .
(1)EXTI0_IRQHandler;EXTI Line 0
EXTI1_IRQHandler;EXTI Line 1
EXTI2_IRQHandler;EXTI Line 2
EXTI3_IRQHandler;EXTI Line 3
EXTI4_IRQHandler;EXTI Line 4
(2)
EXTI9_5_IRQHandler;EXTI Line 5-9
(3)
EXTI15_10_IRQHandler;EXTI Line 10-15
As long as it is a break line 5 The latter cannot be named like the first four , They have their own way .
************************************************************/
void EXTI15_10_IRQHandler(void) //pc13 Interrupt service routine , It is also an interrupt subfunction
{
// There is usually a jitter elimination delay before the key is detected , Directly call the following delay function
if(EXTI_GetITStatus(EXTI_Line13)!= RESET) // Determine whether an interruption has occurred , If it occurs, the interrupt flag is set to 1
{
// LED1=0;
// LED2=1;
GPIO_ResetBits(GPIOG, GPIO_Pin_14); //GPIOG Set to low level ,
GPIO_SetBits(GPIOD, GPIO_Pin_13); //GPIOD Set to high level , In fact, this part can also be written in the main function , stay while In that loop .
}
EXTI_ClearITPendingBit(EXTI_Line13); // Use the library function to clear the interrupt flag bit , After entering the interrupt service program . The first is to clear the flag bit ,
} // Otherwise he will not respond to the interruption , Do not enter interrupt function This is the entire external interrupt handler , Just draw the gourd and ladle directly . 边栏推荐
- 回顾ITIL各版本历程,找到企业运维发展的关键点
- Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
- Pyflink implements custom sourcefunction
- Viewing the ambition of Xiaodu technology from intelligent giant screen TV v86
- 10 days based on stm32f401ret6 smart lock project practice day 1 (environment construction and new construction)
- LabVIEW大型项目开发提高质量的工具
- How to learn C language and share super detailed experience (learning note 1 -- basic data types of C language)
- Installing pytorch geometric
- Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
- 什么是立体角
猜你喜欢

Matplotlib drawing Chinese garbled code

Devexpress implementation flow chart

水管工遊戲

LabVIEW large project development tools to improve quality

传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)

Magics 23.0如何激活和使用视图工具页的切片预览功能

Server installation jupyterab and remote login configuration

When AI meets music, iFLYTEK music leads the industry reform with technology

什么是立体角

华为设备配置双反射器优化虚拟专用网骨干层
随机推荐
什么是立体角
Devaxpress Chinese description --tdximageslider (picture rotation control)
Introduction to Google unit testing tools GTEST and gmoke
五、库存查询功能的完善
指针链表的实现
Logging system in chromium
Luzhengyao, who has entered the prefabricated vegetable track, still needs to stop being impatient
Delphi Google API text to speech MP3 file
一、搭建django自动化平台(实现一键执行sql)
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
VI keyboard diagram
Opencv camera calibration (2): fish eye camera calibration
What is the path field—— Competitive advertising
CXGRID keeps the original display position after refreshing the data
30: Kakfa simulates JSON data generation and transmission
Introduction to ROS runtime
Detailed explanation of audience characteristics
How many smart bids does Google have?
Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
华为设备配置双反射器优化虚拟专用网骨干层