[Getting started with STM32CubeIDE] (2) Classic program: GPIO configuration lights up LED lights

If you don't know how to create a project file, you can refer to an article I wrote before: [Getting Started with STM32CubeIDE] (1) Project Creation & Project Configuration

 1. First observe which LED pin is on the board. I use the smallest development board of stm32f103c8t6, ​​and the LED pin is PC13.

 2. After creating the project file in the previous section, select the PC13 pin and select the output mode.

 

3. GPIO mode configuration, by the way, open and generate the corresponding gpio.c/.h file configuration.

 4. After the configuration is complete, click to automatically generate the configuration code.

 5. After the configuration is completed, you can view the automatically generated led.c/.h file and the definition of the led gpio pin added in main.h in the project directory on the left.

 6. Create a folder to store the peripheral files used, and write the lighting driver code in the led.h file just created. Because the LED light on the board is lit at a low level, the pin must be set to a low level to make the light on.

#ifndef __LED_H__
#define __LED_H__

#include "stm32f1xx.h"
#include "main.h"

#define LED_ON()	HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET)
#define LED_OFF()	HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET)
#define	LED_INV()	HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin)

#endif

 7. Then you need to specify the path of the led.h header file, so that the compiler can find it. Click on Project -> Properties.

 8. Add the led.h header file in main.c, and write the light code. When writing, we must pay special attention to the program we write must be behind USER CODE BEGIN and before USER CODE END, otherwise it will automatically It will be lost when generating the code.

 

 9. Connect the board to the emulator, and then click Run to burn the program into the board. Below you can also see a situation where the program occupies board resources.

 

 

Guess you like

Origin blog.csdn.net/weixin_48896613/article/details/127245357