当前位置:网站首页>基于HAL库的LED驱动库
基于HAL库的LED驱动库
2022-06-30 10:20:00 【Coder_Ja】
led.h
/** ******************************************************************************** * @file led.h * @brief 本文件包含一些led基本操作函数的定义,包括LED的亮、灭,闪烁 * @author Jia Yasen * @date 2022-5-23 * @version V0.0.1 * @copyright Etherealize ******************************************************************************** * @attention 1.本文件的函数均基于HAL库,由于LED的初始化基于GPIO的初始化,GPIO的初始化 * 直接在CubeMX中配置,用CubeMX生成的GPIO初始化函数来初始化. * 2.由于不同板子的对应引脚不同,使用前需先对照原理图修改LED引脚的对应及LED * 亮灭状态与引脚高低电平的对应 ******************************************************************************** */
#ifndef __LED_H__
#define __LED_H__
/*-----------------------------Includes---------------------------------------*/
//根据使用的芯片型号进行修改
#include "stm32f1xx_hal.h"
/*-----------------------------Define-----------------------------------------*/
//定义LED的GPIO组号和引脚号;使用前需要根据板子的原理图进行修改
#define LED1_PORT GPIOx
#define LED1_PIN GPIO_PIN_x
//若需要多个LED可再添加定义
//定义LED亮与灭对应的引脚电平状态,不同电路设计的对应不同,使用前需先对照原理图修改
#define LED_STATE_ON GPIO_PIN_RESET
#define LED_STATE_OFF GPIO_PIN_SET
/*-----------------------------TypeDef----------------------------------------*/
typedef struct
{
GPIO_TypeDef* led_port;
uint16_t led_pin;
}LED_TypeDef;
/*-----------------------------variables--------------------------------------*/
//声明成外部变量,这样可以在引用了该头文件的文件中使用这个变量
//若需要使用多个LED则自行定义
extern LED_TypeDef LED1;
/*-----------------------------Function---------------------------------------*/
void LED_Init(void);
void LED_ON(LED_TypeDef LED_Ptr);
void LED_OFF(LED_TypeDef LED_Ptr);
void LED_Blink(LED_TypeDef LED_Ptr, uint32_t Delay);
#endif
/************************ (C) COPYRIGHT Etherealize*****END OF FILE***********/
led.c
/** ******************************************************************************** * @file led.c * @brief led操作源文件,整体操作均通过一个LED结构体来实现 * @author Jia Yasen * @date 2022-5-23 * @version V0.0.1 * @copyright Etherealize ******************************************************************************** */
/*-----------------------------Includes---------------------------------------*/
#include "led.h"
/*-----------------------------variables--------------------------------------*/
//若需要使用多个LED,可再定义多个LED结构体
//LED_TypeDef* LED1_Ptr;
//若定义结构体指针,则需要给指针开辟空间,只是定义指针的话是不会开辟空间的.可以直接定义结构体
//定义结构体的同时就开辟好了空间,当需要在函数内对结构体的值进行修改时,传递地址即可
LED_TypeDef LED1;
/*-----------------------------Function---------------------------------------*/
/** * @brief LED结构体指针初始化 * @param[in] None * @return None * @details 使用时手动修改要赋值的指针与其对应值 */
void LED_Init(void)
{
//初始化LED对应的GPIO组号与引脚序号
LED1.led_port = LED1_PORT;
LED1.led_port = LED1_PIN;
}
/** * @brief 点亮LED * @param[in] LED 要操作的LED结构体 * @return None * @details 因为不需要修改结构体内的值,只是读取,所以不用传递指针,只传递结构体即可 */
void LED_ON(LED_TypeDef LED)
{
//引脚置对应的位
HAL_GPIO_WritePin(LED.led_port, LED.led_pin, LED_STATE_ON);
}
/** * @brief 熄灭LED * @param[in] LED 要操作的LED结构体 * @return None * @details 因为不需要修改结构体内的值,只是读取,所以不用传递指针,只传递结构体即可 */
void LED_OFF(LED_TypeDef LED)
{
//引脚置对应的位
HAL_GPIO_WritePin(LED.led_port, LED.led_pin, LED_STATE_OFF);
}
/** * @brief LED闪烁 * @param[in] LED 要操作的LED结构体 * Delay LED闪烁的半周期(ms),即一次亮或暗持续的时间,一次亮+暗为一个闪烁周期。 * @return None * @attention 使用时若想达到LED持续闪烁的效果,需要把该函数放到死循环中 */
void LED_Blink(LED_TypeDef LED, uint32_t Delay)
{
LED_ON(LED);//点亮LED
HAL_Delay(Delay);//亮的状态持续Delay ms
LED_OFF(LED);//熄灭LED
HAL_Delay(Delay);//暗的状态持续Delay ms
}
/************************ (C) COPYRIGHT Etherealize*****END OF FILE***********/
边栏推荐
- 透过华为军团看科技之变(五):智慧园区
- Dickinson's soul chooses its companion
- 小程序中读取腾讯文档的表格数据
- Mysql database foundation: TCL transaction control language
- pytorch 筆記 torch.nn.BatchNorm1d
- Pandora IOT development board learning (HAL Library) - Experiment 1 running lantern (RGB) experiment (learning notes)
- Overview of currency
- 【Rust每周一库】num-bigint - 大整数
- ArcGIS Pro scripting tool (5) - delete duplicates after sorting
- 透過華為軍團看科技之變(五):智慧園區
猜你喜欢

The latest SCI impact factor release: the highest score of domestic journals is 46! Netizen: I understand if

Remember the experience of an internship. It is necessary to go to the pit (I)

Pytorch notes torch nn. BatchNorm1d

ArcGIS Pro scripting tool (6) -- repairing CAD layer data sources

时间复杂度与空间复杂度

Mysql database foundation: views and variables

Rejuvenated Dell and apple hit each other, and the two old PC enterprises declined rapidly

国产自研系统的用户突破4亿,打破美国企业的垄断,谷歌后悔不迭

CP2112使用USB转IIC通信教学示例

pytorch 筆記 torch.nn.BatchNorm1d
随机推荐
R语言plotly可视化:使用plotly可视化多分类模型的预测置信度、模型在2D网格中每个数据点预测的置信度、置信度定义为在某一点上最高分与其他类别得分之和之间的差值
Circuit breaker hystrixcircuitbreaker
国产自研系统的用户突破4亿,打破美国企业的垄断,谷歌后悔不迭
My in-depth remote office experience | community essay solicitation
matplotlib 笔记: contourf & contour
The latest SCI impact factor release: the highest score of domestic journals is 46! Netizen: I understand if
js常见问题
pytorch 笔记 torch.nn.BatchNorm1d
R language plot visualization: use plot to visualize the prediction confidence of the multi classification model, the prediction confidence of each data point of the model in the 2D grid, and the conf
Criu enables hot migration
R语言aov函数进行重复测量方差分析(Repeated measures ANOVA、其中一个组内因素和一个组间因素)、分别使用interaction.plot函数和boxplot对交互作用进行可视化
mysql数据库基础:TCL事务控制语言
今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
[proteus simulation] Arduino uno led simulated traffic light
ArcGIS Pro scripting tool (5) - delete duplicates after sorting
透過華為軍團看科技之變(五):智慧園區
Review of mathematical knowledge: curve integral of the second type
59 websites programmers need to know
Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)
腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题