LED running water lamp experiment based on STM32 standard library function

1. A brief introduction to STM32 standard library functions and the difference from register programming
The STM32 Standard Library is a set of software libraries provided by STMicroelectronics to simplify the process of developing applications on STM32 microcontrollers. It contains many commonly used functions and drivers that can be used to configure and operate various hardware resources of the STM32 microcontroller.

STM32 standard library functions can be divided into several main categories:

System initialization function: used to initialize the system clock, interrupt vector table and other system settings.

GPIO function: used to configure and operate general-purpose input/output ports, including setting pin direction, reading and writing pin status, etc.

Peripheral driver functions: used to configure and operate various peripherals, such as serial ports, SPI, I2C, etc. These functions include initializing peripherals, sending and receiving data, etc.

Interrupt handling function: used to configure and handle interrupts. They allow you to define interrupt service functions and associate them with specific interrupt sources.

Compared with register programming, the main advantage of STM32 standard library functions is that it simplifies the development process. Using standard library functions, you can configure and operate hardware resources by calling encapsulated functions without directly accessing registers. This can reduce the workload of writing a lot of low-level code and improve development efficiency.

2. Creation of STM32 standard library function project template

2.1 New construction
We first open keil MDK and click “NEW PROJECT”

Name the project name and file path. (It is recommended that the project name be in English). Once done, select STM32F103C8T6.


Don’t worry about the interface that pops up afterwards, just cancel it. This is to use its own library to add the required library functions. But I tried many times and found that some files were always missing, so I added them manually.

2.2 New group and addition of library function files
Enter the project interface and click on the three boxes pattern on the right side of the magic wand. This is used to manage groups.


Enter the management interface and add four groups in the middle, named Start, Library, User, and System.

At the same time, we also create new files with the same name in the corresponding folder of the project.

All configuration functions need to be downloaded from the ST official website: https://www.st.com/zh/embedded-software/stm32-standard-peripheral-libraries.html

In the Start local folder, we store the startup library functions required for STM32 programming. The required list is as follows. (The Startup function only needs to select the .md item, but there is no problem in adding them all, but we only use that file)

In the Library local folder, we store the application library functions required for STM32 programming, including source functions such as GPIO, interrupts, timing, watchdogs, etc. If we want to use these functions, we can directly find the corresponding functions and make standardized calls according to the function definition.

In the User local folder, we store the STM32 kernel function and our main function.

In the System local folder, we store the functions we want to define and configure. This also meets the needs of modular programming. If all functions are squeezed into the main function, it will look messy and illogical. So we group some functions together separately, write a header file, and call them separately in the main function when using them. Here we need to configure a delay function Delay.

We also need to modify the header file path so that the compiler can find the functions we added

Click the magic wand, select C/C++, and write the following characters in the “Define” column

Then click on the three dots to the right of “Include Paths” and change our path in the window Start, Library, System, and User files are all added.

Then we click on each group in keil and add the corresponding files in the local folder to the group. The adding results are as follows

We finally write the header file in the main function, the main function and the while loop. to compile. If there are no errors, it means the project was successfully created.

We are using the V5 compiler here. In the magic wand, select “Target” and set it

3. LED running water lamp experiment

Final code of program main function

#include "stm32f10x.h" // Device header
#include "Delay.h"



int main(void)
 {<!-- -->
GPIO_InitTypeDef GPIO_InitStructure;//?GPIO_InitStructure,GPIO_InitTypeDef,?Librarygpio.h
\t 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//GPIOA?,?
\t
//GPIOA
\t
\t
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//?
\t
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_0;//GPIOA?0123
\t
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//?50MHZ
\t
GPIO_Init(GPIOA, &GPIO_InitStructure);//GPIO_Init?GPIO?,?,
\t
while (1)
{<!-- -->
\t\t
GPIO_Write(GPIOA, ~0x0001); //16?2?,0000 0000 0000 0001,,A0
Delay_ms(1000);//1000ms,?1?
GPIO_Write(GPIOA, ~0x0002); //0000 0000 0000 0010,,,A1?
Delay_ms(1000);
GPIO_Write(GPIOA, ~0x0004); //0000 0000 0000 0100,,,A2
Delay_ms(1000);
GPIO_Write(GPIOA, ~0x0008); //0000 0000 0000 1000,,,A3
Delay_ms(1000);
\t\t
}
}

keil debugging and port level waveform display
In KEIL, we can not only use hardware to compile and download our code, but also use KEIL’s own software debugging function. We use the KEIL logic analyzer to observe the level change waveform of the port.

Because of the above situation, we changed the picture below to the following

Change 9 to 6 at line 1056
Click “Debug” again and modify the following options

After the modification is completed, click to open the debugging interface, and then open the logic analyzer

There is a “Set Up” option in the upper left corner of the logic analyzer, click on it.

In the pop-up window, we create a new observation pin. Enter (PORTA.X) in the box. Fill in the number of pins you want to observe. I am observing three pins 0, 1, and 2 here. After the addition is completed, be sure to change each “Display Type” to bits

Then get the waveform

According to the observation results, we can see that the waveforms change continuously. After PA0 changes from low to high, PA1 immediately changes to low level. The low level duration is 1s.
Add link description

4. Summary

The main content of this STM32 study is the LED running light based on the STM32 standard peripheral library. While studying the LED running light project, I gained an in-depth understanding of the usage of the STM32 standard peripheral library. By calling library functions, you can easily configure and control MCU peripherals, such as GPIO ports and timers. Compared with register programming, library functions are more logical and modular. Registers need to be constantly searched in the manual to find the addresses corresponding to the registers, including enable registers, output registers, etc. The process is very troublesome, but it is still completed by consulting the information.