Control LED based on STM32 external interrupt

Table of contents

foreword

1. Basic concepts related to interruption

1. Overview of the whole process of interruption

 2. The role of interruption

3. Interrupt priority

2. Initial configuration of CubeMX

1. Chip selection

2. Clock configuration 

 3. GPIO configuration

 4. NVIC external interrupt enable

 5. Configuration project file output

3. Write and burn Keil related programs

1. Program code analysis

 2. Burning of the .HEX file generated by the program

4. Experimental results

5. Reference

6. Summary


foreword

Related peripheral initialization: STM32CubeMX

Program compiler: KEIL

Burning: FlyMcu

Hardware: stm32c8t6

1. Basic concepts related to interruption

1. Overview of the whole process of interruption

1. Interruption occurs: when the CPU is processing a certain event A, another event B occurs, and the CPU is requested to process it quickly

2. Interrupt processing: The CPU suspends the current work and turns to process event B
3. Interrupt return: After the CPU finishes processing event B, it returns to the place where event A was suspended to continue processing event A
 The whole process is called interruption
· Schematic diagram of interrupt execution

 

 2. The role of interruption

Speed ​​matching: can solve the contradiction of data transmission between fast CPU and slow external equipment

Time-sharing operation: CPU can serve multiple external devices in time-sharing, improving the utilization rate of the computer

Real-time response: CPU can process random events of the application system in time, enhancing the real-time performance of the system

High reliability: CPU can handle emergencies such as equipment failure and power failure, improving system reliability

3. Interrupt priority

The processor sets different priority levels according to the important programs of different interrupts. The processing principles of different priority interrupts are: high-level interrupts can interrupt low-level interrupts; low-level interrupts cannot interrupt high-level interrupts

2. Initial configuration of CubeMX

1. Chip selection

2. Clock configuration 

First select the high-speed external clock as the input of the system clock, and then the specific configuration of the clock tree is as follows

 3. GPIO configuration

PB0 is set as an external interrupt trigger pin

PA2 is set as an output pin

 4. NVIC external interrupt enable

· Enable external interrupt

External interrupt trigger mode

· Rising edge trigger

· Falling edge trigger

·  Rising/falling edge (both edges) trigger

 · Configure external interrupt

 5. Configuration project file output

3. Write and burn Keil related programs

1. Program code analysis

· External interrupt trigger and output pin initialization configuration

 When PB0 changes from low level to high level (rising edge), it enters the external interrupt 0 service function

 · Click the right mouse button GO to Definition to go to the definition

   After the interrupt service function is executed, enter the callback function, we can write the interrupt service we need to execute in the callback function

Eliminate the jitter of the button trigger level by judging the interrupt trigger pin level again

 · Engineering Code Portal: LED_EXTI0 · Fantasy

 2. Burning of the .HEX file generated by the program

· Hardware connection

· Program burning

 

4. Experimental results

External interrupt button experiment

5. Reference

The stm32 external interrupt mode control light is on and off

6. Summary

        Through the external interrupt button experiment, I have experienced the characteristics of embedded development interrupts. It is different from the previous while loop to execute all programs sequentially. Instead, it will enter the interrupt service function to execute the corresponding code segment when the interrupt is triggered. After execution, Immediately after that, jump back to the main program to continue execution. The data processing efficiency of the embedded chip can be improved through the interrupt method. Seriously learning and mastering the execution method of the embedded chip interrupt can make it more efficient and apply it to our project works. .

Guess you like

Origin blog.csdn.net/qq_52791446/article/details/127440014