Interrupt-STM32

Interrupt-STM32

Interruption: During the running of the main program, a specific interrupt trigger condition (interrupt source) occurs, causing the CPU to suspend the currently running program and turn to process the interrupt program, and then return to the original suspended position to continue running after the processing is completed.
Interrupt priority: When multiple interrupt sources apply for interrupts at the same time, the CPU will make a decision according to the priority of the interrupt sources, and respond to the more urgent interrupt sources first.
Interrupt nesting: When an interrupt program is running, there is a new higher priority interrupt source requesting to interrupt the CPU to suspend the current interrupt program again, and turn to process the new interrupt program, and return in turn after the processing is completed.
68 maskable interrupt channels include EXTI, TIM, ADCUSART, SPI, 12C, RTC and other peripherals.
Use NVIC to manage interrupts uniformly. Each interrupt channel has 16 programmable priority levels, which can be grouped to further set preemption priority and response priority.
insert image description here
The same Pin in GPIO_Pin cannot trigger interrupt at the same time.
insert image description here

configuration steps

insert image description here
The first step is to configure RCC and turn on the clocks of the involved peripherals.
The second step is to configure GPIO and select the port as input mode.
The third step is to configure AFIO, select the GPIO to be used, and connect it to the EXTI behind.
The fourth step is to configure EXTI and select the edge trigger mode, such as rising edge, falling edge or double edge. There is also a way to choose the trigger response, you can choose interrupt response and event response. (Generally choose interrupt response).
The fifth step is to configure NMIC and choose an appropriate priority for our interrupt. Through the NVIC, the external interrupt signal can enter the CPU. Only when the CPU receives the interrupt signal can it jump to the interrupt function to execute the interrupt program.
insert image description here

insert image description here
insert image description here

insert image description here
The string ending with IRQHandler is the name of the interrupt function
insert image description here

#include "stm32f10x.h"                  // Device header

//用一个数字来统计中断触发的次数
uint16_t CountSensor_Count;

void CountSensor_Inint(void)
{
	//第一步:配置时钟
	
	//开启RRC时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//注意函数和参数的这个APB2、APB1和AHB要对应起来
	//开启AFIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

	//第二步:配置GPIO
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//跟参考手册选择上拉模式,复用
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_14;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	
	//第三步:配置AFIO
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);//选择GPIOB_14为中断引脚
	
	//第四步:配置EXTI
	EXTI_InitTypeDef EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line = EXTI_Line14;//选择第14路中断
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;//使能中断
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;//中断触发
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发
	EXTI_Init(&EXTI_InitStruct);
	
	
	//配置NVIC
	//注意:分组方式整个芯片只能用一种,因此,分组的代码只需要执行一次
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStruct);
}

//编写中断函数
//中断函数的名字都是固定的,中断函数都是无参数无返回值的
//写错了就进不了中断了,最好是直接从启动文件复制过来
//中断函数就不用声明,因为中断函数不需要调用,它是自动执行的
void EXTI15_10_IRQHandler(void)
{
	//一般先进行中断标志位的判断	
	if(EXTI_GetITStatus(EXTI_Line14) == SET)
	{
		CountSensor_Count++;//记录进入中断的次数
		//每次中断程序结束后,都应该清除一下中断标志位
		EXTI_ClearITPendingBit(EXTI_Line14);
	}
}

//返回中断次数
uint16_t Get_CountSensor_Count(void)
{
	return CountSensor_Count;
}

#ifndef _COUNTSENSOR_H
#define _COUNTSENSOR_H

void CountSensor_Inint(void);
uint16_t Get_CountSensor_Count(void);
#endif

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "KEY.h"
#include "CountSensor.h"


int main(void)
{

	CountSensor_Inint();
	
	while(1)
	{

	}
}

Note:
In the interrupt function, it is best not to execute code that takes too long.

Do not call the same function or operate the same hardware in the interrupt function and the main function.

Suggestion: Operate variables or flags in the interrupt, and then operate on the variable when the interrupt returns. It can not only ensure the shortness and speed of the interrupt function, but also ensure the hardware operation without conflict.

Guess you like

Origin blog.csdn.net/qq_45159887/article/details/130444362