The article is reproduced from http://blog.csdn.net/charmingsun/article/details/52095563. For the use of software and other preparations, please refer to: Getting Started Tutorial Using HAL Library, STM32CubeMX and Keil 5 Development (I): Lighting up an LED (NUCLEO-F411RE)
the STM32F4xx HAL driver manual .2. GPIO usage examples
Example function: Use the button (PC13) to flip the LED (PA5) level state.
1. Use STM32CubeMX to configure the pin function and nested vector interrupt controller:
Configure the LED light pin to output mode
Connect the button pin to the external interrupt line 13
Click the GPIO configuration button
Select the GPIO mode as the rising edge triggered external interrupt mode
(what is rising edge triggered, what is external interrupt, and other modes, please Baidu yourself)
Click the Nested Vectored Interrupt Controller Configuration button
Check to enable external interrupt lines 10~15 and generate the corresponding initialization code
2. Define the external interrupt callback function at the corresponding position in the generated project:
/* USER CODE BEGIN 4 */ void HAL_GPIO_EXTI_Callback (uint16_t GPIO_Pin) { if(GPIO_Pin == GPIO_PIN_13) HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); } /* USER CODE END 4 */
1
2
3
4
5
6
7
8
3. Compile and download the program to the development board, and reset the development board.
3. Code Analysis
1. Register the EXTI15_10_IRQHandler function as the interrupt service function of external interrupt lines 10 to 15 in the startup file startup_stm32f411xe.s of the development board. When external interrupt lines 10 to 15 generate an external interrupt, the hardware calls the EXTI15_10_IRQHandler function to interrupt the currently running program. The CPU starts to execute the program in the interrupt service function, and after execution, continues to run the program before the interruption.
2. Because the hardware structure of STM32F4 determines that external interrupt lines 10 to 15 share an interrupt vector, only one interrupt service function can be registered. The HAL framework uses the HAL_GPIO_EXTI_IRQHandler() function and the HAL_GPIO_EXTI_Callback() function to make it appear to the user that each external interrupt line has its own interrupt service function (these two functions will be explained later);
3. The EXTI15_10_IRQHandler function is defined in the stm32f4xx_it.c file, which calls the HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13); statement;
4. HAL_GPIO_EXTI_IRQHandler() is defined in the stm32f4xx_hal_gpio.c file. This function determines which external interrupt line generates the external interrupt, clears the corresponding bit in the interrupt pending register, and then calls the external interrupt callback function HAL_GPIO_EXTI_Callback(), and passes the interrupt line that generates the external interrupt as a parameter to the external interrupt callback function HAL_GPIO_EXTI_Callback();
5. All the above work is done by STM32CubeMX for us. We only need to redefine the HAL_GPIO_EXTI_Callback() function in the main.c file;
6. Because all external interrupts will call the HAL_GPIO_EXTI_Callback() function, we need to determine which external interrupt line has generated the external interrupt based on the input GPIO_Pin parameter in the HAL_GPIO_EXTI_Callback() function, and then execute different codes according to different external interrupts;
7. Therefore, we use the statement if(GPIO_Pin == GPIO_PIN_13) to determine whether the external interrupt is generated by external interrupt line 13, and then execute the HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); statement to flip the level state of the LED light;
Tips: We should not execute too much code in the interrupt service function or interrupt callback function to minimize interrupt nesting. For information about interrupt nesting and interrupt priority, please search Baidu. Interrupt priority (not limited to external interrupts) can be set in NVIC Configuration in STM32CubeMX.
Previous article:Getting Started Tutorial Using HAL Library, STM32CubeMX and Keil 5 Development (Part 1)
Next article:Why does STM32 remap boot from Flash address 0x08000000
Recommended ReadingLatest update time:2024-11-15 07:42
- Popular Resources
- Popular amplifiers
- Plant growth monitoring system source code based on Raspberry Pi 5
- 【Follow me Season 2 Episode 2】Arduion UR4 homework submission code
- Sensor Principle and Application Circuit Design (Edited by Chen Shuwang, Song Lijun, Xu Yunfeng)
- Principle and application of super-directivity of sensor array (Yang Yixin et al.)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Application and precautions of Hall elements
- Which one is better to use, the Type-C interface or the MicroUSB interface for mobile phone data cables?
- Showing goods + development board and multimeter
- Why is the GaN device [G-level is negative voltage powered]
- Signal Integrity and Power Integrity Analysis (3rd Edition)
- Share the FAQ about STM32/8
- MCU Model Challenge 1: Guess the manufacturer and model based on the MCU's power pins.
- 【i.MX6ULL】Driver Development 9——Linux IO Model Analysis
- What is the selection method and calculation process of ESD tube? Which forum friend can write out the calculation steps?
- Five Experiences in RF Circuit Design