Embedded STM32—On the third day, the key interruption key realizes the control of led lights

1. Software and engineering environment construction

  • I won’t introduce it, you can directly use the existing
    link: https://pan.baidu.com/s/1GaSYpNm6yh-lZZmqRdXxvQ
    extraction code: dy63
  • Related library file
    link: https://pan.baidu.com/s/19PLYdtiuSoZZGdVD7_tumQ
    extraction code: mrw8

2. Press the key to interrupt the control of the led light

  • Learn about the interrupt handling process

Interrupt handling process: exception (interrupt) interrupt source -> allow interrupt -> apply for interrupt -> CPU response -> protect scene (stack) -> jump processing -> return.
Insert picture description here
Insert picture description here

  • Significance of
    interrupts Interrupts occupy a very important position in program events. If there is no interruption, the efficiency of the CPU will be greatly reduced. Just like in the UART module, when receiving the data sent by the computer, the while(){} method of querying the status is used. If the computer does not send data, the program will keep blocking, making the CPU unable to do other things. If there is a mechanism, instead of the CPU cyclically inquiring whether there is data coming, the hardware automatically receives the data, and automatically informs the CPU when the data is received, and the CPU then reads the data out. In this way, before the data is received, the CPU can do other things, and the work efficiency is naturally improved. In the CPU hardware, this function is realized through the interrupt mechanism. Each on-chip peripheral hardware provides an interrupt signal. When the module processes a specific working state, an interrupt signal will occur to communicate with the CPU.

  • Add some interrupted library functions
    Insert picture description here

  • Take a look at the schematic diagram of the development board. The method of
    external interrupt trigger (interrupt) event: 1. Rising edge trigger 2. Falling edge trigger 3. Edge trigger The
    Insert picture description here
    corresponding button corresponding to the previous day is KEY1->PA0,KEY2->PD2,KEY3-> PC12, click KEY1 to be high level, KEY2 and KEY3 are respectively low level.
    High level triggers on rising edge, low level triggers on falling edge

  • Select the external interrupt line EXTI
    Insert picture description here
    STM32F4xx Chinese Reference Manual.pdf to get that PA0 of key1 corresponds to EXTI0, PD2 of key2 corresponds to EXTI2, and PC12 of key3 corresponds to EXTI12

  • System Configuration Controller (SYSCFG) The
    system configuration controller is mainly used to manage the address remapping of the executable code storage area, select the Ethernet PHY interface
    , and manage the external interrupt line connection of GPIO.
    Unless otherwise specified, this section applies to the entire STM32F4xx series.

  • Initialize the LED first, so I won’t write any more. If you have one on the first day, you can check it out if you don’t, then Exit_Init is initialized

/*******************************************************
*函数名:Exit_Init
*功  能:中断初始化
*参  数:无
*返  回:无
*备  注:注意开启PA0,PD2的PC12的映射SYSCFG时钟 ,使能外部中断
********************************************************/
void Exit_Init(void)
{
    
    		
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);//开启SYSCFG时钟,简称重映射 
		EXTI_InitTypeDef  Exit_Key1,Exit_Key2,Exit_Key3;//定义外部中断结构体变量	
//		//key1
		SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0); //映射到PA0引脚到EXTI
		Exit_Key1.EXTI_Line = EXTI_Line0; //选择EXTI0
		Exit_Key1.EXTI_Mode = EXTI_Mode_Interrupt; // 选择模式(中断)
		Exit_Key1.EXTI_Trigger = EXTI_Trigger_Rising;//选择上升沿触
		Exit_Key1.EXTI_LineCmd = ENABLE;//启用EXTI0
		EXTI_Init(&Exit_Key1);//写入EXTI初始化
	  	NVIC_EnableIRQ(EXTI0_IRQn);//使能外部中断0线进行工作
		//key2
		SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD,EXTI_PinSource2); //映射到PD2引脚到EXTI2	
		Exit_Key2.EXTI_Line = EXTI_Line2; //选择EXTI2
		Exit_Key1.EXTI_Mode = EXTI_Mode_Interrupt; // 选择模式(中断)
		Exit_Key2.EXTI_Trigger = EXTI_Trigger_Falling;//选择下升沿触
		Exit_Key2.EXTI_LineCmd = ENABLE;启用EXTI2
		EXTI_Init(&Exit_Key2);//写入EXTI初始化
	  	NVIC_EnableIRQ(EXTI2_IRQn);//使能外部中断2线进行工作
		//key3
		SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC,EXTI_PinSource12); //映射到PC12引脚到EXTI3	
		Exit_Key3.EXTI_Line = EXTI_Line12; //选择EXTI12
		Exit_Key3.EXTI_Mode = EXTI_Mode_Interrupt; // 选择模式(中断)
		Exit_Key3.EXTI_Trigger = EXTI_Trigger_Falling;//选择下升沿触
		Exit_Key3.EXTI_LineCmd = ENABLE;//启用EXTI3
		EXTI_Init(&Exit_Key3);//写入EXTI初始化
	  	NVIC_EnableIRQ(EXTI15_10_IRQn);//由于没有EXTI12,所以使能外部中断15_10线进行工作		
}
  • (The main one is coming, he is coming) Press the key to interrupt and click to control the led light to turn on and off
/*******************************************************
*函数名:EXTI0_IRQHandler
*功  能:按键一中断
*参  数:无
*返  回:无
*备  注:注意挂起寄存器(清0)
********************************************************/
int EXTI1_flag;

void EXTI0_IRQHandler(void)//由中断自动触发
{
    
    
		 
		EXTI_ClearFlag(EXTI_Line0); //挂起寄存器PA0
		EXTI1_flag++;
		if(EXTI1_flag%2)
		GPIO_ResetBits(GPIOC,GPIO_Pin_13);
		else 
		GPIO_SetBits(GPIOC,GPIO_Pin_13);
}
/*******************************************************
*函数名:EXTI0_IRQHandler
*功  能:按键二中断
*参  数:无
*返  回:无
*备  注:注意挂起寄存器(清0)
********************************************************/
int EXTI2_flag;
void EXTI2_IRQHandler(void)//由中断自动触发
{
    
    
		EXTI_ClearFlag(EXTI_Line2); //挂起寄存器PD2
		EXTI2_flag++;
		if(EXTI2_flag%2)
		GPIO_ResetBits(GPIOC,GPIO_Pin_10);
		else 
		GPIO_SetBits(GPIOC,GPIO_Pin_10);
		
}

/*******************************************************
*函数名:EXTI0_IRQHandler
*功  能:按键三中断
*参  数:无
*返  回:无
*备  注:注意挂起寄存器(清0)
********************************************************/
int EXTI3_flag;

void EXTI15_10_IRQHandler(void)
{
    
    
			EXTI3_flag++;
			if(EXTI3_flag%2)
			GPIO_ResetBits(GPIOC,GPIO_Pin_11);
			else 
			GPIO_SetBits(GPIOC,GPIO_Pin_11);
			EXTI_ClearFlag(EXTI_Line12);  //挂起寄存器PC2
}
  • The main function calls related functions
LEDInit();//LED初始化
Exit_Init();//中断初始化
while(1)
	{
    
    
			//是不是很激动,这里什么都不用写,哈哈
	}

At this point, the key interruption is perfectly realized. Click to control the LED light to turn on and off. Finally, thank you all for reading. If you need other related information, please contact me.

Guess you like

Origin blog.csdn.net/weixin_45247466/article/details/114414069