中文 字库 的8针 OLED 12864模块 arduino-Arduino中文社区 - Powered by Discuz! Archiver

plasticplant 发表于 2019-12-18 16:50

留个足迹,谢谢分享

bigbrothershing 发表于 2019-12-19 12:27

thankyou for sharing

bigbrothershing 发表于 2019-12-19 12:48

#include <SPI.h>
#include "U8glib.h"
unsigned char name0 = {0xC4, 0xE3, 0xBA, 0xC3, 0xCA, 0xC0, 0xBD, 0xE7};
unsigned char name1 = {0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x2C, 0x57, 0x4F, 0x52, 0x4C, 0x44, 0x21};

/*===================================================================
以下函数由arduinoCN坛友NoComment提供表示感谢
这是他的github:https://github.com/XAS-712/gt20l16s1y
===================================================================*/
U8GLIB_SSD1306_ADAFRUIT_128X64 u8g(10, 9);    // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins areSCK = 13 and MOSI = 11) //[重要]注意u8g的配置要与以下4行匹配 必须要保证设备名为u8g

#define CS 8//字库数据输出
#define SO 12 //字库片选
#define SI 11 //MOSI(数据线)
#define CL 13 //CLK(时钟)
long getAddr(uint16_t str)
{ byte hb = (byte)((str & 0xff00) >> 8);
byte lb = (byte)(str & 0x00ff);
Serial.print("HB ");
Serial.print(hb, HEX);
Serial.print(" LB ");
Serial.println(lb, HEX);
long Address;
if (hb > 0xA0 && lb > 0xA0)//is GB2312
{ long BaseAdd = 0;
    if (hb == 0xA9 && lb >= 0xA1)
    { Address = (282 + (lb - 0xA1 ));//8位机在此出现了溢出,所以分三步
      Address = Address * 32;
      Address += BaseAdd;
    }
    else if (hb >= 0xA1 && hb <= 0xA3 && lb >= 0xA1)
    { Address = ((hb - 0xA1) * 94 + (lb - 0xA1));
      Address = Address * 32;
      Address += BaseAdd;
    }
    else if (hb >= 0xB0 && hb <= 0xF7 && lb >= 0xA1)
    { Address = ((hb - 0xB0) * 94 + (lb - 0xA1) + 846);
      Address = Address * 32;
      Address += BaseAdd;
    }
} else {//is ASCII
    long BaseAdd = 0x03b7c0;
    if (lb >= 0x20 && lb <= 0x7E)
    { Address = (lb - 0x20 ) * 16 + BaseAdd;
    }
}
return Address;
}

byte revBit(byte input)
{ byte output = 0;
for (int i = 0;i < 8;i++)
{ if (input & (1 << i))
      output |= 1 << (7 - i);
}
return output;
}

void fetchBitmap16(long address, byte *p)
{ byte hb = (byte)((address & 0x00ff0000) >> 16);
byte mb = (byte)((address & 0x0000ff00) >> 8);
byte lb = (byte)(address & 0x000000ff);
digitalWrite(CS, LOW);//选通字库
SPI.transfer(0x0b);//移入命令字
SPI.transfer(hb);//移入地址次高位
SPI.transfer(mb);//移入地址次低位
SPI.transfer(lb);//移入地址最低位
SPI.transfer(0x8d);//移入dummy byte
for (int i = 0; i < 16; i++)
{ byte buf = SPI.transfer(0x00); //读出16bytes
    p = revBit(buf);
}
digitalWrite(CS, HIGH);//反选通字库
}

void fetchBitmap32(long address, byte *p)
{ byte hb = (byte)((address & 0x00ff0000) >> 16);
byte mb = (byte)((address & 0x0000ff00) >> 8);
byte lb = (byte)(address & 0x000000ff);
digitalWrite(CS, LOW);//选通字库
SPI.transfer(0x0b);//移入命令字
SPI.transfer(hb);//移入地址次高位
SPI.transfer(mb);//移入地址次低位
SPI.transfer(lb);//移入地址最低位
SPI.transfer(0x8d);//移入dummy byte
for (int i = 0; i < 32; i++)
{ byte buf = SPI.transfer(0x00); //读出32bytes
    p = revBit(buf);
}
digitalWrite(CS, HIGH);//反选通字库
}

void drawV8P(int x, int y, byte b)
{ if ((b & 0x80) == 0x80)
    u8g.drawPixel(x, y);
if ((b & 0x40) == 0x40)
    u8g.drawPixel(x, y + 1);
if ((b & 0x20) == 0x20)
    u8g.drawPixel(x, y + 2);
if ((b & 0x10) == 0x10)
    u8g.drawPixel(x, y + 3);
if ((b & 0x08) == 0x08)
    u8g.drawPixel(x, y + 4);
if ((b & 0x04) == 0x04)
    u8g.drawPixel(x, y + 5);
if ((b & 0x02) == 0x02)
    u8g.drawPixel(x, y + 6);
if ((b & 0x01) == 0x01)
    u8g.drawPixel(x, y + 7);
}

void draw8x16(int x, int y, byte *b)
{ int i;
for (i = 0; i < 8; i++)
{ drawV8P(x + i, y, b);
}
for (i = 0; i < 8; i++)
{ drawV8P(x + i, y + 8, b);
}
}

void draw16x16(int x, int y, byte *b)
{ int i;
for (i = 0; i < 16; i++)
{ drawV8P(x + i, y, b);
}
for (i = 0; i < 16; i++)
{ drawV8P(x + i, y + 8, b);
}
}

void displayStr(int x, int y, int len, unsigned char *c)
{ int i;
for (i = 0; i < len; i++)
{ if (c > 0xA0) //is GB2312
    { int code = (int)(c * 256) + (int)c;
      byte bmp;
      fetchBitmap32(getAddr(code), bmp);
      draw16x16(x, y, bmp);
      x += 16;
      i++;
    } else
    { //is ASCII
      int code = (int)c;
      byte bmp;
      fetchBitmap16(getAddr(code), bmp);
      draw8x16(x, y, bmp);
      x += 8;
    }
}
}

void displayStrC(int row, int len, unsigned char *c)
{ int x = 64 - ((len * 8) / 2);
displayStr(x, row, len, c);
}

/*===================================================================
以上函数由arduinoCN坛友NoComment提供表示感谢
这是他的github:https://github.com/XAS-712/gt20l16s1y
===================================================================*/
void setup(void)
{ pinMode(8, OUTPUT); //字库数据输出
}

void loop(void)
{ // picture loop
u8g.firstPage();
do
{ displayStr(0, 0, 8, name0);//你好世界
    displayStr(0, 16, 12, name1);//HELLO,WORLD!
} while ( u8g.nextPage() );

// rebuild the picture after some delay
//delay(50);
}

yuxian166 发表于 2019-12-22 23:43

瞅一瞅看看咋样

nihao881021 发表于 2019-12-23 11:10

jhgnhgnbvngyfg

JM夏枯草 发表于 2019-12-25 12:32

感谢分享,找了好久了,顺便挖个坟

linsir 发表于 2019-12-25 18:16

233333棒棒

巨壳编程 发表于 2019-12-27 15:26

好东西,进来看看。

metoo 发表于 2020-1-10 08:05

感谢楼主分享

niuzi_kong 发表于 2020-1-11 08:54

谢谢分享
页: 1 2 3 4 5 6 7 8 9 [10] 11 12 13 14 15 16 17 18 19
查看完整版本: 中文 字库 的8针 OLED 12864模块 arduino