Detailed analysis and summary of STM32GPIO external interrupt

1

STM32 interrupt grouping

Each GPIO of the STM32 can be configured as an external interrupt trigger source, which is also the power of the STM32. STM32 divides many interrupt trigger sources into different groups according to the serial number of the pins. For example: PA0, PB0, PC0, PD0, PE0, PF0, PG0 are the first group, and so on, we can get a total of 16 Group, STM32 stipulates that there can only be one interrupt trigger source working in each group at the same time, then the 16 most external interrupts are working. The STM32F103's interrupt controller supports 19 external interrupt/event requests. Each interrupt has a status bit, and each interrupt/event has separate trigger and mask settings. The 19 external interrupts of the STM32F103 are:

Line 0~15: Input interrupt corresponding to external IO port.

Line 16: Connect to the PVD output.

Line 17: Connect to the RTC alarm event.

Line 18: Connect to a USB wakeup event.
Detailed analysis and summary of STM32GPIO external interrupt

2

External interrupt configuration process

1. Configure the trigger source GPIO port

Because the GPIO port is used as the trigger source, the GPIO port is configured as an input mode. The trigger modes are as follows:

a.GPIO_Mode_AIN, analog input (ADC analog input, or low power consumption)

b.GPIO_Mode_IN_FLOATING, floating input

c.GPIO_Mode_IPD with pull-down input

d.GPIO_Mode_IPU with pull-up input

GPIO_InitTypeDef GPIO_InitStructure; / / define the structure

RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOE, ENABLE); / / enable the clock

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//Select IO port PE2

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; / / set to pull up input

GPIO_Init(GPIOE, &GPIO_InitStructure);//Use the structure information to initialize the IO port

2. Enable AFIO multiplexed clock function

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

3. Map the GPIO port to the interrupt line

GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2);

4. Interrupt initialization on the interrupt line

EXTI_InitTypeDef EXTI_InitStructure; / / define the initialization structure

EXTI_InitStructure.EXTI_Line=EXTI_Line2; //The number of the interrupt line is in the range of EXTI_Line0~EXTI_Line15

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; / / interrupt mode, optional values ​​are the interrupt EXTI_Mode_Interrupt and the event EXTI_Mode_Event.

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; / / trigger mode, can be the falling edge trigger EXTI_Trigger_Falling, rising edge trigger EXTI_Trigger_Rising, or any level (rising edge and falling edge) trigger EXTI_Trigger_Rising_Falling

EXTI_InitStructure.EXTI_LineCmd = ENABLE;

EXTI_Init(&EXTI_InitStructure);//Initialize based on structure information

5. Interrupt priority configuration

NVIC_InitTypeDef NVIC_InitStructure; / / define the structure

NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //Enable the channel where the external interrupt is located

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //Preemption priority 2,

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //Subpriority 2

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable external interrupt channel

NVIC_Init(&NVIC_InitStructure); //Priority initialization based on structure information

6. Preparation of external interrupt service function

The external interrupt functions are:

EXPORT EXTI0_IRQHandler

EXPORT EXTI1_IRQHandler

EXPOR T EXTI2_IRQHandler

EXPORT EXTI3_IRQHandler

EXPORT EXTI4_IRQHandler

EXPORT EXTI9_5_IRQHandler

EXPORT EXTI15_10_IRQHandler

Interrupt line 0-4 Each interrupt line corresponds to an interrupt function, interrupt line 5-9 shares the interrupt function, EXTI9_5_IRQHandler, interrupt line 10-15 shared interrupt function EXTI15_10_IRQHandler.

Void EXTI2_IRQHandler(void)

{

If(EXTI_GetITStatus(EXTI_Line2)!=RESET)//Check if an interruption on a line occurs

{

Interrupt logic...

EXTI_ClearITPendingBit(EXTI_Line2); //Clear the interrupt flag on LINE

}

}

When using the button for external interrupt, it is generally necessary to perform button delay debounce and release detection related processing. The interrupt function can refer to the following code:

Void EXTI2_IRQHandler(void)

{

Delay_ms(10);//delayed debounce

If(KEY2==0) //The button is really pressed

{

LED0=!LED0;

}

While(KEY2!=0);//waiting to let go

EXTI_ClearITPendingBit(EXTI_Line2); //clear interrupt flag bit

}

Of course, if your button is allowed to press and hold, then perform other logic operations, no research here.


12V LiFePo4 Battery

This battery is for replacing lead-acid battery, it has the standard appearance and size as well as capacity, but longer cycle life and high energy and good charge and discharge performance.

Capacity:100AH/150AH/180AH/200AH/250AH.

Voltage:12.8V, cycle life is more than 2000 times, also can customize the capacity.

Lifepo4 Battery,Lifepo4 Battery 12V,Solar Battery Pack,180Ah Lithium Mobile Battery,2304Wh Lithium Battery Power Bank,Lead Acid Replacement Battery

Shenzhen Enershare Technology Co.,Ltd , https://www.enersharepower.com

This entry was posted in on