STM32 based on HAL library running water lamp experiment

Experimental background:
LED running water lamp is an electronic device composed of multiple LED lamp beads arranged and controlled according to certain rules. In various display situations, LED running lights are often used to display dynamic effects, such as billboards, stage lighting, etc. In addition, LED running lights are also widely used in education, scientific research and other fields, and can help people understand the working principles and circuit design of microcontrollers.

Experimental purpose:
The main purpose of this experiment is to explore the control method of LED running lights and understand the use of the internal resources and peripheral libraries of the microcontroller. Through experiments, we hope to achieve the following goals:

Master the programming method of LED running lights based on standard peripheral libraries;
Learn to use the internal resources of the microcontroller to control LED running lights;
Master the basic connection methods of circuits and learn to build hardware according to schematic diagrams;
Analyze experimental results to understand the effects of circuit connections and program execution.

Experimental principle:
The control principle of LED running lights can be realized through the microcontroller peripheral library and internal resources. The peripheral library provides operation functions for the I/O port of the microcontroller, such as delays, counters, interrupts, etc. Internal resources include memory, timers, serial ports, etc., which can be used to store program codes, control the on-off time of LED lamp beads, and implement communication and other functions. In terms of circuit connection, the positive pole of the LED lamp bead needs to be connected to the I/O port of the microcontroller, the negative pole is connected to ground, and an appropriate resistor is connected in series to protect the LED lamp bead.

1. Build the STM32 development environment
(1) Install jdk
Since STM32CubeMX is implemented in Java, a jdk environment needs to be installed.
jdk official website download link:
https://www.oracle.com/java/technologies/javase-downloads.html

(2) Install STM32CubeMX
1. Download address:
https://www.st.com/en/development-tools/stm32cubemx.html
2. Open after installation is complete

2. Periodic flashing of three LED traffic lights on the HEL library GPIO port
(1) CubeMX construction project
1. Install the firmware library
Open STMCubeMX

Confirm installation

2. Create a new project
Return to the main interface of STMCubeMX and create a new project:

Select chip STM32F103C8

RCC selection

Select the port output setting, select the required port, and click to set GPIO_OUTPUT. I chose PC13, PA15, PB0

Next look at the clock architecture

Project Manager settings

After creating the project, open it with Keil

(2) Main.c code modification

1. Enter the following code in while(1)

 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Light 1 is on
HAL_Delay(1000); // Delay 1s
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Light 1 is off
\t  
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET); // Light 2 is on
HAL_Delay(1000); // Delay 1s
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); // Light 2 is off
\t  
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); // Light 3 is on
HAL_Delay(1000); // Delay 1s
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); // Light 3 is off

2. Click compile to generate hex file

(3) Burning operation results
Burning tool
Hardware tools:
st-link v2
STM32F103C8 development board
LED light
Several lines

software tools:
keil c51

2.Result display
Insert image description here

3. Waveform Observation
Simulation environment settings
debug settings

2. Click the magic wand, select the target, and select the crystal oscillator as 8MHZ:
(Note: This option plays a very important role in software simulation. If the wrong choice is made, the waveform must be wrong because the time is inaccurate)

3. Click Debug

4. After the settings are completed, turn on the debugging mode and open the logic analyzer:

5.Select the pins you want to observe
①Click Setup Logic Analy
②Add the pins to be observed

6. Enter (PORTB & amp; 0X00000000)>>0, select Bit for Display Type, and enter (PORTA & amp; 0X00008000)>>15, (PORTC & amp; 0X00002000)>>13 in sequence

7. Click to run at full speed; after executing for a period of time, click the stop button:
Another: Click in or out to set the size of the Grid to 1s. Check signal info and cursor to set the starting line and see relevant information:

8. Waveform chart
After running for a period of time, click Pause;
In order to observe whether the delay function is accurate: move the mouse to a falling edge of the first waveform and click to lock the time at this moment; then move the mouse to the falling edge of the second waveform to overlap it, and observe the two time:

Summarize
Through this experiment, I not only understood the basic principles of STM32cubemx, but also used HAL library programming in the development environment of stm32cubeMX to realize the simulation operation of LED running lights and Keil environment, and observed its waveforms, which further deepened my understanding of Understanding and applying some knowledge. During the actual operation, there were still some minor problems, but after online inquiries and the help of classmates, the experiment was completed.