跟猫一起酸汤面系列:第48天 驱动段式LCD(HT1621)

先用Arduino尝试驱动,由于是5V IO,似乎有些黑影,后来换STM32显示效果挺好的
通过Arduino库中的说明知道源码的地址
https://github.com/5N44P/ht1621-7-seg
通过阅读源码,基本了解HT1621的驱动方式,首先自己建立了一个Arduino文件

#define  BIAS     0x52             //0b1000 0101 0010  1/3duty 4com
#define  RC256    0X30             //0b1000 0011 0000  内部时钟
#define  SYSDIS   0X00             //0b1000 0000 0000  关振系统荡器和LCD偏压发生器
#define  WDTDIS1  0X0A             //0b1000 0000 1010  禁止看门狗
#define  SYSEN    0X02             //0b1000 0000 0010 打开系统振荡器
#define  LCDON    0X06             //0b1000 0000 0110  打开LCD偏压

#define  LCDOFF   0X04             //0b1000 0000 0100  关LCD偏压
#define  XTAL     0x28             //0b1000 0010 1000 外部接时钟
#define  TONEON   0X12             //0b1000 0001 0010  打开声音输出
#define  TONEOFF  0X10             //0b1000 0001 0000 关闭声音输出
#define  BUFFERSIZE 4
int _cs_p;
int _wr_p;
int _data_p;

char _buffer[BUFFERSIZE];
  
void wrDATA(unsigned char data, unsigned char cnt) {
  unsigned char i;
  for (i = 0; i < cnt; i++) {
    digitalWrite(_wr_p, LOW);
    delayMicroseconds(4);
    if (data & 0x80) {
      digitalWrite(_data_p, HIGH);
    }
    else
    {
      digitalWrite(_data_p, LOW);
    }
    digitalWrite(_wr_p, HIGH);
    delayMicroseconds(4);
    data <<= 1;
  }
}

void wrCMD(unsigned char CMD) {
  digitalWrite(_cs_p, LOW);
  wrDATA(0x80, 4);
  wrDATA(CMD, 8);
  digitalWrite(_cs_p, HIGH);
}

void config()
{
  wrCMD(BIAS);
  wrCMD(RC256);
  wrCMD(SYSDIS);
  wrCMD(WDTDIS1);
  wrCMD(SYSEN);
  wrCMD(LCDON);
}

void wrclrdata(unsigned char addr, unsigned char sdata)
{
  addr <<= 2;
  digitalWrite(_cs_p, LOW);
  wrDATA(0xa0, 3);
  wrDATA(addr, 6);
  wrDATA(sdata, 8);
  digitalWrite(_cs_p, HIGH);
}

void wrCLR(unsigned char len) {
  unsigned char addr = 0;
  unsigned char i;
  for (i = 0; i < len; i++) {
    wrclrdata(addr, 0x00);
    addr = addr + 2;
  }
}

void clear(){
 wrCLR(16);
}

void wrone(unsigned char addr, unsigned char sdata)
{
  addr <<= 2;
  digitalWrite(_cs_p, LOW);
  wrDATA(0xa0, 3);
  wrDATA(addr, 6);
  wrDATA(sdata, 8);
  digitalWrite(_cs_p, HIGH);
}

void update(){ // takes the buffer and puts it straight into the driver
    // the buffer is backwards with respect to the lcd. could be improved
    wrone(0, _buffer[0]);
    //wrone(2, _buffer[1]);
    //wrone(4, _buffer[2]);
    wrone(6, _buffer[3]);
}
void setup() {
  // put your setup code here, to run once:
  _cs_p=13;
  _wr_p=12;
  _data_p=8;
  
  _buffer[0]=0x8f;
  _buffer[1]=0x1a;
  _buffer[2]=0x4e;
  _buffer[3]=0xad;

  
  pinMode(_cs_p, OUTPUT);
  pinMode(_wr_p, OUTPUT);
  pinMode(_data_p, OUTPUT);
  config();
  //clear();
  delay(1);
  update();
  delay(1);
  pinMode(_cs_p, INPUT);
  pinMode(_wr_p, INPUT);
  pinMode(_data_p, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
}

然后翻译成了STM32,基本变化不大

#define  BIAS     0x52
#define  RC256    0X30
#define  SYSDIS   0X00
#define  WDTDIS1  0X0A
#define  SYSEN    0X02
#define  LCDON    0X06

#define  LCDOFF   0X04
#define  XTAL     0x28
#define  TONEON   0X12
#define  TONEOFF  0X10
#define  BUFFERSIZE 4

#define LED_OFF()                            HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET)
#define LED_ON()                             HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET)
#define CS_LOW()                             HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET)
#define CS_HIGH()                            HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET)
#define WR_LOW()                             HAL_GPIO_WritePin(WR_GPIO_Port, WR_Pin, GPIO_PIN_RESET)
#define WR_HIGH()                            HAL_GPIO_WritePin(WR_GPIO_Port, WR_Pin, GPIO_PIN_SET)
#define DA_LOW()                             HAL_GPIO_WritePin(DA_GPIO_Port, DA_Pin, GPIO_PIN_RESET)
#define DA_HIGH()                            HAL_GPIO_WritePin(DA_GPIO_Port, DA_Pin, GPIO_PIN_SET)

char _buffer[BUFFERSIZE];

void wrDATA(unsigned char data, unsigned char cnt) {
  unsigned char i;
  for (i = 0; i < cnt; i++) {
    WR_LOW();
    //delayMicroseconds(4);
    //HAL_Delay(1);
    if (data & 0x80) {
      DA_HIGH();
    }
    else
    {
      DA_LOW();
    }
    WR_HIGH();
    //delayMicroseconds(4);
    //HAL_Delay(1);
    data <<= 1;
  }
}

void wrCMD(unsigned char CMD) {
  CS_LOW();
  wrDATA(0x80, 4);
  wrDATA(CMD, 8);
  CS_HIGH();
}

void config()
{
  wrCMD(BIAS);
  wrCMD(RC256);
  wrCMD(SYSDIS);
  wrCMD(WDTDIS1);
  wrCMD(SYSEN);
  wrCMD(LCDON);
}

void wrclrdata(unsigned char addr, unsigned char sdata)
{
  addr <<= 2;
  CS_LOW();
  wrDATA(0xa0, 3);
  wrDATA(addr, 6);
  wrDATA(sdata, 8);
  CS_HIGH();
}

void wrCLR(unsigned char len) {
  unsigned char addr = 0;
  unsigned char i;
  for (i = 0; i < len; i++) {
    wrclrdata(addr, 0x00);
    addr = addr + 2;
  }
}

void clear(){
 wrCLR(16);
}

void wrone(unsigned char addr, unsigned char sdata)
{
  addr <<= 2;
  CS_LOW();
  wrDATA(0xa0, 3);
  wrDATA(addr, 6);
  wrDATA(sdata, 8);
  CS_HIGH();
}

void update(){
    wrone(0, _buffer[0]);
    wrone(2, _buffer[1]);
    wrone(4, _buffer[2]);
    wrone(6, _buffer[3]);
}

void print(uint16_t num) {
    
}
void setup() {
  _buffer[0]=0x8f;
  _buffer[1]=0x1a;
  _buffer[2]=0x4e;
  _buffer[3]=0xad;

  config();
  clear();
  HAL_Delay(1);
  update();
  HAL_Delay(1);
}

启动写数据时的两个延时还是比较明显的,去掉问题也不大,下一步要解决字模问题

标签: none

添加新评论