STM32CubeMX study notes (2)-GPIO interface use

1. New construction

1. Open the STM32CubeMX software and click "New Project"

2. Choose MCU and package

3. Configure the clock
RCC settings, select HSE (external high-speed clock),

select Clock Configuration for Crystal/Ceramic Resonator (crystal oscillator/ceramic resonator) , configure the system clock SYSCLK to 72MHz,
modify the value of HCLK to 72, and press Enter. Automatically modify all configurations

4. It
is a very important step to configure the debug mode , otherwise the debugger
SYS setting will not be recognized after the first programming program , select Debug as Serial Wire

Two, GPIO output

2.1 Parameter configuration

In System Coreselecting GPIOsettings.

Find the corresponding pin of the LED light in the figure on the right and select it GPIO_Output.

In GPIO output levelselecting Lowthe output of the low level lighting, custom labels may be added (this will be defined according to the generated code label macro setting pin).

2.2 Generate code

Path item names and item input

selection of an application development environment IDE MDK-ARM V5

each peripheral generates a separate ’.c/.h’file
not hook: All initialization code is generated in main.c
checked: initialization code file generated in the associated peripheral. For example, the GPIO initialization code is generated in gpio.c.
Click GENERATE CODE to generate code

2.3 Comparison of HAL library and standard library code

STM32CubeMX uses the code generated by the HAL library:

#define LED_G_Pin GPIO_PIN_0
#define LED_G_GPIO_Port GPIOB
#define LED_B_Pin GPIO_PIN_1
#define LED_B_GPIO_Port GPIOB

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
    
    
    /*定义一个GPIO_InitTypeDef类型的结构体*/
    GPIO_InitTypeDef GPIO_InitStruct = {
    
    0};

    /* GPIO Ports Clock Enable */  /*开启LED相关的GPIO外设时钟*/
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /*Configure GPIO pin Output Level */ /* 关闭所有led灯	*/
    HAL_GPIO_WritePin(GPIOB, LED_G_Pin|LED_B_Pin, GPIO_PIN_SET);

    /*Configure GPIO pins : LED_G_Pin LED_B_Pin */
    GPIO_InitStruct.Pin = LED_G_Pin|LED_B_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

HAL_GPIO_WritePin(GPIOB, LED_G_Pin|LED_B_Pin, GPIO_PIN_SET); // 输出高电平
HAL_GPIO_WritePin(GPIOB, LED_G_Pin|LED_B_Pin, GPIO_PIN_RESET); // 输出低电平
HAL_GPIO_TogglePin(GPIOB, LED_G_Pin);  // 反转

Use STM32 standard library code:

// R-红色
#define LED1_GPIO_PORT    	GPIOA			            /* GPIO端口 */
#define LED1_GPIO_CLK 	    RCC_APB2Periph_GPIOB		/* GPIO端口时钟 */
#define LED1_GPIO_PIN		GPIO_Pin_8			        /* 连接到SCL时钟线的GPIO */

// G-绿色
#define LED2_GPIO_PORT    	GPIOB			            /* GPIO端口 */
#define LED2_GPIO_CLK 	    RCC_APB2Periph_GPIOB		/* GPIO端口时钟 */
#define LED2_GPIO_PIN		GPIO_Pin_0			        /* 连接到SCL时钟线的GPIO */

/**
  * @brief  初始化控制LED的IO
  * @param  无
  * @retval 无
  */
void LED_GPIO_Config(void)
{
    
    		
    /*定义一个GPIO_InitTypeDef类型的结构体*/
    GPIO_InitTypeDef GPIO_InitStructure;

    /*开启LED相关的GPIO外设时钟*/
    RCC_APB2PeriphClockCmd( LED1_GPIO_CLK | LED2_GPIO_CLK, ENABLE);
		
    /*选择要控制的GPIO引脚*/
    GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN;	
    /*设置引脚模式为通用推挽输出*/
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
    /*设置引脚速率为50MHz */   
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    /*调用库函数,初始化GPIO*/
    GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);	
		
    /*选择要控制的GPIO引脚*/
    GPIO_InitStructure.GPIO_Pin = LED2_GPIO_PIN;
    /*调用库函数,初始化GPIO*/
    GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);

    /* 关闭所有led灯	*/
    GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN);
    GPIO_SetBits(LED2_GPIO_PORT, LED2_GPIO_PIN);	 
}

GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); // 输出高电平
GPIO_ResetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); // 输出低电平

__HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();Correspondence RCC_APB2PeriphClockCmd( LED1_GPIO_CLK | LED2_GPIO_CLK, ENABLE);
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);correspondence GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);
HAL_GPIO_WritePin(GPIOB, LED_G_Pin|LED_B_Pin, GPIO_PIN_SET);correspondenceGPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN);GPIO_SetBits(LED2_GPIO_PORT, LED2_GPIO_PIN);

Three, GPIO input

3.1 Parameter configuration

In System Coreselecting GPIOsettings.

Find the corresponding pin of the button in the figure on the right and select it GPIO_Input.

3.2 Generate code

Enter the project name and project path,

select the IDE development environment MDK-ARM V5 of the application, and

click GENERATE CODE to generate the code

3.3 Comparison of HAL library and standard library code

STM32CubeMX uses the code generated by the HAL library:

#define KEY2_Pin GPIO_PIN_13
#define KEY2_GPIO_Port GPIOC
#define KEY1_Pin GPIO_PIN_0
#define KEY1_GPIO_Port GPIOA

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
    
    
    GPIO_InitTypeDef GPIO_InitStruct = {
    
    0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /*Configure GPIO pin : KEY2_Pin */
    GPIO_InitStruct.Pin = KEY2_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(KEY2_GPIO_Port, &GPIO_InitStruct);

    /*Configure GPIO pin : KEY1_Pin */
    GPIO_InitStruct.Pin = KEY1_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(KEY1_GPIO_Port, &GPIO_InitStruct);
}

HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin); // 读取按键状态

Use STM32 standard library code:

#define    KEY1_GPIO_CLK     RCC_APB2Periph_GPIOA
#define    KEY1_GPIO_PORT    GPIOA			   
#define    KEY1_GPIO_PIN     GPIO_Pin_0

#define    KEY2_GPIO_CLK     RCC_APB2Periph_GPIOC
#define    KEY2_GPIO_PORT    GPIOC		   
#define    KEY2_GPIO_PIN     GPIO_Pin_13

/**
  * @brief  配置按键用到的I/O口
  * @param  无
  * @retval 无
  */
void Key_GPIO_Config(void)
{
    
    
	GPIO_InitTypeDef GPIO_InitStructure;
	
	/*开启按键端口的时钟*/
	RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);
	
	//选择按键的引脚
	GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN; 
	// 设置按键的引脚为浮空输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	//使用结构体初始化按键
	GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
	
	//选择按键的引脚
	GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN; 
	//设置按键的引脚为浮空输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	//使用结构体初始化按键
	GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);	
}

GPIO_ReadInputDataBit(KEY1_GPIO_PORT, KEY1_GPIO_PIN);

__HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();Correspondence RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);
HAL_GPIO_Init(KEY1_GPIO_Port, &GPIO_InitStruct);correspondence GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);correspondenceGPIO_ReadInputDataBit(KEY1_GPIO_PORT, KEY1_GPIO_PIN);

Four, matters needing attention

User code to be added to USER CODE BEGIN Nand USER CODE END Nbetween, otherwise the next use after STM32CubeMX regenerate the code, it will be deleted.


Written by Leung on January 11, 2021

• Reference: STM32CubeMX series tutorial 1: GPIO

Guess you like

Origin blog.csdn.net/qq_36347513/article/details/112464598