使用HC32L13X系列,Cortex-M0内核
LedDrv.h
#ifndef _LED_DRV_HPP_
#define _LED_DRV_HPP_
#include "gpio.h"
#define HLD_RAM_LEN 4 // ram的大小
extern uint8_t sg_hld_display_ram[HLD_RAM_LEN]; // ram
extern struct _hwr_led_drv_ sg_hld;
#define GET_LED_RAM() sg_hld_display_ram
#define GET_HLD() (&sg_hld)
struct _hwr_led_drv_
{
stc_gpio_config_t _gpio_cfg_float; // 浮空配置
stc_gpio_config_t _gpio_cfg_pu_pd; // 上下拉配置
uint8_t _tag; // 状态标记
uint16_t _flag;
lc_bool _is_led_light; // 依次点亮标记
};
extern void hld_init(struct _hwr_led_drv_* hld);
extern void hld_update(struct _hwr_led_drv_* hld);
// 1ms的计时操作
#define HLD_DELAY_1MS(hld) \
if (0<(hld)->_flag)\
{\
--(hld)->_flag;\
}
#endif
LedDrv.c
#include "BaseDef.h"
#include "LedDrv.h"
#include "base_types.h"
#include <string.h>
// 亮灭间隔为3ms:3ms
#define HLD_CFG_FLAG_ON 3
#define HLD_CFG_FLAG_OFF 3
// 管脚配置:上拉;下拉;浮空
enum _HL_GPIO_STAT_ {GPIO_PU=0, GPIO_PD, GPIO_FLOAT, GPIO_NUM};
// 数码管针脚选择
enum _HLD_TAG_ {HT_1=0, HT_2, HT_3, HT_4, HT_7, HT_NUM};
// 初始化RAM
uint8_t sg_hld_display_ram[HLD_RAM_LEN] = {0};
// 管脚的端口及脚位
static en_gpio_port_t s_gpio_port[7] = {GpioPortx, GpioPortx, GpioPortx, GpioPortx, GpioPortx, GpioPortx, GpioPortx};
static en_gpio_pin_t s_gpio_pin[7] = {GpioPinx, GpioPinx, GpioPinx, GpioPinx, GpioPinx, GpioPinx, GpioPinx};
struct _hwr_led_drv_ sg_hld;
// 数码管初始化,将上下拉和浮空分别存储
void hld_init(struct _hwr_led_drv_* hld)
{
hld->_tag = HT_1;
hld->_flag = 0;
hld->_is_led_light = lc_true;
hld->_gpio_cfg_float.enDir = GpioDirIn; //< 端口方向配置->输入
hld->_gpio_cfg_float.enCtrlMode = GpioAHB; //< 端口输入/输出值寄存器总线控制模式配置->AHB
hld->_gpio_cfg_pu_pd.enDir = GpioDirOut; //< 端口方向配置->输出
hld->_gpio_cfg_pu_pd.enDrv = GpioDrvH; //< 端口驱动能力配置->高驱动能力
hld->_gpio_cfg_pu_pd.enPuPd = GpioNoPuPd; //< 端口上下拉配置->无上下拉
hld->_gpio_cfg_pu_pd.enOD = GpioOdDisable; //< 端口开漏输出配置->开漏输出关闭
hld->_gpio_cfg_pu_pd.enCtrlMode = GpioAHB; //< 端口输入/输出值寄存器总线控制模式配置->AHB
}
// 脚位控制:选择输出上下拉还是浮空
// no: pin编号;stat:状态
void _hld_ctrl_pin(struct _hwr_led_drv_* hld, uint8_t no, uint8_t stat)
{
if (GPIO_PU==stat)
{
Gpio_Init(s_gpio_port[no], s_gpio_pin[no], &hld->_gpio_cfg_pu_pd);
Gpio_WriteOutputIO(s_gpio_port[no], s_gpio_pin[no], TRUE);
} else if (GPIO_PD==stat)
{
Gpio_Init(s_gpio_port[no], s_gpio_pin[no], &hld->_gpio_cfg_pu_pd);
Gpio_WriteOutputIO(s_gpio_port[no], s_gpio_pin[no], FALSE);
} else if (GPIO_FLOAT==stat)
{
Gpio_Init(s_gpio_port[no], s_gpio_pin[no], &hld->_gpio_cfg_float);
}
}
// 更新RAM
void hld_update(struct _hwr_led_drv_* hld)
{
if (0<hld->_flag)
{
return;
}
hld->_is_led_light = !hld->_is_led_light;
hld->_flag = hld->_is_led_light ? HLD_CFG_FLAG_ON : HLD_CFG_FLAG_OFF;
switch (hld->_tag)
{
case HT_1:
{
_hld_ctrl_pin(hld, 0, GPIO_PU);
_hld_ctrl_pin(hld, 1, (sg_hld_display_ram[0]&0X01) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 2, (sg_hld_display_ram[0]&0X02) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 3, (sg_hld_display_ram[0]&0X04) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 4, (sg_hld_display_ram[0]&0X08) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 5, (sg_hld_display_ram[0]&0X10) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 6, (sg_hld_display_ram[0]&0X20) ? GPIO_PD : GPIO_FLOAT);
break;
}
case HT_2:
{
_hld_ctrl_pin(hld, 1, GPIO_PU);
_hld_ctrl_pin(hld, 0, (sg_hld_display_ram[0]&0X40) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 2, (sg_hld_display_ram[0]&0X80) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 3, (sg_hld_display_ram[1]&0X01) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 4, (sg_hld_display_ram[1]&0X02) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 5, (sg_hld_display_ram[1]&0X04) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 6, (sg_hld_display_ram[1]&0X08) ? GPIO_PD : GPIO_FLOAT);
break;
}
case HT_3:
{
_hld_ctrl_pin(hld, 2, GPIO_PU);
_hld_ctrl_pin(hld, 0, (sg_hld_display_ram[1]&0X10) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 1, (sg_hld_display_ram[1]&0X20) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 3, (sg_hld_display_ram[1]&0X40) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 4, (sg_hld_display_ram[1]&0X80) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 5, (sg_hld_display_ram[2]&0X01) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 6, (sg_hld_display_ram[2]&0X02) ? GPIO_PD : GPIO_FLOAT);
break;
}
case HT_4:
{
_hld_ctrl_pin(hld, 3, GPIO_PU);
_hld_ctrl_pin(hld, 0, (sg_hld_display_ram[2]&0X04) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 1, (sg_hld_display_ram[2]&0X08) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 2, (sg_hld_display_ram[2]&0X10) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 4, (sg_hld_display_ram[2]&0X20) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 5, (sg_hld_display_ram[2]&0X40) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 6, (sg_hld_display_ram[2]&0X80) ? GPIO_PD : GPIO_FLOAT);
break;
}
case HT_7:
{
_hld_ctrl_pin(hld, 6, GPIO_PU);
_hld_ctrl_pin(hld, 0, (sg_hld_display_ram[3]&0X01) ? GPIO_PD : GPIO_FLOAT);
_hld_ctrl_pin(hld, 3, GPIO_FLOAT);
break;
}
}
hld->_tag = (++hld->_tag)%HT_NUM;
}