当前位置:网站首页>External interrupt experiment based on stm32f103zet6 library function
External interrupt experiment based on stm32f103zet6 library function
2022-06-27 19:18:00 【It's Beichen bupiacra】
be based on STM32F103ZET6 Library function external interrupt experiment
Introduction to external interrupt
Through the onboard 3 A button , Two on the control board LED And the buzzer sounds .
The code is mainly distributed in the firmware library stm32f10x_exti.h and stm32f10x_exti.c In file .
STM32 Each IO Can be used as an external interrupt Interrupt input port of , This is also true. STM32 The power of .STM32F103 The interrupt controller supports 19 External interrupts / Event request . Each interrupt is provided with a status bit , Every interrupt / Events have independent trigger and mask settings .STM32F103 Of 19 An external interrupt is :
Line 0~15: Corresponding to the outside IO The input of the port is interrupted .
Line 16: Connect to PVD Output .
Line 17: Connect to RTC Alarm clock event .
Line 18: Connect to USB Wake Events .
GPIO Mapping diagram with break line :
In library functions , To configure GPIO A function of the mapping relationship with the break line GPIO_EXTILineConfig() To achieve :
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource) ;
This function will GPIO Ports are mapped to interrupt lines , The usage example is :
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
Break line 2 And GPIOE It maps , So obviously GPIOE.2 And EXTI2 The interrupt line is connected .
The initialization of the interrupt on the interrupt line is through the function EXTI_Init() Realized .
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); // according to EXTI_InitStruct Specified in the
// Parameter initialization peripheral EXTI register
Look at the structure EXTI_InitTypeDef The success of Member variables :
typedef struct
{
uint32_t EXTI_Line;
EXTIMode_TypeDef EXTI_Mode;
EXTITrigger_TypeDef EXTI_Trigger;
FunctionalState EXTI_LineCmd;
}EXTI_InitTypeDef;
As can be seen from the definition , Yes 4 Two parameters need to be set .
The first parameter is the label of the break line , The value range is EXTI_Line0~EXTI_Line15. The concept of broken wire has been mentioned above . in other words , This function is configured with An interrupt parameter on an interrupt line .
The second parameter is the interrupt mode , The optional value is interrupt EXTI_Mode_Interrupt And things Pieces of EXTI_Mode_Event.
The third parameter is the trigger mode , It can be triggered by the falling edge EXTI_Trigger_Falling, Rising edge trigger EXTI_Trigger_Rising, Or any level ( Rising edge and falling edge ) Trigger EXTI_Trigger_Rising_Falling.
The last parameter is to enable disconnection 了 .
Set the break line and GPIO The mapping relationship , Then the initialization parameters such as the trigger mode of the interrupt are set . Both However, it is an external interrupt , When it comes to interruptions, of course we have to set NVIC Interrupt priority . This has been explained before , this Here we will follow the example above , Set break line 2 Interrupt priority of .
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; // Enable the external interrupt channel of the key
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; // preemption 2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; // Sub priority 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channels
NVIC_Init(&NVIC_InitStructure); // Interrupt priority group initialization
After configuring interrupt priority , Then we need to write the interrupt service function . The name of the interrupt service function The word is in MDK Defined in advance . There's a little bit of clarification here ,STM32 Of IO The interrupt function outside the port has only 6 individual , Respectively :
EXPORT EXTI0_IRQHandler
EXPORT EXTI1_IRQHandler
EXPORT EXTI2_IRQHandler
EXPORT EXTI3_IRQHandler
EXPORT EXTI4_IRQHandler
EXPORT EXTI9_5_IRQHandler
EXPORT EXTI15_10_IRQHandler
trunk 0-4 Each interrupt line corresponds to an interrupt function , trunk 5-9 Shared interrupt function EXTI9_5_IRQHandler, in Broken wire 10-15 Shared interrupt function EXTI15_10_IRQHandler.
Two functions are often used when writing interrupt service functions , The first function is to determine whether an interrupt on an interrupt line occurs ( Whether the flag is set ) :
ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);
This function is usually used at the beginning of the interrupt service function to determine whether an interrupt has occurred . Another function is to clear a break line Interrupt flag bit of :
void EXTI_ClearITPendingBit(uint32_t EXTI_Line);
This function is usually used before the end of the interrupt service function , Clears the interrupt flag bit .
The commonly used interrupt service function format is :
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3)!=RESET) // Determine whether an interruption on a line occurs
{
Interrupt logic …
EXTI_ClearITPendingBit(EXTI_Line3); // eliminate LINE Interrupt flag bit on
}
}
The firmware library also provides two functions to judge the external interrupt state and clear the external state Function of flag bit EXTI_GetFlagStatus and EXTI_ClearFlag, Their role is similar to that of the previous two functions . It's just EXTI_GetITStatus The function will first determine whether the interrupt is enabled , Enable to judge the interrupt flag bit , and EXTI_GetFlagStatus Directly used to determine the status flag bit .
IO General steps of external interrupt
1) initialization IO The mouth is the input .
2) Turn on AFIO The clock
3) Set up IO Mapping relationship between port and interrupt line .
4) Initialize online interrupt , Set trigger conditions, etc .
5) Configure interrupt grouping (NVIC), And can interrupt .
6) Write interrupt service function .
software design
exti.c
#include "exti.h"
#include "led.h"
#include "key.h"
#include "delay.h"
#include "usart.h"
#include "beep.h"
// External interrupt 0 Service program
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
KEY_Init(); // Key port initialization
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); // Enable multiplexing function clock
//GPIOE.3 Interrupt line and interrupt initialization configuration Falling edge trigger
//KEY1
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
EXTI_InitStructure.EXTI_Line=EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure); // according to EXTI_InitStruct The parameter specified in EXTI register
//GPIOE.4 Interrupt line and interrupt initialization configuration Falling edge trigger
//KEY0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_Init(&EXTI_InitStructure); // according to EXTI_InitStruct The parameter specified in EXTI register
//GPIOA.0 Interrupt line and interrupt initialization configuration Rising edge trigger
//WK_UP
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure); // according to EXTI_InitStruct The parameter specified in EXTI register
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // Enable key WK_UP External interrupt channel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; // preemption 2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; // Sub priority 3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channels
NVIC_Init(&NVIC_InitStructure); // according to NVIC_InitStruct The parameter specified in NVIC register
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; // Enable key KEY1 External interrupt channel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; // preemption 2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; // Sub priority 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channels
NVIC_Init(&NVIC_InitStructure); // according to NVIC_InitStruct The parameter specified in NVIC register
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; // Enable key KEY0 External interrupt channel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; // preemption 2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; // Sub priority 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channels
NVIC_Init(&NVIC_InitStructure); // according to NVIC_InitStruct The parameter specified in NVIC register
}
// External interrupt 0 Service program
void EXTI0_IRQHandler(void)
{
delay_ms(10); // Desquamation
if(WK_UP==1) //WK_UP Key
{
BEEP=!BEEP;
}
EXTI_ClearITPendingBit(EXTI_Line0); // eliminate LINE0 Interrupt flag bit on
}
// External interrupt 3 Service program
void EXTI3_IRQHandler(void)
{
delay_ms(10); // Desquamation
if(KEY1==0) // Key KEY1
{
LED1=!LED1;
}
EXTI_ClearITPendingBit(EXTI_Line3); // eliminate LINE3 Interrupt flag bit on
}
void EXTI4_IRQHandler(void)
{
delay_ms(10); // Desquamation
if(KEY0==0) // Key KEY0
{
LED0=!LED0;
LED1=!LED1;
}
EXTI_ClearITPendingBit(EXTI_Line4); // eliminate LINE4 Interrupt flag bit on
}
1. Because of our WK_UP The key is active at high level , and KEY0 and KEY1 It's low level active , So we set WK_UP Trigger an interrupt for the rising edge , and KEY0 and KEY1 It is set as falling edge trigger .
2. Here we assign all interrupts to the second group , Set the key preemption priority to the same , The sub priorities are different , These three buttons ,KEY0 The highest priority .
3. At most Then there is a sentence EXTI_ClearITPendingBit(EXTI_Line3); Clear the interrupt request that has occurred through this sentence .
exti.h
#ifndef __EXTI_H
#define __EXIT_H
#include "sys.h"
void EXTIX_Init(void); // External interrupt initialization
#endif
main.c
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "exti.h"
#include "beep.h"
int main(void)
{
delay_init(); // Delay function initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set up NVIC Interrupt grouping 2:2 Bit preemption priority ,2 Bit response priority
uart_init(115200); // The serial port is initialized to 115200
LED_Init(); // Initialization and LED Connected hardware interface
BEEP_Init(); // Initialize buzzer IO
EXTIX_Init(); // Initialize external interrupt input
LED0=0; // Light the red light first
while(1)
{
printf("OK\r\n");
delay_ms(1000);
}
}
边栏推荐
- OpenSSL client programming: SSL session failure caused by an obscure function
- 全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
- 教你打印自己的日志 -- 如何自定义 log4j2 各组件
- PostgreSQL数据库WAL——资源管理器RMGR
- Market status and development prospect forecast of global active quality control air sampler industry in 2022
- The difficulty of realizing time series database "far surpassing" general database in specific scenarios
- 如何封装调用一个库
- TIA博途_基于SCL语言制作模拟量输入输出全局库的具体方法
- How to encapsulate and call a library
- 使用logrotate对宝塔的网站日志进行自动切割
猜你喜欢
如何实现IM即时通讯“消息”列表卡顿优化
银河麒麟V10系统激活
What is ICMP? What is the relationship between Ping and ICMP?
9.OpenFeign服务接口调用
Galaxy Kirin V10 system activation
别焦虑了,这才是中国各行业的工资真相
PCB线路板蛇形布线要注意哪些问题?
Vscode suggests that you enable gopls. What exactly is it?
Tdengine connector goes online Google Data Studio store
Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders
随机推荐
Four years of College for an ordinary graduate
惊呆!原来 markdown 的画图功能如此强大!
What is ICMP? What is the relationship between Ping and ICMP?
SQL update批量更新
新中大冲刺科创板:年营收2.84亿 拟募资5.57亿
喜讯丨英方软件2022获得10项发明专利!
PostgreSQL database Wal - resource manager rmgr
Campus book resource sharing platform
MongoDB和MySQL的区别
The first in China! EMQ joined Amazon cloud technology's "startup acceleration - global partner network program"
Camera calibration with OpenCV
Market status and development prospect of resorcinol derivatives for skin products in the world in 2022
教你打印自己的日志 -- 如何自定义 log4j2 各组件
Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
Win10 LTSC 2021 wsappx CPU usage high
工作流自动化 低代码是关键
银河麒麟V10系统激活
Open source summer 2022 | opengauss project selected and announced
MFS分布式文件系统
中国工业软件市场研究报告出炉,力控SCADA、MES丰富国产工业软件生态