【STM32】点亮一个LED灯(HAL库版本)
本文基于正点原子探索者开发板(STM32F407ZGT6),开发工具为Keil5。
我还写了如何配置寄存器点亮LED,文中详细解析了对应寄存器,感兴趣的看官可点击跳转观看。
【STM32】点亮一个LED(寄存器版本)
查看原理图
首先需要打开开发板原理图,找到LED对应的GPIO引脚
以LED0为例,LED0对应的GPIO为PF9,我们需要操作PF9这个引脚,看原理图可知,右边接的是VCC,给PF9输出低电平,LED就能亮了。
使用HAL库点亮LED0
打开GPIOF时钟
操作IO口首先需要打开IO口对应的时钟,这个函数是针对PF所有引脚的
__HAL_RCC_GPIOF_CLK_ENABLE();
初始化GPIO
调用下列的函数即可初始化对应的GPIO
/**
* @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
* @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
* x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
* @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);
这个函数需要两个参数,GPIOx指的是需要初始化IO组的基地址,GPIO_Init为初始化GPIO的一个结构体。
先看第一个参数,这里我们要操作的IO是PF9,所以对应的IO分组为GPIOF,这个组的地址,在stm32f407xx.h中已经给我们定义好了,是一个名为GPIOF的宏,我们打开此头文件即可看到。
跳转GPIOF可看到,GPIOF_BASE对应的其实就是一个地址
第二个参数需要传GPIO_InitTypeDef结构体,我们看看此结构体
/**
* @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 */
uint32_t Alternate; /*!< Peripheral to be connected to the selected pins.
This parameter can be a value of @ref GPIO_Alternate_function_selection */
}GPIO_InitTypeDef;
此结构体第一个成员变量Pin指的是操作哪个pin脚,Mode对应的是工作模式,Pull对应的是上下拉,Speed对应的是输出速度,Alternate对应的是复用功能。
GPIO的工作模式有8种,此次我们使用推挽输出
对应的宏可在此头文件中查看
我们代码中,定义一个此结构体,并给对应成员赋值,传入到HAL_GPIO_Init函数中,Pin对应的是GPIO_PIN_9,模式使用推挽输出GPIO_MODE_OUTPUT_PP,输出速度设置为GPIO_SPEED_FREQ_HIGH,速度的宏也可查看stm32f4xx_hal_gpio.h此文件。
GPIO_InitTypeDef gpio_init_struct;
gpio_init_struct.Pin = GPIO_PIN_9; /* LED0 引脚 */
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; /* 推挽输出 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
HAL_GPIO_Init(GPIOF, &gpio_init_struct);
此时,我们的PF9引脚已经初始化完毕!
写入数据
使用HAL库,给PF9写入低电平,即可点亮LED0
/**
* @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..K) to select the GPIO peripheral for STM32F429X device or
* x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
* @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);
第一个参数为GPIO组的基地址,第二个参数为对应操作的Pin脚,第三个参数是一个名为GPIO_PinState的枚举,可以设置pin脚的状态。第一、二个参数我们已经熟悉了,第三个参数我们可以传0或者1,0代表低电平,1代表高电平,不过hal库stm32f4xx_hal_gpio.h头文件中有一个GPIO_PinState枚举,可对应高低电平。
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_RESET);
此时,编译、下载,即可点亮LED0!
全部代码
main.c
#include "stm32f4xx_hal.h"
int main(void)
{
__HAL_RCC_GPIOF_CLK_ENABLE();//GPIO时钟使能
GPIO_InitTypeDef gpio_init_struct;
gpio_init_struct.Pin = GPIO_PIN_9; /* LED0 引脚 */
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; /* 推挽输出 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
HAL_GPIO_Init(GPIOF, &gpio_init_struct);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_RESET);
while(1)
{
}
}
以上就是这篇文章的所有内容了,此为个人学习记录,如有哪个地方写的有误,劳烦大佬指出,感谢,希望对各位看官有所帮助!
作者:IM雾凇