ST7920算是蠻常見的Arduino顯示LCD了,很多小型的機器都有它的身影,主要是因為它的尺寸夠大,還能內建中文,不過有分繁體和簡體,千萬別買錯了!
ST7920可以用的函式庫,基本上有兩款,一款是大大有名的u8g2,一款是輕巧型的LCD12864RSPI。u8g2雖然強大,但它無法使用ST7920的內建字庫;LCD12864RSPI雖然很陽春,只能顯示固定尺寸的文字,圖片也只能單張全畫面,但勝在簡單易用,我們先來試試這個函式庫吧!
首先請先下載LCD12864RSPI函式庫,並解壓縮到電腦中Arduino的Documents\Arduino\libraries目錄裡。
下載網址:
http://jmaker.banner.tw/doc/LCD12864RSPI.zip
線比I2C的LCD多一些,請依以下的指示接線:
- VSS=>GND
- VDD=>5V
- RS=>D8
- R/W=>D9
- E=>D3
- PSB=>GND
- BLA=>3.3V
- BLK=>GND
因為函式庫很單純,所以直接用它的範例LCD12864_test來修改。
#include "LCD12864RSPI.h"
#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
unsigned char show0[]= {0xB3,0xC7,0xB4,0xCB,0xB3,0xD0,0xA4,0x75}; //傑森創工的BIG5編碼
unsigned char show1[]="jmaker.com.tw";
unsigned char messageString[]={
0xB3, 0xD0,
0xAB, 0xC8,
0xAA, 0xBA,
0xA6, 0x6E,
0xAA, 0x42,
0xA4, 0xCD
};
//圖檔,必須是128x64,並且在繪圖軟體就要做成單色BMP,才能進Image2LCD轉檔,不然會顯示失敗!
unsigned char jmaker_bitmap[] = {
0X00,0X00,0X00,0X00,0X00,0X00,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X00,0X00,0X00,0X00,0X00,0X00,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X00,0X00,0X00,0X07,0X00,0X00,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X00,0X00,0X00,0X0F,0X80,0X00,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0X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00,0X07,0XFF,0XC6,0X01,0XC0,0XF0,0X00,0X00,0X7F,0XFF,
0X7F,0XC3,0XFF,0XFC,0X00,0X00,0X07,0XFF,0XE6,0X00,0XC0,0XFF,0XFF,0XFF,0XFF,0XFF,
0X7F,0XFB,0XFF,0XF8,0X00,0X00,0X07,0XFF,0XFE,0X31,0XE1,0XFF,0XFF,0XFF,0XFF,0XFF,
0X7F,0XFF,0XFF,0XF8,0X00,0X00,0X07,0XFF,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X7F,0XFF,0XFE,0XF0,0X00,0X00,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X7F,0XFF,0XFC,0X70,0X00,0X00,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X00,0X00,0X00,0X00,0X00,0X00,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
};
void setup()
{
LCDA.Initialise(); // 初始化
delay(100);
LCDA.DrawFullScreen(jmaker_bitmap); //顯示全畫面圖片
delay(2000);
LCDA.CLEAR();//清除畫面
delay(100); //這個延遲是必要的,不然下一個指令會有問題。
LCDA.DisplayString(0,2,show0,AR_SIZE(show0));//第一行第三格開始顯示文字
delay(100);
LCDA.DisplayString(1,1,messageString,AR_SIZE(messageString));//第二行第二格開始顯示文字
delay(100);
LCDA.DisplayString(3,1,show1,AR_SIZE(show1));//第三行第二格開始,显示傑森創工的網址。
delay(5000);
}
void loop()
{
}
其實只有兩個主要的函式:
- LCDA.DrawFullScreen(),顯示全螢幕圖片,所以圖片必須是128x64
- LCDA.DisplayString(),顯示文字。
顯示英文很簡單,直接把字串放進DisplayString()裡就行了!
LCDA.DisplayString(3,1,"jmaker.com.tw",13);
這行指的是從第四行第二格開始顯示文字,而文字的長度是13。
如果是中文就要小麻煩一下,先把你要顯示的中文,轉成Big5碼,網路上有不少線上服務,大家可以找找!
線上中文轉Big5碼:
http://billor.chsh.chc.edu.tw/php/Tools/qBig5.php
以傑字來說,Big5碼是B3C7,必須把它拆成 0xB3,0xC7 。所以這行就是把「傑森創工」Big5碼寫成了陣列:
unsigned char show0[]= {0xB3,0xC7,0xB4,0xCB,0xB3,0xD0,0xA4,0x75};
要顯示它很簡單:
LCDA.DisplayString(0,2,show0,AR_SIZE(show0));
第一行第三格開始顯示文字,文字長度用算的就行了。
至於圖片,首先到繪圖軟體先畫面,以Photoshop為例,記要做成單色BMP,才能進Image2LCD轉檔,不然會顯示失敗!
LCDA.DrawFullScreen(jmaker_bitmap);
LCD12864RSPI函式庫處理圖片很簡單,就是畫一張全版的圖片,尺寸就是128x64,好處就是簡單啦!
如果要部份地址是圖片,部份是文字呢?很簡單,製作圖片時留下空白,圖片先放上LCD,再把文字寫到空白的地方就可以囉!
如果你覺得LCD12864RSPI函式庫太陽春了,傑森會在之後介紹u8g2用在ST7920上,變化會多一些,請期待喔!