STM32CubeMX configures GPIO to light up the LED

1. Create a new project
Insert picture description here
2. Select the chip you are using: Take stm32F405 as an example, enter the corresponding chip in Part Number, and then the chip of the desired model will appear in the MCUs/MPUs List window, and double-click to open it.

Insert picture description here
Double-click to enter:
Insert picture description here

3. Configure the clock (RCC)
Insert picture description here
after completing the above two steps, click j to enter the Clock Configuratiion window,
Insert picture description here
and then proceed to the second step in the above figure: select HSE (ie, external high-speed clock, HSI is high-speed internal clock), external clock frequency range It is 4MHz-16MHz, we choose 8MHz here. That is, the 8 set in the third step; finally, as in the fourth step, select HSE in the system clock mux.

4. Configure the debug
operation as shown in the figure below:
Insert picture description here
5. Configure GPIO.
Because our purpose at this time is to light up the LED lights on the board, we first determine the port corresponding to the LED on the stm32f405RG chip.
Insert picture description here
It can be seen on the schematic diagram that there are four LED ports corresponding to PA2, PA8, PC13, and PC14. This time we use PC13 and PC14 to carry out the experiment, set the port of PC13 to high level, and set the port of PC14 to low level to achieve the purpose of comparison.
Insert picture description here
First find the right port on the board, left-click, and then select GPIO_Output. Set the corresponding ports of the two LEDs as output ports. After setting, two port information appears in the middle box, click to expand, the corresponding information appears below, the red box is the port level status, we set one of them to low (PC13), and the other to high (PC14) ).
Insert picture description here
After setting:
Insert picture description here

6. Project settings
Enter the project Manager interface and do the following operations in the project window:
(1) Fill in the project name
(2) Select the save location
(3) Select the compiler, I am using MDK
(4) Select the firmware library, if √ Go to use latest available version, you may need to download the latest version. You can choose by yourself.
Insert picture description here
Enter the code generator interface and do the following: The
Insert picture description here
second step of generating settings is to generate the corresponding .c and .h files for each peripheral, which will help simplify our code.
Finally, click on generator code:
Insert picture description here
If the following situation occurs, don’t worry, the code has been successfully generated, but for some reason we can’t directly open the MDK, we can enter the folder to open the project. Go to
Insert picture description here
the location where you just saved the file:
Insert picture description here
Insert picture description here
double-click to open the LED.uvproix file. As shown in the figure: The
Insert picture description here
gpio.c file is generated because we just selected:
Insert picture description here
if we don't check it, gpio.c will be merged into main.c, making main.c more complicated and difficult to understand.

7. Compile, burn/download to the board
7.1 Compile
Click to compile and
compile the result: an error appears, the content of the error is shown in the long box: LED\LED.sct(7): error: L6236E: No section matches selector- no section to be FIRST/LAST. It means that the startup file is missing. Insert picture description here
Reason: stm32cube will not actively add startup files to the project, we need to add them manually.
Solution:
(1)
Insert picture description here
Go to the location of the file and find the startup file . Right-click Aplication/user/code in keil5, select Add Existing Files...
Insert picture description here
and then go to the previous folder and select the startup file. If you can't find it, it may be a file The type setting is incorrect, just set it to All Files. Insert picture description here
After the addition is complete, it is as follows:
Insert picture description here
Compile again: There is no error.
Insert picture description here
7.2 Burning/Downloading
Connect the board and click to Insert picture description here
start burning.
Result:
Insert picture description here
Solution:
(1) Click the magic wand to enter the corresponding window.
Insert picture description here
(2) Click Debug, then select ST-Link or others according to the burning tool you use, and then click setting.
Insert picture description here
(3) After clicking the setting, it looks like this:
Insert picture description here
We change Port: JTAG to SW, and then the device name is displayed in the upper right corner, indicating that the connection is successful.
Insert picture description here
Programming again:
Insert picture description here
keil5 shows that the programming is successful, but there is no response on the board:
Insert picture description here
Solution: Click the magic wand, then click debug, click setting, click Flash download in the change window, and check reset and run. In this way, the board will respond immediately after downloading the board.
Insert picture description here
Let's take a look at the burning result: success!
Insert picture description here
PC13 corresponds to the LED on, and PC14 corresponds to the LED off. Let's take a look at the schematic diagram: the
Insert picture description here
left ends of the two LEDs have been connected to high level. If the right side is connected to high level, the diode will not conduct and the LED light will be off. Otherwise, it is bright, which is the same as our result.

Guess you like

Origin blog.csdn.net/qq_43516928/article/details/112801390