当前位置:网站首页>External Interrupts and Timers
External Interrupts and Timers
2022-08-06 06:11:00 【have a chili sauce】
Interrupt-related special registers:
(1) Interrupt Enable Control Register (IE)-------controls the opening and masking of each interrupt
(2) Timer/Counter Control Register (TCON)------- Timer and External Interrupt Control
(3) Serial port control register (SCON) ------- serial interrupt control
(4) Interrupt priority control register (IP)------- set the priority of each interrupt
Interrupt configuration steps:
- Clock configuration (GPIO clock, AFIO clock)
- GPIO configuration (interrupt function, mapping)
- EXTI configuration (trigger mode, interrupt enable,)
- NVIC configuration (priority grouping, interrupt enable)
- Interrupt service routine (judgment flag, clear flag)
Interrupt configuration step implementation:
1. Initialize the IO port as input;
Set the state of the IO port you want to use as an external interrupt input. It can be set as a pull-up/pull-down input, or it can be set as a floating input, but when it is floating, an external pull-up or pull-down resistor must be provided.Otherwise, the interrupt may be triggered continuously.In places with large interference, even if pull-up/pull-down is used, it is recommended to use external pull-up/pull-down resistors, which can prevent the influence of external interference to a certain extent.
GPIO_Init();
2. Open the IO port multiplexing clock
The corresponding relationship between the IO port and the interrupt line of STM32 needs to be configured with the external interrupt configuration register EXTICR, so we need to turn on the multiplexing clock first, and then configure the corresponding relationship between the IO port and the interrupt line.To connect the external interrupt with the interrupt line.
In this step, we need to configure the conditions for interrupt generation. STM32 can be configured as rising edge trigger, falling edge trigger, or any level change trigger, but it cannot be configured as high level trigger and low level trigger.Here you can configure it according to your actual situation.At the same time, it is necessary to enable the interrupt on the interrupt line. It should be noted here that if an external interrupt is used and the EMR bit of the interrupt is set, it will cause the software emulation to fail to jump to the interrupt, but the hardware can.Without setting the EMR, the software emulation can enter the interrupt service function, and it is also possible on the hardware.It is recommended not to configure the EMR bit
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
3. Set the mapping relationship between the IO port and the interrupt line
void GPIO_EXTILineConfig()
4. Initialize the online interrupt and set the trigger condition
EXTI_Init()
5. Configure interrupt grouping and enable interrupts
is the grouping and enabling of configuration interrupts. For STM32 interrupts, only the NVIC settings are configured and enabled to be executed, otherwise they will not be executed into the interrupt service function.For a detailed introduction to NVIC, please refer to the previous chapters.
NVIC_Init()
6, write interrupt service function
The last step of the interrupt setting, the interrupt service function, is essential. If the interrupt is enabled in the code, but the interrupt service function is not written, it may cause a hardware error and cause the program to crash!So after opening an interrupt, you must remember to write a service function for the interrupt.
EXTIx_IRQHander()
7, clear the interrupt standard bit
EXTI_ClearITPendingBit()
Break code
//exti.c#include "exti.h"void KEY_Init(void){RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOA,ENABLE); //Enable multiplexing function clockGPIO_InitTypeDef gpiost;gpiost.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_3;gpiost.GPIO_Speed = GPIO_Speed_50MHz;gpiost.GPIO_Mode = GPIO_Mode_IPU;GPIO_Init(GPIOE,&gpiost);gpiost.GPIO_Pin = GPIO_Pin_0;gpiost.GPIO_Mode = GPIO_Mode_IPD;GPIO_Init(GPIOA,&gpiost);}void exti_init(void){EXTI_InitTypeDef extist;NVIC_InitTypeDef nvicst;KEY_Init();RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //Enable multiplexing function clockGPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);extist.EXTI_Line = EXTI_Line3;extist.EXTI_Mode = EXTI_Mode_Interrupt;extist.EXTI_Trigger = EXTI_Trigger_Falling;extist.EXTI_LineCmd = ENABLE;EXTI_Init(&extist);GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);extist.EXTI_Line = EXTI_Line4;extist.EXTI_Mode = EXTI_Mode_Interrupt;extist.EXTI_Trigger = EXTI_Trigger_Falling;extist.EXTI_LineCmd = ENABLE;EXTI_Init(&extist);GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);extist.EXTI_Line = EXTI_Line0;extist.EXTI_Mode = EXTI_Mode_Interrupt;extist.EXTI_Trigger = EXTI_Trigger_Rising;extist.EXTI_LineCmd = ENABLE;EXTI_Init(&extist);nvicst.NVIC_IRQChannel = EXTI3_IRQn;nvicst.NVIC_IRQChannelPreemptionPriority = 0x02;nvicst.NVIC_IRQChannelSubPriority = 0x02;nvicst.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&nvicst);nvicst.NVIC_IRQChannel = EXTI4_IRQn;nvicst.NVIC_IRQChannelPreemptionPriority = 0x02;nvicst.NVIC_IRQChannelSubPriority = 0x01;nvicst.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&nvicst);nvicst.NVIC_IRQChannel = EXTI0_IRQn;nvicst.NVIC_IRQChannelPreemptionPriority = 0x02;nvicst.NVIC_IRQChannelSubPriority = 0x03;nvicst.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&nvicst);}void EXTI0_IRQHandler(void){led1 = !led1;led2 = !led2;EXTI_ClearITPendingBit(EXTI_Line0);}void EXTI3_IRQHandler(void){led1 = !led1;EXTI_ClearITPendingBit(EXTI_Line3);}void EXTI4_IRQHandler(void){led2 = !led2;EXTI_ClearITPendingBit(EXTI_Line4);}//main() functionint main(void){Stm32_Clock_Init();GPIOB_And_E_Config();NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//The main priority 0-3 has a total of four levels, and the slave priority level is 0-3.exti_init();led1 = 0;led2 = 0;while(1){//Wait for an external interrupt to arrive}}Key Interrupt
Press the button continuously

Continuous not supported

Two in one

边栏推荐
猜你喜欢
随机推荐
emby服务器绝对路径设置
二叉搜索树BST
树莓派官方系统取消pi用户,没有显示器如何初始化默认用户并进行SSH连接?
what is an API
shell脚本编写(3.修改文件内容)
shell之循环语句
学好iptables防火墙拒绝信息泄露
Docker 快速安装&搭建 Mysql 环境
动态规划之最小路径之和
MySQL optimization query details and index optimization examples
Office2016登录的账户名和microsoft账户名不照应的解决方法
揭示OT安全四大挑战!Fortinet 发布《2022年全球运营技术和网络安全态势报告》
MITRE ATT&CK报告发布 FortiEDR连续两年100%拦截恶意攻击
5.4 系统类型探测:主机系统识别
3.1 shellcode 概述
shell脚本一键部署dhcp服务(简单通俗易懂)图加注释详解
openstack报错记录及解决办法
一群菜鸟们的“员工信息管理系统”(C语言课程设计)
Red hat修改静态路由
shell之iptables 防火墙









