Running water lamp based on HAL library

Article directory

    • 1. Use the GPIO port to complete the periodic flashing of three LED traffic lights.
      • (1) Install STM32CubeMX
        • 1.Download
        • 2. Installation process
      • (2) Install HAL library
      • (3) New projects
      • (4) keil simulation debugging
        • (1)Write code
        • (2) Compilation settings
        • (3) Burning
        • (4) Observe the waveform of the GPIO port
    • 2. Use interrupt mode programming. When the switch is connected to high level, the LED running water lamp will work; when the switch is connected to low level, the LED running water lamp will stop working.
      • (1) New projects
      • (2) keil simulation test

1. Use the GPIO port to complete the periodic flashing of three LED traffic lights

(1) Install STM32CubeMX

1.Download

[https://www.st.com/en/development-tools/stm32cubemx.html]:

It is best to register an account. You will also need to register later. After registering, select the version (choose a newer version) and you can directly download the compressed package.

Note: Since STM32CubeMX is implemented in Java, a jdk environment needs to be installed. (You can download STM32CubeMX first. If there is no jdk installed, it will jump to the jdk official website and download it directly)

2. Installation process

Open the program file in the compressed package and click next (do not download version 4.27.0, it is too old, but the installation steps are the same)

image-20231021145422726

Click I accept…then next

image-20231021145703008

Select the installation location and then next

image-20231021145756000

Continue next

image-20231021150155347

image-20231021150230137

Then click Done to install successfully.

(2) Install HAL library

(1) Run the installed STM32CubeMX as administrator, click help, and select Manage embedded software packages

image-20231021150942574

Note: If a pop-up window pops up: The Updater is already in use and checking the server. Please retry launching the Updater after a few seconds

Solution: 1. Check whether you are running as an administrator

? 2. Click help, select check for updates, and click refresh to solve the problem.

image-20231021151316639

If there are no above problems, you can ignore it. After selecting Manage embedded software packages as mentioned above, select the required firmware library, and then install now

image-20231021151740548

image-20231021154105583

(3) New project

(1) Return to the main page to create a new project

image-20231021154503663

(2) Select your own chip in PartNumber in the upper left corner, then select your own chip in the information bar, and click start project in the upper right corner

image-20231021154533632

(3) Click system core, enter SYS, and select serial wire under debug

image-20231021154849878

(4) Configure the clock, enter the rcc above, there are two clocks, one is hse and lse, set the hse to Crystal/Ceramic Resonator:

image-20231021155042273

Next, observe the clock architecture. The clock of the APB2 bus is controlled by hse. At the same time, you must select the right side of PLLCLK on this interface.

image-20231021155107444

(5) The next step is to click on the corresponding pin to set the output register, which is the output item. There are three selected in total, which are PA4, PB9, and PC15:

image-20231021160042185

image-20231021160145808

(6) Click project manager, configure your path and project name, and then change the IDE item to MDK-ARM:

image-20231021160231217

(7) Enter the code generate interface, select to generate the initialization .c/.h file, then click generate code, select open project, and then go to KEIL5

image-20231021160314193

(4) keil simulation debugging

(1)Write code

Open the main.c main function and replace the main function part

SystemClock_Config();//System clock initialization
  MX_GPIO_Init();//gpio initialization
  while (1)
  {
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);//PA4 lights up
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);//PB9 lights out
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);//PC15 lights out
HAL_Delay(1000);//delay 1s
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);//PA4 lights out
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);//PB9 lights up
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);//PC15 lights out
HAL_Delay(1000);//Delay 1s
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);//PA4 lights out
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);//PB9 lights out
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_RESET);//PC15 lights up
HAL_Delay(1000);//Delay 1s
}

(2) Compilation settings

Click the magic wand to select Target and select v6. If you press the default selection, it may not run.

image-20231021160732992

Select the output option in the magic wand to generate a hex file

image-20231021160904899

(3) Burning

Line connection: red – B9, green – C15, yellow – A4

image-20231021161926707

Burn, select the generated hex file

image-20231021162021806

Lighting effect:

(4) Observe the waveform of the GPIO port

Click the magic wand to enter the Debug interface and perform the settings shown below.

image-20231021165523114

Click Debug to enter the debugging interface and select the logic analyzer

image-20231021165725430

Add the pins you want to observe

image-20231021165751836

image-20231021165827596

Run and observe waveforms

image-20231021165901266

The result is that the light on the pin is at low level, and the light at high level is not on. The high and low level conversion period (LED flashing period) is about 1s.

2. Use interrupt mode programming. When the switch is connected to high level, the LED running water lamp will work; when the switch is connected to low level, the LED running water lamp will stop working.

(1) New project

1. Create a new project as mentioned above, select your own chip in PartNumber in the upper left corner, then select your own chip in the information bar, click start project in the upper right corner, and then configure the system debugging interface SYS, select Serial Wire. Configure peripherals RCC and select HSE (external high-speed clock) as Crystal/Ceramic Resonator (crystal/ceramic resonator)

image-20231021170848110

image-20231021170907551

2. Select the LED light pin PA5 and set the pin to output mode GPIO_Output;
Select the pin PB15 as the external interrupt and set it to be connected to the interrupt line GPIO_EXTI15.

2.1 Command GPIO_PB15 to B1_EXTI and select falling edge trigger as trigger mode.

image-20231021171026976

2.2 Name GPIO_PA5 as LD1

image-20231021171049758

2.3 Enable the external interrupt line corresponding to the pin EXTI line[15:10]

image-20231021171304161

2.4 Configure interrupt priority

image-20231021171333798

3. Generate project

image-20231021171413237

(2) keil simulation test

1. Write the code and put the code into the corresponding position of the main function

image-20231021171706517

2. Compile and burn, the same settings as the above content

image-20231021171836757

Lighting effect:


Reference article:
https://blog.csdn.net/m0_58892312/article/details/121004248
https://blog.csdn.net/weixin_46129506/article/details/120780184