今天的內容是LCD顯示,稍微有點複雜,所以拖更研究的時間有一丟丟久。接下來,一起來看實現的效果吧!
請看視頻
實物接線圖
12864系列LCD的接線如下(背光引腳接電源正負極,PSB引腳接地,表示選擇串行通信):
LabVIEW程序框圖
LabVIEW程序依舊是使用串口通信與Arduino「對話」,但是這次沒有用LINX工具包,而是用自己編寫的簡單串口通信程序:
第1部分 For循環初始化得到一個不斷遞增1字符的字符串數組;以此為基礎實現字符串動態遞增顯示的效果;
第2部分 初始化串口,默認波特率為9600,與Arduino一致;
第3部分 將字符串數組通過for循環索引,每隔0.1s通過串口發送相應的數據給Arduino開發板;
第4部分Arduino接收到LabVIEW發送下來的34個字節的字符串數據(包含2個字節的數據包頭+32個字節的數據),在LCD上顯示出來後,將接收到的32個字節的數據返回給LabVIEW;
第5部分 退出循環後關閉串口。
Arduino原始碼
向上滑動查看Arduino原始碼
#define LCD_RS 2
#define LCD_RW 3
#define LCD_E 4
#define LCD_REST 5
unsigned char Receptions[32] = {};
void setup() {
for (char i = 2; i <= 5; i++)
{
pinMode(i, OUTPUT);
}
Serial.begin(9600);
LCDInit(LCD_REST, LCD_RS, LCD_RW, LCD_E);
}
void loop() {
if (Serial.available() > 2)
{
if (Serial.read() == 0x55)
{
if (Serial.read() == 0xAA)
{
for (char i = 0; i <= 31; i++)
{
delay(1);
Receptions[i] = Serial.read();
}
ShowWord(0x80, Receptions, 16, LCD_RS, LCD_RW, LCD_E);
Serial.write(Receptions, 32);
}
}
}
}
void SendByteLCD(unsigned char WLCDData, char RW, char E)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
if ((WLCDData << i) & 0x80)digitalWrite(RW, 1);
else digitalWrite(RW, 0);
digitalWrite(E, 0);
digitalWrite(E, 1);
}
}
void SPIWR(unsigned char Wdata, unsigned char WRS, char RW, char E)
{
SendByteLCD(0xf8 + (WRS << 1), RW, E);
SendByteLCD(Wdata & 0xf0, RW, E);
SendByteLCD((Wdata << 4) & 0xf0, RW, E);
}
void WRCommand(unsigned char CMD, char RS, char RW, char E)
{
digitalWrite(RS, 0);
digitalWrite(RS, 1);
SPIWR(CMD, 0, RW, E);
delay(1);
}
void WRData(unsigned char Data, char RS, char RW, char E)
{
digitalWrite(RS, 0);
digitalWrite(RS, 1);
SPIWR(Data, 1, RW, E);
}
void LCDInit(char REST, char RS, char RW, char E)
{
digitalWrite(REST, 1);
digitalWrite(REST, 0);
digitalWrite(REST, 1);
WRCommand(0x06, RS, RW, E); //啟始點設定:光標右移
WRCommand(0x01, RS, RW, E); //清除顯示 DDRAM
WRCommand(0x0C, RS, RW, E); //顯示狀態開關:整體顯示開,光標顯示關,光標顯示反白關
WRCommand(0x02, RS, RW, E); //地址歸零
}
void ShowWord(unsigned char addr, unsigned char *english, unsigned char count, char RS, char RW, char E)
{
unsigned char i;
WRCommand(addr, RS, RW, E); //設定 DDRAM 地址
for (i = 0; i < count;)
{
WRData(english[i * 2], RS, RW, E);
WRData(english[i * 2 + 1], RS, RW, E);
i++;
}
}
礙於篇幅,Arduino源碼這裡先不進行講解,後面再詳細講解Labview與Arduino串口通信的具體實現。
以上就是今天的內容~最後,溫馨提示:距離下次放假還有5天!!!加油了呀~