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)
Click I accept…then next
Select the installation location and then next
Continue next
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
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.
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
(3) New project
(1) Return to the main page to create a new project
(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
(3) Click system core, enter SYS, and select serial wire under debug
(4) Configure the clock, enter the rcc above, there are two clocks, one is hse and lse, set the hse to Crystal/Ceramic Resonator:
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.
(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:
(6) Click project manager, configure your path and project name, and then change the IDE item to MDK-ARM:
(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
(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.
Select the output option in the magic wand to generate a hex file
(3) Burning
Line connection: red – B9, green – C15, yellow – A4
Burn, select the generated hex file
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.
Click Debug to enter the debugging interface and select the logic analyzer
Add the pins you want to observe
Run and observe waveforms
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)
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.
2.2 Name GPIO_PA5 as LD1
2.3 Enable the external interrupt line corresponding to the pin EXTI line[15:10]
2.4 Configure interrupt priority
3. Generate project
(2) keil simulation test
1. Write the code and put the code into the corresponding position of the main function
2. Compile and burn, the same settings as the above content
Lighting effect:
Reference article:
https://blog.csdn.net/m0_58892312/article/details/121004248
https://blog.csdn.net/weixin_46129506/article/details/120780184