单片机-STM32部分:5、STM32CubeMX实现HAL点灯
飞书文档https://x509p6c8to.feishu.cn/wiki/TcUlwkSVci30jrkdIEqcqlZLnPe
原理图原理说明
| |
新建工程
复制打开工程模板ProjectTemplates
在右侧的PinOut View中找到PA6
设置PA6为输出模式(这里我们可以看到PA6这个IO是支持多种功能的)
例如ADC、SPI、TIM、GPIO这些功能,与芯片手册是一一对应的
GPIO输出参数说明
然后设置硬件对应的参数
先了解原理部分:在芯片手册中,可以看到GPIO部分的结构图:
保护二极管: |
再了解如何使用:
GPIO output level:上电后的默认输出电平,有low和high两种选择 |
代码生成&理解:
然后点击生成代码,重新打开工程,打开main.c,这里就是整个工程的入口了。
HAL_Init();用于初始化HAL库相关配置
SystemClock_Config();用于初始化芯片时钟配置
MX_GPIO_Init();用于初始化GPIO配置
跳转MX_GPIO_Init(),在这里,我们可以看到里面的配置是由CUBEMX中的配置生成的,
void MX_GPIO_Init(void)
{GPIO_InitTypeDef GPIO_InitStruct = {0};/* GPIO Ports Clock Enable */__HAL_RCC_GPIOD_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);/*Configure GPIO pin : PtPin */GPIO_InitStruct.Pin = LED1_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_PULLUP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);}
接口讲解:
/*** @brief Sets or clears the selected data port bit.** @note This function uses GPIOx_BSRR register to allow atomic read/modify* accesses. In this way, there is no risk of an IRQ occurring between* the read and the modify access.** @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral* @param GPIO_Pin: specifies the port bit to be written.* This parameter can be one of GPIO_PIN_x where x can be (0..15).* @param PinState: specifies the value to be written to the selected bit.* This parameter can be one of the GPIO_PinState enum values:* @arg GPIO_PIN_RESET: to clear the port pin* @arg GPIO_PIN_SET: to set the port pin* @retval None*/
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)GPIOx=GPIOA
GPIO_Pin=GPIO_PIN_6
PinState=GPIO_PIN_SET
对应初始化时,设置GPIOA6输出高电平/*** @brief GPIO Init structure definition*/
typedef struct
{uint32_t Pin; /*!< Specifies the GPIO pins to be configured.This parameter can be any value of @ref GPIO_pins_define */uint32_t Mode; /*!< Specifies the operating mode for the selected pins.This parameter can be a value of @ref GPIO_mode_define */uint32_t Pull; /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.This parameter can be a value of @ref GPIO_pull_define */uint32_t Speed; /*!< Specifies the speed for the selected pins.This parameter can be a value of @ref GPIO_speed_define */
} GPIO_InitTypeDef;
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LED1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
初始化结构体变量
设置GPIO_PIN_6为推挽输出,上拉,速度为低速/*** @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.* @param GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral* @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains* the configuration information for the specified GPIO peripheral.* @retval None*/
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
最后初始化GPIOA GPIO_PIN_6为推挽输出,上拉,速度为低速
控制GPIO输出
/* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);HAL_Delay(500);HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);HAL_Delay(500);}/* USER CODE END 3 */设置Pin输出高低电平
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState);
当然,也可以使用HAL_GPIO_TogglePin控制输出电平翻转
/* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);HAL_Delay(500);}/* USER CODE END 3 */
}设置Pin电平翻转
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
烧录、运行
4、设置烧录参数,烧录固件
多个LED控制
如果希望所有灯同时闪烁,则同时添加多个IO
while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);HAL_GPIO_TogglePin(LED3_GPIO_Port,LED3_Pin);HAL_Delay(500);}/* USER CODE END 3 */
补充:复用输出功能
复用输出功能:可以理解为GPIO口被用作第二功能时的配置情况(即并非作为通用IO口使用) |