1. Create a new project
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.
Double-click to enter:
3. Configure the clock (RCC)
after completing the above two steps, click j to enter the Clock Configuratiion window,
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:
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.
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.
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) ).
After setting:
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.
Enter the code generator interface and do the following: The
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:
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
the location where you just saved the file:
double-click to open the LED.uvproix file. As shown in the figure: The
gpio.c file is generated because we just selected:
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.
Reason: stm32cube will not actively add startup files to the project, we need to add them manually.
Solution:
(1)
Go to the location of the file and find the startup file . Right-click Aplication/user/code in keil5, select Add Existing Files...
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.
After the addition is complete, it is as follows:
Compile again: There is no error.
7.2 Burning/Downloading
Connect the board and click to
start burning.
Result:
Solution:
(1) Click the magic wand to enter the corresponding window.
(2) Click Debug, then select ST-Link or others according to the burning tool you use, and then click setting.
(3) After clicking the setting, it looks like this:
We change Port: JTAG to SW, and then the device name is displayed in the upper right corner, indicating that the connection is successful.
Programming again:
keil5 shows that the programming is successful, but there is no response on the board:
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.
Let's take a look at the burning result: success!
PC13 corresponds to the LED on, and PC14 corresponds to the LED off. Let's take a look at the schematic diagram: the
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.