[Embedded] STM32-External Interrupt/Event Controller (EXTI) Detailed Explanation. "Example: External Interrupt to Realize LED Turning Off"

1. Introduction to EXTI

EXTI (External interrupt/event controller) — External interrupt/event controller, which manages 20 interrupt/event lines of the controller. Each interrupt/event line corresponds to an edge detector, which can
detect the rising edge and falling edge of the input signal . EXTI can realize the individual configuration of each interrupt/event line, which can be individually configured as an
interrupt or event, as well as the attributes of the trigger event.

2. EXTI functional block diagram

Insert picture description here

1. EXTI can be divided into two major functions, one is to generate interrupts, and the other is to generate events.
2. The EXTI controller has 19 interrupt/event input lines. These input lines can be set to any GPIO or some other events of the peripheral through the register .
3. For the edge detection circuit, set the trigger mode by controlling the corresponding bits of the rising edge trigger selection register ( EXTI_RTSR ) and the falling edge trigger selection register ( EXTI_FTSR ).
4. The interrupt trigger can come from the external input line and the software interrupt event register ( EXTI_SWIER ) respectively. From the structure diagram, they are connected through an OR gate .
5. If an interrupt occurs in the pending register ( EXTI_PR ), the corresponding register is 1.
6. The interrupt mask register ( EXTI_IMR ) can mask the interrupt.
7. Finally, output the contents of the EXTI_PR register to the NVIC , so as to realize the system interrupt event control.
8. Generate an event, the same. The pulse signal is the final product of the circuit that generates the event. This pulse signal can be used by other peripheral circuits, such as timer TIM, analog-to-digital converter ADC, etc. Such pulse signals are generally used to trigger TIM or ADC to start conversion.

Three, programming

Grasp the following points:

1. EXTI has 20 interrupt/event lines, and each GPIO can be set as an input line.
2. EXTI0 to EXTI15 are used for GPIO , and any GPIO can be used as the input source of EXTI through programming control.
3. When configuring EXTI , you need to turn on the multiplexed clock ( RCC_AFIO )
4. EXTI interrupt/event line
Insert picture description here
Insert picture description here

In the figure above, what represents EXTI n is the input, which can be P X n (X = A,B,C,D,E,F,G) (n=1,2…15)

1. Programming points
1) Initialize the GPIO used to generate interrupts;
2) Initialize EXTI;
3) Configure NVIC;
4) Write interrupt service function;

Configure GPIO, similar to buttons, just use floating input mode,

4. Examples:

"Realize button control LED turn on and off"

1) Initialize the GPIO used to generate interrupts;

void KEY_GPIO_Config(void)
{
    
    
	GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_APB2PeriphClockCmd(GPIO_KEY1_CLK|
						   GPIO_KEY2_CLK|
	                       GPIO_KEY3_CLK|
	                       GPIO_KEY4_CLK|
						   RCC_APB2Periph_AFIO,ENABLE);
												//RCC_APB2Periph_AFIO
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	
	GPIO_InitStruct.GPIO_Pin = GPIO_KEY1_PIN;
	GPIO_Init(GPIO_KEY1_PORT,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_KEY2_PIN;
	GPIO_Init(GPIO_KEY2_PORT,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_KEY3_PIN;
	GPIO_Init(GPIO_KEY3_PORT,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_KEY4_PIN;
	GPIO_Init(GPIO_KEY4_PORT,&GPIO_InitStruct);
	
}

2) Initialize EXTI;

void EXTI_Config(void)
{
    
    
	EXTI_InitTypeDef EXTI_InitStruct;
	
	NVIC_Config();
	
	//为什么我额外使能一下RCC_AFIO 程序会出错,  PS现在好像不错了
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

	//配置EXTI结构体
	GPIO_EXTILineConfig( KEY1_INT_EXTI_PortSourse, KEY1_INT_EXTI_PinSourse);//选择EXTI的信号源
	
	EXTI_InitStruct.EXTI_Line = KEY1_INT_EXTI_Line;
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;		//KEY1
	EXTI_Init(&EXTI_InitStruct);
	
	GPIO_EXTILineConfig(KEY2_INT_EXTI_PortSourse, KEY2_INT_EXTI_PinSourse);//选择EXTI的信号源
	EXTI_InitStruct.EXTI_Line = KEY2_INT_EXTI_Line; 		//KEY2
	EXTI_Init(&EXTI_InitStruct);
	
	GPIO_EXTILineConfig(KEY3_INT_EXTI_PortSourse, KEY3_INT_EXTI_PinSourse);//选择EXTI的信号源
	EXTI_InitStruct.EXTI_Line = KEY3_INT_EXTI_Line; 		//KEY3
	EXTI_Init(&EXTI_InitStruct);
	
	GPIO_EXTILineConfig(KEY4_EXTI_PortSourse, KEY4_EXTI_PinSourse);//选择EXTI的信号源
	EXTI_InitStruct.EXTI_Line = KEY4_EXTI_Line; 			//KEY4
	EXTI_Init(&EXTI_InitStruct);
}

3) Configure NVIC;

void NVIC_Config(void)
{
    
    
	NVIC_InitTypeDef NVIC_InitStruct;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//配置NVIC为优先级组1
	
	NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;				//KEY1
	NVIC_Init(&NVIC_InitStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel = EXTI9_5_IRQn;  			//KEY2
	NVIC_Init(&NVIC_InitStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn;  				//KEY3
	NVIC_Init(&NVIC_InitStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel = EXTI2_IRQn;  				//KEY4
	NVIC_Init(&NVIC_InitStruct);
}

4) Write interrupt service function;

void EXTI0_IRQHandler(void)						//KEY1
{
    
    
	if(EXTI_GetITStatus(KEY1_INT_EXTI_Line))
	{
    
    
		LED_1_TOGGLE;
		EXTI_ClearITPendingBit(KEY1_INT_EXTI_Line);
	}

	
}
void EXTI9_5_IRQHandler(void)					//KEY2
{
    
    

	if(EXTI_GetITStatus(KEY2_INT_EXTI_Line))
	{
    
    
		LED_2_TOGGLE;
		EXTI_ClearITPendingBit(KEY2_INT_EXTI_Line);
	}

	
}
void EXTI1_IRQHandler(void)						//KEY3
{
    
    
	
	if(EXTI_GetITStatus(KEY3_INT_EXTI_Line))
	{
    
    
		LED_3_TOGGLE;
		EXTI_ClearITPendingBit(KEY3_INT_EXTI_Line);
	}
	
	
}
void EXTI2_IRQHandler(void)						//KEY4
{
    
    
	
	if(EXTI_GetITStatus(KEY4_EXTI_Line))
	{
    
    
		LED_4_TOGGLE;
		EXTI_ClearITPendingBit(KEY4_EXTI_Line);
	}
	
}

If you have any questions, you can leave a message, welcome to exchange and discuss!

Guess you like

Origin blog.csdn.net/qq_45689790/article/details/113847739