智能家居入门DIY——【七、添加一个LCD12864吧】

        今天加了一个LCD12864,IC看说明上是ST7567,结果一顿U8g2,发现两个问题:

1、买的时候不知道是卖家写的我理解错了还是怎么了,反正是不出汉字的。

2、U8g2太大了…………占了uno的80%还多

        然后,破罐子破摔吧,换成U8glib小了很多;汉字么,用图形表示好了,看了一下U8glib的范例,发现可以用二进制来表示黑白点,然后弄一小工具,读16×16点阵,转化为二进制就可以了:

const uint8_t wen[] PROGMEM = {
    0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
    0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,
};

这是温字,加PROGMEN关键字就不占运行内存了,所以这样的字可以放一些,当然会使得程序占用的空间大一点,但好处是有针对性,一般我们也用不了几个字不是。

3、用那个声明?

        U8glib这个库在用的时候需要把它注释掉的若干行u8g定义中的一行注释掉,而名字也很直观,但是无奈的是我确实没找到我这个的声明,所以只好找一个相似的咯,几番实验和观察代码下来,发现主要是位置不同,所以选了一个库就这样用起来就可以了:

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8

4、“英汉混合显示”

         其实说白了,无论什么东西显示的时候都是设置点的颜色而已。所以处理的时候都会和上面所说的图形一样。至于我们写代码么,只要算好偏移,分别用两种方法显示:汉字用图形,其他英文字母数字字符什么的还是用原来的方法,原来的方法即直观又高效:

void draw(void) {
    // graphic commands to redraw the complete screen should be placed here  
    u8g.drawBitmapP(0, 0, 2, 16, wen);
    u8g.drawBitmapP(16, 0, 2, 16, du);
    u8g.setFont(u8g_font_unifont);
    u8g.drawStr(32, 14, ": 33.70");
    u8g.drawBitmapP(96, 0, 2, 16, sheshidu);

    u8g.drawBitmapP(0, 16, 2, 16, shi);
    u8g.drawBitmapP(16, 16, 2, 16, du);
    u8g.setFont(u8g_font_unifont);
    u8g.drawStr(32, 30, ": 33.70 %RH");
    
}

5、全部代码:

#include <U8glib.h>

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8

const uint8_t wen[] PROGMEM = {
    0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
    0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,
};
const uint8_t du[] PROGMEM = {
    0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
    0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,
};
const uint8_t sheshidu[] PROGMEM{
    0x60,0x00,0x91,0xF4,0x96,0x0C,0x6C,0x04,0x08,0x04,0x18,0x00,0x18,0x00,0x18,0x00,
    0x18,0x00,0x18,0x00,0x18,0x00,0x08,0x00,0x0C,0x04,0x06,0x08,0x01,0xF0,0x00,0x00,
};
const uint8_t shi[] PROGMEM{
    0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
    0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00,
};

void draw(void) {
    // graphic commands to redraw the complete screen should be placed here  
    u8g.drawBitmapP(0, 0, 2, 16, wen);
    u8g.drawBitmapP(16, 0, 2, 16, du);
    u8g.setFont(u8g_font_unifont);
    u8g.drawStr(32, 14, ": 33.70");
    u8g.drawBitmapP(96, 0, 2, 16, sheshidu);

    u8g.drawBitmapP(0, 16, 2, 16, shi);
    u8g.drawBitmapP(16, 16, 2, 16, du);
    u8g.setFont(u8g_font_unifont);
    u8g.drawStr(32, 30, ": 33.70 %RH");
    
}

void setup(void) {
}

void loop(void) {
    // picture loop
    u8g.firstPage();
    do {
        draw();
    } while (u8g.nextPage());

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

 

posted @ 2018-10-28 21:18 zcsor~流浪dè风 Views(...) Comments(...) Edit 收藏