3.3.External interrupt experiment

1. Learning objectives

1.Learn about STM32 external interrupts.

2.Use the button module to control the switch of the onboard LED light in the form of an external terminal.

Introduction to External Interrupts:

Interruption means that when the CPU is executing the program, there are other events that need to occupy the CPU. After receiving the interrupt request, the CPU suspends the execution of the original program and executes the interrupt function. After the execution of the interrupt function is completed, it continues to return to the original program to continue execution.

The STM32F10x external interrupt/event controller (EXTI) contains up to 20 edge detectors for generating event/interrupt requests. Each of EXTI's input lines can be individually configured to select the type (interrupt or event) and corresponding trigger event (rising edge, falling edge, or edge), and can be masked independently.

The description of the peripherals connected to the 20 interrupt/event lines is as follows:

Each GPIO of STM32f103c8t6 can generate interrupts, there are 16 interrupt lines, and there are up to 6 interrupt service functions.

19 interrupt detection edges and 16 interrupt lines correspond to up to 16 interrupts, but the CPU can only process one event at a time, so these interrupts must be prioritized.

Interrupt lines 0-4 each correspond to an interrupt function, interrupt lines 5-9 share the interrupt function EXTI9_5_IRQHandler, and interrupt lines 10-15 share the interrupt function EXTI15_10_IRQHandler. General steps to use IO port external interrupt:

1)Initialize the IO port as input.

2)Turn on the multiplexing clock of the IO port, and set the mapping relationship between the IO port and the interrupt line.

3)Initialize online interrupts, set trigger conditions, etc.

4)Configure interrupt grouping (NVIC) and enable interrupts.

5)Write an interrupt service function.

2. Hardware wiring construction

The button module and Dupont cable used in the external interrupt experiment need to be purchased separately, otherwise the course cannot be completed due to the lack of important accessories.

Module Wiring Diagram

3. Program code analysis

Interrupt initialization:

003

To use an external interrupt, we must first configure it as shown above.

EXTI interrupt function:

After initializing EXTI, the interrupt is already enabled. When any key is pressed, an interrupt will be triggered, and then the program will enter the interrupt service function for execution.

main function code:

The function realized by the main function is very simple. First, group the NVICs. Here we call the NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2) function to divide the NVICs into 2 groups, that is, the preemption priority and the response priority occupy 2 bits. Then initialize the used hardware port clock and IO port, and then call the EXTI initialization function we wrote earlier.

We have mapped the button pins to the interrupt line in the My_EXTI_Init() function, and configured the corresponding trigger mode. When a button is pressed, it will enter the corresponding interrupt service function to execute the corresponding function program, LED control It is done within the interrupt function.

4. Experimental phenomena

After the program download is complete, you can control the LED light to turn on and off by pressing the button, press the button once, the LED light turns on and then turns off.