Getting Started Tutorial Using HAL Library, STM32CubeMX and Keil 5 Development (Part 2): GPIO

Publisher:不染尘埃Latest update time:2018-05-06 Source: eefocusKeywords:STM32CubeMX  Keil Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 
Configure the LED light pin as output mode

Connect the button pin to the external interrupt line 13 
Connect the button pin to the external interrupt line 13

Click the GPIO configuration button 
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) 
Select GPIO mode as rising edge triggered external interrupt mode

Click the Nested Vectored Interrupt Controller Configuration button 
Click the Nested Vectored Interrupt Controller Configuration button

Check to enable external interrupt lines 10~15 and generate the corresponding initialization code 
Check to enable external interrupt lines 10~15 and generate the corresponding initialization code 
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.


Keywords:STM32CubeMX  Keil Reference address:Getting Started Tutorial Using HAL Library, STM32CubeMX and Keil 5 Development (Part 2): GPIO

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

Domestic manufacturers target 5G mobile phone LCP antenna modules, and market competition further intensifies
After Apple and Huawei released their 5G blockbuster models and sold them out, it also announced the arrival of the 5G phone replacement wave. According to data from the China Academy of Information and Communications Technology, the domestic market shipments of 5G mobile phones exceeded 100 million units in the fir
[Mobile phone portable]
Domestic manufacturers target 5G mobile phone LCP antenna modules, and market competition further intensifies
China Academy of Information and Communications Technology: In February, domestic 5G mobile phone shipments reached 15.07 million units, accounting for about 70%
Recently, the "Analysis Report on the Operation of the Domestic Mobile Phone Market in February 2021" released by the China Academy of Information and Communications Technology showed that in February 2021, the total shipments of the domestic mobile phone market were 21.759 million units, a year-on-year increase of 24
[Mobile phone portable]
China Academy of Information and Communications Technology: In February, domestic 5G mobile phone shipments reached 15.07 million units, accounting for about 70%
With the help of 5G technology, doctors use robots to perform remote surgery
Chinese doctors, led by Dr. Liu Rong, successfully completed the world's first remote operation using 5G technology, which was tested on pigs in Fujian Province using state-of-the-art technology. Dr. Liu Rong was able to remotely control a robotic arm through a 5G connection at a hospital 50 kilometers away to remov
[Embedded]
With the help of 5G technology, doctors use robots to perform remote surgery
Light up a LED using Keil
First, open our keil5 and create a new project After clicking Save, the following window will appear and you can set the following: Create a new file Direct Save Add the newly created file (demo.c above) to the target project: Start writing code: Generate .hex file: .hex is generated successfully. Th
[Microcontroller]
Light up a LED using Keil
iQOO 5 series released: Pro model joins hands with BMW 120W flash charging and can be fully charged in 15 minutes
       On the afternoon of August 17, smartphone brand iQOO held an online press conference to officially launch the new generation of flagship mobile phones, the iQOO 5 series. This series of mobile phones is divided into two products, iQOO 5 and iQOO 5 Pro. Among them, iQOO 5 Pro supports 120W ultra-fast flash charg
[Mobile phone portable]
It is reported that Samsung Electronics Chairman Lee Jae-yong visited the United States in May and met with CEOs of Apple, Google and other companies.
According to the news on April 21, the Korea Economic Daily reported on April 20 that Samsung Electronics Chairman Lee Jae-yong will go to the United States for three weeks in May to meet with CEOs of many technology giants and plan future business. According to reports, Lee Jae-yong is finalizing a business trip to
[Semiconductor design/manufacturing]
50 days ahead of schedule, the first batch of G8.5 mass production equipment was delivered to the new factory of Jingzhou Equipment
According to official news from Jingzhou Equipment, the company's mass production of G8.5 wet etching equipment was completed 50 days ahead of the standard production cycle and successfully rolled off the production line. At the beginning of the completion of Jingzhou Equipment's new factory, it received an order for
[Mobile phone portable]
The release of iPhone 5S brings about the battle for 64-bit processors
Apple Inc. has unveiled its long-awaited iPhone 5S, touting it as the world's first smartphone with a 64-bit processor that is far more powerful than its predecessor. In response, South Korea's Samsung said its next smartphone will also be equipped with a 64-bit processor.   Shin Jong-kyun, head of Samsung's mobile
[Analog Electronics]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号