Example of STM32 external interrupt implemented based on STM32CubeIDE software

Infineon / Mitsubishi / Fuji / Semikron / Eupec / IXYS

Example of STM32 external interrupt implemented based on STM32CubeIDE software

Posted Date: 2024-01-14

This article uses STM32CubeIDE software to implement STM32 external interrupt examples.

New Construction

"File"-"New"-"STM32 Project".

Enter the chip model STM32F103ZE.

Select the appropriate package and proceed to the next step.

Fill in the project name, select the project location, and click next.

Configure clock, debug mode

Select a high-speed external clock.

Enter 72 at the HCLK bus clock and press Enter to configure the highest clock frequency.

Select serial for debugging mode to facilitate program downloading.

Configure pin peripherals

In this example, LED1 and KEY_LEFT are used. Find the corresponding circuit diagram.

It can be seen that LED1 corresponds to the PC0 pin and lights up at low level. Button K1 corresponds to the PE2 pin, which is active at low level.

PC0 is configured as GPIO_Output, GPIO output mode

PE2 is configured as GPIO_EXTI2, GPIO interrupt line 2.

Configure the GPIO and you can see the PC0 and PE2 pins.

PC0 pin configuration:

GPIO output level, "High", high level after reset, LED1 is off.

GPIO mode, "Output Push Pull", push-pull output, related articles: sort out push-pull, open drain, OC, OD.

GPIO pull-up and pull-down, "Pull-up", pull-up, default high level to avoid unstable pin level status

For the configuration of the PE2 pin, there are 6 options in GPIO mode, namely:

Rising edge triggered external interrupt

Falling edge triggered external interrupt

Rising/falling edge triggered external interrupt

Rising edge triggered event interrupt

Falling edge triggered event interrupt

Rising/falling edge triggered event interrupt

The difference between "external interrupt" and "event interrupt":

"Event interrupt" can automatically complete the results of this event by hardware, such as causing DMA operations, AD conversion, etc., and the response speed is faster.

In this example, an interrupt service function is required to complete the interrupt. The result is an "external interrupt"

It can be seen from the circuit diagram that when the KEY1 button is pressed, it is low level, and a falling edge will be generated during the process, so select the "falling edge external interrupt" option.

The low level of the button is active. Select pull-up to give the pin a stable level state. When it is not pressed, it is the default high level.

Configure the NVIC nested vectored interrupt controller

NVIC is used to control interrupt response. The following figure lists each interrupt. Its enabling status, preemption priority and response priority can be configured separately.

Because PE2 is configured for the GPIO_EXTI2 interrupt, it appears in the list. SysTick also appears at the same time, which is the time base source of HAL_Delay. As mentioned in the note below, an interrupt is generated every fixed time.

Because the HAL_Delay delay function is used in the interrupt service function code below, the preemption priorities of SysTick and EXTI2 are set to 0 and 2 respectively. Here, the larger the value, the smaller the priority.

Configure interrupt service function

After making the above configuration, click Generate Code.

The interrupt type in this example is "external interrupt", which is implemented through the interrupt service function.

Open the stm32f1xx_it.c file and find the interrupt service function, which calls the GPIO interrupt service function.

F3 finds the definition of this function, and after clearing the interrupt flag, enters the Callback callback function.

This Callback function is also a weakening function and can be rewritten, as shown in the __weak logo in the figure below.

Go back to the stm32f1xx_it.c file and write the result to be achieved when the interrupt occurs at the end of the user code.

Review Editor: Huang Fei

#STM32 #external #interrupt #implemented #based #STM32CubeIDE #software