STM32CubeMX series 02 - LED, button

====>>> Article summary (with code summary) <<<====

1. Hardware used

Punctual atom Mini board, the main control STM32F103RCT6.

Peripherals used: LED, buttons. Schematic:
insert image description here

2. Generate project

2.1. Create a project

insert image description here

2.2. Search and double-click to select the main control chip

insert image description here

2.3. Configuration

Configure the clock source
insert image description here
Configure the debug mode (you can check it if you need ST-Link download and debug)
insert image description here
Configure the clock tree (you can directly enter 72 in HCLK, and then press Enter to automatically configure)
insert image description here

2.4. Configuration project directory

insert image description here
If checked, c files and header files will be generated separately
insert image description here

2.5. Peripherals used in configuration

PA8: LED0.
PC5: KEY0, configuration input mode, key scanning.
insert image description here

2.6. Generate code

After the configuration is complete, click Generate Code.
insert image description here

3. Directory structure

The generated project directory is as follows:
Core: The source files and header files of the peripherals used in the software generation, main.c is also here. Equivalent to User + Hardware in MDK.
Drivers: HAL library driver files, chip startup files.
MDK-ARM: MDK project file. Enter the MDK-ARM directory to open the project.
insert image description here

4. Code

Add the following code in main.c.

  while (1)
  {
    
    
	if(0 == HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin))
	{
    
    
		HAL_Delay(20);
		if(0 == HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin))
		{
    
    
			// 输出高电平
			// HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
			// 输出低电平
			// HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
			// 反转
			HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
		}
	}
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

Among them: pin aliases such as LED0_GPIO_Port and LED0_Pin are defined in main.h.

5. Effect verification

Compile, burn, and view the results.

Effect: Press the button and the LED will reverse.

Guess you like

Origin blog.csdn.net/weixin_46253745/article/details/127802976