STM32L151 firmware library programming: external interrupts

1 Principle

1.1 EXTI

EXTI(External interrupt/event controller), the peripheral used for external interrupts, interrupts the controller and generates interrupts.

Functional block diagram of EXTI

Input Line: GPIO corresponds to 20 (EXTI_Line1. EXTI_Line19)
Rising/Descending Rim Trigger Selection Register: Configuring Trigger Conditions
Software interrupt register: whether interrupts occur
Interrupt Shielding Register: Is the Interrupt Request Corresponding

1.2 NVIC

NVIC - ----------------------- Configure interrupt priority, send interrupt signal to the kernel, and the kernel reacts according to interrupt service program.

2 code

An example of external interruption is given, and key interruption is realized to realize lamp switch.
Some macro definitions of keys:

#define KEY_GPIO_PIN 			GPIO_Pin_1
#define KEY_GPIO_PORT 		        GPIOA

#define KEY_ON 		1
#define KEY_OFF 	0

Step 1: Configure GPIO that generates interruptions
(1) Define GPIO initialization structure

GPIO_InitTypeDef GPIO_InitStruct;

(2) Initialization of GPIO

//************************ 1 - Initialization of GPIO******************************************
	RCC_AHBPeriphClockCmd(KEY_INT_CLK, ENABLE);		//Clock opening
	GPIO_InitStruct.GPIO_Pin = KEY_INT_PIN;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;           //Floating space
	GPIO_Init(KEY_INT_PORT, &GPIO_InitStruct);		//Initialization

Step 2: Initialize EXTI
(1) Define EXTI initialization structure

EXTI_InitTypeDef EXTI_InitStruct;

(2) Initialization of EXTI

	//********************** 2 - Initialization EXTI******************************************
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 		//Clock opening
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource1);    //Select the input line
	EXTI_InitStruct.EXTI_Line = EXTI_Line0; 							//Select EXTI Line
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;						//Choice mode
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;		//Select Rising Edge Trigger
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;				//EXTI_Line Enabling Selection
	EXTI_Init(&EXTI_InitStruct)			           	//Initialization structure

Step 3: Configure interrupt priority
(1) Define NVIC initialization structure

NVIC_InitTypeDef NVIC_InitStruct;

(2) Configuring interrupt priority

	//************************ 3 - Configuration Interruption Priority ****************************************
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);		        //Configure Group Priority
	NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;			//Select interrupt source
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;		//Preemption Priority
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;			//Sub priority
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;			//General interruption
	NVIC_Init(&NVIC_InitStruct);					//Initialization structure

Step 4: Write interrupt service functions

(1) Declare the interrupt service function in the startup file
(I use the EXTI0 line, and the interrupt service function has been written by default in the startup file and is weakly defined.)

(2) Implement the interrupt service function in stm32l1xx_it.c (according to your model) and declare it in the header file

void EXTI0_IRQHandler(void)
	if(EXTI_GetITStatus(EXTI_Line0) != RESET)


Added by OmarHaydoor on Sat, 05 Oct 2019 15:16:27 +0300