D.A.Godse A.P.Godse
33 books written by D.A.Godse A.P.Godse
一.種類:可分為
1.文字型:已經有內建文字圖形(通常只有英文字母大小寫、阿拉伯數字、標點符號),
只要輸入對應的字形碼(ASCII code),LCD便會將該字的圖形顯示於LCD,可參考課
本第三篇第三章。
2.繪圖型:只能用繪圖的方式將資料顯示於LCD,所以必須先將要顯示文字的圖形依LCD所
需的格式事先存起來,如一個16x15的中文字便需儲存30byte的資料,將此30byte的資
料依序填入LCD即可顯示對應文字圖形,可參考課本第四篇第二章。
3.文字繪圖兩用型:可依使用者需要切換成文字型或繪圖型。
在此我們只討論如何控制文字型LCD
二.原理
下圖為目前市面上常見的文字型LCD基本的方塊圖,
1
|
Vss
| 接地(0 V) |
2
|
Vdd
| 電源 (+5 V) |
3
|
Vo
| LCD亮度控制(0 V最亮) |
4
|
RS
| 暫存器選擇:1 資料 0 指令 |
5
|
R/W
| 讀/寫選擇:1 從LCD讀取資料
0 寫資料給LCD
|
6
|
E
| 致能LCD,負緣動作 |
7
|
D0
|
LCD 資料線
|
8
|
D1
| |
9
|
D2
| |
10
|
D3
| |
11
|
D4
| |
12
|
D5
| |
13
|
D6
| |
14
|
D7
|
RS
|
R/W
| 功 能 |
0
|
0
| 寫命令到LCD |
0
|
1
| 讀取忙碌旗號和位址計數器AC(記錄目前游標位址)內容 |
1
|
0
| 寫資料到DDRAM(要顯示的文字)或CGRAM(要造字的字形) |
1
|
1
| 從DDRAM或CGRAM讀取資料 |
在寫入狀態(R/W=0)時,當接腳E產生負緣訊號,LCD便將資料線D0~D7取入LCD處理,依照RS設定狀態,
決定此資料為控制命令(RS=0)或是要顯示或造字的資料。
在讀取狀態(R/W=1)時,當接腳E產生負緣訊號,LCD便依照RS設定狀態,將資料從 D0~D7送出來。
PS:8051如何產生負緣訊號給LCD接腳E?
SETB E
CLR E ;where E is the pin of 8051 which connect to LCD
在多數應用的場合中,我們常直接將接腳R/W接地,使其一直處於寫入狀態,這樣8051可節省一隻接腳去控
制它,但以後寫資料或命令給LCD時,需保留足夠的處理時間給LCD(因為無法判斷LCD是否已處理好,且用
讀取LCD忙碌旗號來判斷是否處理完的程式並不會比較好寫),另外游標實際所在的位置也需處理好。
若已知8051與LCD的接法如下圖所定義,將接腳R/W接地,使其處於寫入命令或資料的狀態,這樣在控制時,單晶片可節省
一支接腳,但須注意控制資料及命令寫入之後的延遲時間,寧可時間長一些,以避免LCD在未處裡完成前給予下一筆資料。
;定義接腳名稱
LCDBUS EQU P0 ;LCD位置
RS EQU P2.6
ENABLE EQU P2.7
;----- WRITE 資料 TO LCD --------------
WRDR: SETB RS ;RS=1 => 寫資料到LCD
SETB ENABLE ;E=1
MOV LCDBUS,A
MOV R7,#5 ;等待約10us等8051輸出電壓穩定
DJNZ R7,$
CLR ENABLE ;E=0 => 接腳E產生負緣訊號
MOV R7,#25 ;等待約50us等LCD將資料處理完畢
DJNZ R7,$
RET
;----- WRITE 命令 TO LCD --------------
WRIR: CLR RS ;RS=0 => 寫命令到LCD
SETB ENABLE
MOV LCDBUS,A
MOV R7,#5
DJNZ R7,$
CLR ENABLE
MOV R7,#25
DJNZ R7,$
RET
下表為有關LCD的控制命令表:(詳細說明請參考課本)
命令
|
指令編碼
| |||||||||
RS
|
R/W
|
DB7
|
DB6
|
DB5
|
DB4
|
DB3
|
DB2
|
DB1
|
DB0
| |
| 1.清除顯示器 |
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
| 2.游標歸位 |
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
X
|
| 3.進入模式設定 |
0
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
I/D
|
S
|
| 4.顯示器ON/Off控制 |
0
|
0
|
0
|
0
|
0
|
0
|
1
|
D
|
C
|
B
|
| 5.顯示器或游標移動 |
0
|
0
|
0
|
0
|
0
|
1
|
S/C
|
R/L
|
X
|
X
|
| 6.功能設定 |
0
|
0
|
0
|
0
|
1
|
DL
|
N
|
F
|
X
|
X
|
| 7.CGRAM 位址設定 |
0
|
0
|
0
|
1
|
AC5
|
AC4
|
AC3
|
AC2
|
AC1
|
AC0
|
| 8.DDRAM 位址設定 |
0
|
0
|
1
|
AD6
|
AD5
|
AD4
|
AD3
|
AD2
|
AD1
|
AD0
|
1.清除顯示器
功能:LCD畫面清除,LCD位址計數器AC=0,即游標移到左上角。
範例:
MOV A,#1
CALL WRIR
依照課本說明完成此命令需1.64ms,為安全起見,下達此命令後須等2ms以上,等LCD處理完此命令後,
才能再給LCD下一命令或資料,否則在LCD尚未處理完之前給的任何命令或資料均屬無效。
2.游標歸位
功能:LCD畫面不變,LCD位址計數器AC=0,游標移到左上角。
範例:
MOV A,#00000010B
CALL WRIR
依照課本說明完成此命令需1.64ms,時間較長,可以用第8個命令取代,亦可達到相同效果,
所花時間只要40us。
MOV A,#10000000B ;設定游標移至位址00的地方
CALL WRIR
3.進入模式設定
6. 功能設定
早期的CPU有4位元及8位元2種,這個命令主要是配合不同CPU介面設計,當設定CPU介面為4位元時,
只有DB7~DB4有作用,以後要送8位元資料時,都要將資料拆成2筆4位元資料,再分2次給LCD。目前
CPU都是8位元,LCD也幾乎都是2行的,所以標準命令如下
MOV A,#00111000B ;設定LCD介面長度為8位元,2行,5x7字型
CALL WRIR
(因為需先確定LCD與CPU間的介面形式,所以此命令必須最先執行)
7.CGRAM 位址設定
此命令提供使用者自行造字之功能,若LCD字型編碼表上沒有想顯示的字,使用者可自行造字,最多可
造8個字,此命令共保留6個位元,前3位元表示第n個字(n<=8), 後3位元代表該字的第幾行(共8行,最
後一行要填0),每一行的資料只有5位元,前3位元沒用到可直接給0或省略不寫,結果相同。
MOV A,#01000000B ;設定寫入字型資料, 從第一字的第一行開始
CALL WRIR
1.笙泉的82G516執行速度較快,如果將其他8051系列的LCD控制程式移植到82G516上執行,須調整寫入後的延遲時間, 否則可能會無法正常工作。 2.如果要在程式一開始就直接規劃LCD,建議先加一小段延遲程式,否則可能因LCD穩定時間較慢,單晶片在LCD還沒穩 定前所做的規劃是沒有意義的,LCD當然不能正常工作。一般在使用ICE測試時,電源早已啟動,不會有這個問題, 等實際燒錄作測試,需再按RESET才可正常工作時,通常是因為這個原因。範例下載1(ASM),下載2(ASM)
LCD CommandsThe commands for HD44780 chip are shown in the table below. Scroll down to see the commands in binary, and more info on the entry mode set command.
INSTRUCTION Decimal HexadecimalFunction set (8-bit interface, 2 lines, 5*7 Pixels) 56 38Function set (8-bit interface, 1 line, 5*7 Pixels) 48 30Function set (4-bit interface, 2 lines, 5*7 Pixels) 40 28Function set (4-bit interface, 1 line, 5*7 Pixels) 32 20Entry mode set Scroll display one character right (all lines) 28 1EScroll display one character left (all lines) 24 18Home (move cursor to top/left character position) 2 2Move cursor one character left 16 10Move cursor one character right 20 14Turn on visible underline cursor 14 0ETurn on visible blinking-block cursor 15 0FMake cursor invisible 12 0CBlank the display (without clearing) 8 08Restore the display (with cursor hidden) 12 0CClear Screen 1 01Set cursor position (DDRAM address) 128 + addr 80+ addrSet pointer in character-generator RAM (CG RAM address) 64 + addr 40+ addrRead DDRAM/CGRAM & Check Busy Flag
This command sets cursor move direction and display shift ON/OFF. There are 4 possible function set commands;04, 05, 06, and 07. This command changes the direction the cursor moves by setting the address counter to increment or decrement. This command is very important. If you do not understand it you may not see anything or what you actually wanted to see on LCD screen. I have created 4 animated gifs to demonstrate what the function set command is all about.When my friend Hakan Dereli wanted to learn about LC displays I gave him an LC display and the 4*4 keyboard that I used with my Z80 system.The keyboard is based on a 74C922 chip and my circuit to convert the 4 bit data output of the 74C922 to 8 bit data.It also has 3 other buttons on it.This makes it great to experiment with LCDs by entering commands/data manually. Hakan tested all possible commands by sending all the numbers between 00h and FFh to the LCD to see if there is an undocumented feature and see how all commands work. He did not find anything undocumented, but he noted how LCD behaves after each function set command.Below are the gifs to demonstrate those commands. To demonstrate the effect of each function set command it is assumed that the LCDs below are 2*16 LC Displays with DD RAM address set to 8 and underline cursor ON. Note that when the address counter is set to DECREMENT, strings sent to the LCD will be printed in reverse order. The same thing applies to the CG RAM, as well.
Entry mode set command 04h
Decrement Address Counter
Display Shift :OFF
String:Dereli
Entry mode set command 05h
Display Shift :ON
Decrement Address Counter
String :Hakan
Entry mode set command 06h
Display Shift :OFF
Increment Address Counter
String:Dereli
Entry mode set command 07h
Display Shift :ON
Increment Address Counter
String:Hakan
To check the state of the busy flag and read the address counter
*This is a 1-micro-second instruction.
Complete command reference:
Follow these links to navigate through my LCD related pages: Character Type LCD: Graphics LCD: (Based on: KS0108, HD61202 and similars...)
清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...