单片机为HC32L13X,Cortex-M puls内核
Ht1621m.h
#ifndef _HT1621M_HPP_
#define _HT1621M_HPP_
#define HT1621M_RAM_SIZE 16
struct _ht1621m_
{
uint8_t _port_cs;
uint8_t _port_wr;
uint8_t _port_dat;
uint8_t _pin_cs;
uint8_t _pin_wr;
uint8_t _pin_dat;
uint8_t _ram_last[HT1621M_RAM_SIZE];
uint8_t _ram[HT1621M_RAM_SIZE];
};
extern struct _ht1621m_ sg_ht1621m;
#define GET_HT1621M() (&sg_ht1621m)
#define GET_HT1621M_RAM(ht1621m) ((ht1621m)->_ram)
boolean_t ht1621m_init(struct _ht1621m_* ht1621m, uint8_t port_cs, uint8_t pin_cs, uint8_t port_wr, uint8_t pin_wr, uint8_t port_dat, uint8_t pin_dat);
boolean_t ht1621m_update(struct _ht1621m_* ht1621m);
#endif
Ht1621m.c
#include "BaseDef.h"
#include "Ht1621m.h"
#include "gpio.h"
#include <string.h>
struct _ht1621m_ sg_ht1621m;
#define BIAS 0x29 // 0x50// 1/3 bias 4 com
#define SYSEN 0x01 // 0X02//Turn on system oscillator振荡
#define LCDON 0x03 // 0x06//Turn on LCD bias generator偏压发生器
#define LCDOFF 0x02 // 0x04//Turn off LCD bias generator
#define RC256 0X18
#define HT1621M_CTRL_CS(ht1621m, stat) Gpio_WriteOutputIO((en_gpio_port_t)(ht1621m)->_port_cs, (en_gpio_pin_t)(ht1621m)->_pin_cs, stat) // 控制cs:true:拉高,false:拉低
#define HT1621M_CTRL_WR(ht1621m, stat) Gpio_WriteOutputIO((en_gpio_port_t)(ht1621m)->_port_wr, (en_gpio_pin_t)(ht1621m)->_pin_wr, stat) // 控制wr:true:拉高,false:拉低
#define HT1621M_CTRL_DAT(ht1621m, stat) Gpio_WriteOutputIO((en_gpio_port_t)(ht1621m)->_port_dat, (en_gpio_pin_t)(ht1621m)->_pin_dat, stat) // 控制dat:true:拉高,false:拉低
// 传地址,高六位
void _ht1621m_sendbit_high(struct _ht1621m_* ht1621m, uint8_t data, uint8_t cnt)
{
uint8_t i;
for (i = 0; i < cnt; i++)
{
HT1621M_CTRL_DAT(ht1621m, data & 0x80);
HT1621M_CTRL_WR(ht1621m, FALSE);
delay10us(1);
HT1621M_CTRL_WR(ht1621m, TRUE);
data <<= 1;
delay10us(1);
}
}
// 传送数据,低四位
void _ht1621m_sendbit_low(struct _ht1621m_* ht1621m, uint8_t data, uint8_t cnt)
{
uint8_t i;
for (i = 0; i < cnt; i++)
{
HT1621M_CTRL_DAT(ht1621m, data & 0x01);
HT1621M_CTRL_WR(ht1621m, FALSE);
delay10us(1);
HT1621M_CTRL_WR(ht1621m, TRUE);
data >>= 1;
delay10us(1);
}
}
// 写命令
void _ht1621m_send_cmd(struct _ht1621m_* ht1621m, uint8_t command)
{
HT1621M_CTRL_CS(ht1621m, FALSE);
_ht1621m_sendbit_high(ht1621m, 0x80, 3);
_ht1621m_sendbit_high(ht1621m, command, 8);
_ht1621m_sendbit_high(ht1621m, 0x0, 1);
HT1621M_CTRL_CS(ht1621m, TRUE);
}
// 写地址和数据
void _ht1621m_write_byte(struct _ht1621m_* ht1621m, uint8_t addr, uint8_t data)
{
HT1621M_CTRL_CS(ht1621m, FALSE);
_ht1621m_sendbit_high(ht1621m, 0xa0, 3);
_ht1621m_sendbit_high(ht1621m, addr << 3, 6);
_ht1621m_sendbit_low(ht1621m, data, 8);
HT1621M_CTRL_CS(ht1621m, TRUE);
}
// 初始化
boolean_t ht1621m_init(struct _ht1621m_* ht1621m, uint8_t port_cs, uint8_t pin_cs, uint8_t port_wr, uint8_t pin_wr, uint8_t port_dat, uint8_t pin_dat)
{
uint8_t i;
ht1621m->_port_cs = port_cs;
ht1621m->_pin_cs = pin_cs;
ht1621m->_port_wr = port_wr;
ht1621m->_pin_wr = pin_wr;
ht1621m->_port_dat = port_dat;
ht1621m->_pin_dat = pin_dat;
_ht1621m_send_cmd(ht1621m, RC256);
_ht1621m_send_cmd(ht1621m, BIAS);
_ht1621m_send_cmd(ht1621m, SYSEN);
_ht1621m_send_cmd(ht1621m, LCDON);
memset(ht1621m->_ram, 0X00, HT1621M_RAM_SIZE);
for (i = 0; i < HT1621M_RAM_SIZE; ++i)
{
_ht1621m_write_byte(ht1621m, i, ht1621m->_ram[i]);
}
return TRUE;
}
// 更新RAM
boolean_t ht1621m_update(struct _ht1621m_* ht1621m)
{
uint8_t i;
if (0==memcmp(ht1621m->_ram_last, ht1621m->_ram, HT1621M_RAM_SIZE))
{
return lc_false;
}
memcpy(ht1621m->_ram_last, ht1621m->_ram, HT1621M_RAM_SIZE);
for (i=0; i<HT1621M_RAM_SIZE; ++i)
{
_ht1621m_write_byte(ht1621m, i, ht1621m->_ram[i]);
}
return TRUE;
}