LCD1602的簡(jiǎn)單使用原理——跟大家分享我學(xué)習(xí)1602的總結(jié)與體會(huì) 我的總結(jié)主要分為這幾部分: ①LCD1602的硬件特性及引腳功能 ②LCD1602的時(shí)序特性 ③LCD1602的使用原理(包括帶字庫(kù)和不帶字庫(kù)的簡(jiǎn)要使用方法,控制顯示指令) ④基于FPGA的LCD1602使用案例
Part 1. LCD1602的硬件特性及引腳功能 LCD1602顧名思義是一種02*16,即為兩行十六列的液晶顯示屏,液晶兩行,每行可以顯示16個(gè)字符,但是CGRAM及CGROM里面一共有160個(gè)字符,包括阿拉伯?dāng)?shù)字,英文字母大小寫,常用符號(hào)及日文。每個(gè)字符對(duì)應(yīng)于一個(gè)ASCII碼值,在液晶顯示屏上顯示對(duì)應(yīng)的字符時(shí)候,只需要將對(duì)應(yīng)的ASCII碼寫到DDRAM中就好,詳細(xì)的步驟會(huì)在下面細(xì)說(shuō)。液晶板上排列著5*8的字符點(diǎn)陣,8行,每行5個(gè)點(diǎn)位,高電平1就是該點(diǎn)顯示,低電平0就是該點(diǎn)不顯示。 RS,R/W,E控制數(shù)據(jù)端口DB0~DB7,數(shù)據(jù)的命令的讀寫由控制端口控制,并通過(guò)數(shù)據(jù)端口傳輸。端口其他特性這里不再贅述,詳細(xì)見(jiàn)1602液晶手冊(cè)。O(∩_∩)O 二、硬件特性: ①CGRAM 和CGROM CGRAM:character generator ram CGROM:character generator rom CGRAM的地址空間: CGRAM的地址是0x40~0x7F, 64個(gè)地址空間,每個(gè)地址雙字節(jié),一共128字節(jié),一個(gè)字符 是8個(gè)字節(jié),所以一共能顯示8個(gè)自定義字符(每個(gè)雙字節(jié)地址只有一個(gè)字節(jié)是被自定義字符數(shù)據(jù)寫入的,另外個(gè)字節(jié)無(wú)效,因?yàn)镃GARM的字符代碼的規(guī)定,詳細(xì)原因見(jiàn)下面) 字符對(duì)應(yīng)的區(qū)位碼如下圖所示: CGRAM: 字符產(chǎn)生ram,用來(lái)存放用戶自定義的字符,如上圖的兩條(1)~(8),區(qū)位碼為0x00~0x0F.0x00~0x07對(duì)應(yīng)于(1)~(8);0x08~0x0F對(duì)應(yīng)于下一條(1)~(8),雖然看起來(lái)有16個(gè)地址,但是其實(shí)只要8個(gè)地址可用,CGRAM的“字符碼”規(guī)定0~2為地址,3位無(wú)效,4~7位全為0,因此CGRAM的字符碼等效為0000X111,X為無(wú)效位,最后三位的地址只要八個(gè),所以實(shí)際能用的只有8個(gè)。 其他為CGROM中自帶的字符,區(qū)位碼從0x21~0x7F,以各自的ASCII碼作為區(qū)位碼表示的基本字符。 將自定義的字符字模數(shù)8*8據(jù)寫入,字符數(shù)據(jù)有八行,每行八位點(diǎn)陣。 ②DDRAM DDRAM:data display ram數(shù)據(jù)顯示存儲(chǔ)器 DDRAM的地址空間與屏幕的對(duì)應(yīng)關(guān)系如下圖:
DDRAM的地址空間一共有80字節(jié)
在1602中,我們只要前面16行就行,其地址和屏幕的對(duì)應(yīng)關(guān)系:
Part 2. LCD1602的時(shí)序特性 讀狀態(tài):RS=0 ,RW=1,E=1 讀數(shù)據(jù):RS=1,RW=1,E=1
寫命令:RS=0,RW=0,E=下降沿脈沖;DB0~DB7指令字 寫數(shù)據(jù):RS=1,RW=0,E=下降沿脈沖;DB0~DB7數(shù)據(jù) 在E=1的時(shí)候?qū)懭霐?shù)據(jù),在數(shù)據(jù)寫完之后,E來(lái)一個(gè)下降沿,把數(shù)據(jù)送到LCD。
Part4.LCD1602的指令功能 <1>cursor move to first digit <2>地址計(jì)數(shù)器AC的值設(shè)置為0; <1>把光標(biāo)撤回到顯示器的左上方 <2>把地址計(jì)數(shù)器Ac設(shè)置為0; <3>保持DDRAM的值不變 - Entry Mode set輸入模式設(shè)置指令
Function: I/D set cursor move direction H:increase L:decrease 寫入數(shù)據(jù)之后的光標(biāo)移動(dòng)的方向H:右移L:左移 S specifies shift of display H:display is shifted L:display is not shifted 寫入一個(gè)數(shù)據(jù)之后顯示屏移動(dòng)或者不移動(dòng) - Display On/off顯示開(kāi)關(guān)控制指令
D:H:顯示開(kāi); L:顯示關(guān) C:H:光標(biāo)開(kāi) L:光標(biāo)關(guān) B:H: 光標(biāo)閃爍 L:光標(biāo)不閃爍 - Shift 設(shè)置顯示屏或者光標(biāo)移動(dòng)的方向
S/C:顯示屏還是光標(biāo) H:顯示屏移動(dòng)L:光標(biāo)移動(dòng) R/L:向左移動(dòng)還是向右移動(dòng)H:右移L;左移(光標(biāo)右移:AC值加1;光標(biāo)左移:AC值減1) DL:data length L:數(shù)據(jù)總線為4位 H:數(shù)據(jù)總線為8位
N :number line L:1行 顯示 H:2行顯示
F : L:5×7點(diǎn)陣/每字符 H:5×10點(diǎn)陣/每字符
7、Set CGRAM Address 設(shè)置CGRAM的地址,我們將我們自定義的字模數(shù)據(jù)存入對(duì)應(yīng)的地址,從0x40~0x7F,128字節(jié),8個(gè)字符的字模數(shù)據(jù)可存入。 - Set DDRAM Address,(表格中寫錯(cuò)了,是DDRAM)
與CGRAM一樣,在往DDRAM里面寫入想要顯示的字符的字符區(qū)位碼的之前需要將存儲(chǔ)字符區(qū)位碼的地址首先寫入。DDRAM的地址空間以上已述。(加地址的時(shí)候我們要加上0x80,因?yàn)閷懭氲刂返臅r(shí)候DB7必須為1) - Read Busy Flag and the Address讀取忙碌標(biāo)志和地址計(jì)數(shù)器的值
BF:H:忙碌,表示無(wú)法結(jié)束單片機(jī)送來(lái)的數(shù)據(jù);L:準(zhǔn)備就緒,可以接受數(shù)據(jù) 當(dāng)內(nèi)部操作正在進(jìn)行的時(shí)候,讀取BF的值 AC:讀取地址計(jì)數(shù)器的值 - 數(shù)據(jù)寫入CGRAM和DDRAM
- 從CGRAM和DDRAM讀取數(shù)據(jù)
Part5.LCD1602的簡(jiǎn)要原理
顯示字庫(kù)中本來(lái)就有的字符和顯示自定義的字符
步驟一:系統(tǒng)初始化和LCD初始化 步驟二:LCD液晶屏上面每個(gè)字符對(duì)應(yīng)于DDRAm的地址,你想要把字符寫進(jìn)屏幕哪個(gè)位置,就往DDRAM寫入該位置所對(duì)應(yīng)的地址。(對(duì)應(yīng)的地址在上述已說(shuō)) 步驟三:字庫(kù)中本來(lái)已經(jīng)存在的字符各自有對(duì)應(yīng)的字符區(qū)位碼,其實(shí)就是每個(gè)字符對(duì)應(yīng)的ASCII碼(見(jiàn)字符表格)。你要顯示哪個(gè)字符,只要將該字符對(duì)應(yīng)的ASCII碼寫入DDRAM就搞定了,這個(gè)是很簡(jiǎn)單的。
其實(shí)顯示自定義的字符和字庫(kù)字符差不多的,只需要將你自定義的字符用取模工具得到字模的數(shù)組數(shù)據(jù)之后。之后的步驟和顯示字庫(kù)字符是一樣一樣滴。 步驟一:系統(tǒng)初始化和LCD初始化 步驟二:搞清楚字符點(diǎn)陣的格式是5*8,通過(guò)取字模工具得出自定義字符的字模數(shù)據(jù),再寫入相應(yīng)的CGRAM地址,一個(gè)地址寫入一個(gè)一個(gè)字節(jié)數(shù)據(jù)(一個(gè)字節(jié)的8位,前3位為0,點(diǎn)陣的格式5*8) 步驟三:同顯示字庫(kù)中本來(lái)就有的字符一樣的做法 Part 6. 基于FPGA的LCD1602使用案例
以下的程序例子是基于FPGA的液晶1602時(shí)鐘,是利用自帶字庫(kù)的方式顯示實(shí)時(shí)時(shí)鐘,主要通過(guò)時(shí)鐘控制狀態(tài)機(jī)來(lái)實(shí)現(xiàn)1602的工作程序(尚未驗(yàn)證嘿嘿)
- library ieee;
- use ieee.std_logic_1164.all;
- use ieee.std_logic_arith.all;
- use ieee.std_logic_unsigned.all;
- entity lcd is
- port(
- clk:in std_logic;
- rw:out std_logic;
- rs:out std_logic;
- lcd_rst:out std_logic;
- data:out std_logic_vector( 7 downto 0);
- en:out std_logic
- );
- end lcd;
- architecture lcd1602 of lcd is
- signal clk_1m:std_logic:='1';
- signal clk_1s:std_logic:='1';
- type state is(s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10);--定義狀態(tài)類型case語(yǔ)句上有具體說(shuō)明
- signal current_s:state:=s0;
- type data_buffer is array(0 to 9) of std_logic_vector(7 downto 0);---定義數(shù)組類型
- signal
- date_buf:data_buffer:=(x"30",x"31",x"3a",x"33",x"34",x"3a",x"36",x"37",x"2a",x"2a");
- signal shi,fen,miao:integer range 0 to 100:=0;
- signal shi1,fen1,miao1:integer range 0 to 100:=0;
- begin
- process(clk)---對(duì)CLK 50M進(jìn)行分頻系數(shù)為50的分頻clk_1m
- variable cnt:integer range 0 to 10000;---得到1M的時(shí)鐘
- begin
- if clk'event and clk='1' then
- if cnt=24 then
- clk_1m <= not clk_1m;cnt:=0;
- else cnt:=cnt+1;
- end if;
- end if;
- end process;
- process(clk)
- variable cnt:integer range 0 to 10000;
- variable cnt1:integer range 0 to 5000;
- begin
- if clk'event and clk='1' then
- if cnt=24900000 then ---cnt:0~24900000counter
- clk_1s <= not clk_1s;cnt:=0;------fenpin clk_1s
- elsif cnt1=2000 then cnt:=cnt+1;cnt1:=0;---cnt1: 0~2000 counter
- else cnt1:=cnt1+1;
- end if;
- end if;
- end process;
- process(clk_1s)
- begin
- if clk_1s'event and clk_1s='1' then
- if miao =59 then
- miao<=0;
- if fen=59 then
- fen<=0;
- if shi=23 then
- shi<=0;
- else shi<=shi+1;
- end if;
- else fen<=fen+1;
- end if;
- else miao<=miao+1;
- end if;
- end if;
- date_buf(0)<=conv_std_logic_vector (shi /10 ,8)+x"30";--強(qiáng)制轉(zhuǎn)換邏輯矢量,8bit
- date_buf(1)<=conv_std_logic_vector (shi mod 10,8)+x"30";
- date_buf(3)<=conv_std_logic_vector (fen /10,8)+x"30";
- date_buf(4)<=conv_std_logic_vector (fen mod 10,8)+x"30";
- date_buf(6)<=conv_std_logic_vector (miao /10,8)+x"30";
- date_buf(7)<=conv_std_logic_vector (miao mod 10 ,8)+x"30";
- end process;
- process(clk_1m,current_s,date_buf)
- variable i:integer range 0 to 11:=0;
- variable cnt1:integer range 0 to 100;
- variable cnt2:integer range 0 to 100;
- variable cnt3:integer range 0 to 100;
- variable cnt4:integer range 0 to 100;
- variable cnt5:integer range 0 to 100;
- variable cnt6:integer range 0 to 100;
- variable cnt7:integer range 0 to 100;
- variable cnt8:integer range 0 to 60000;
- begin
- current_s<=s0;
- if (clk_1m'event and clk_1m='1') then
- current_s <= current_s ;
- case current_s is
- when s0=>
- if cnt1 <5000 then
- lcd_rst<='0';cnt1:=cnt1+1;
- elsif cnt1<10000 then
- lcd_rst<='1';cnt1:=cnt1+1;
- elsif cnt1=10000 then lcd_rst<='1';current_s<=s1;cnt1:=0;
- end if;
-
- when s1=> cnt2:=cnt2+1;----設(shè)置功能指令字:8位數(shù)據(jù)總線,兩行顯示,5*7點(diǎn)陣
- if cnt2<10 then rw<='0';rs<='0';----command 0
- elsif cnt2<20 then data<=x"38";00111000
- elsif cnt2<30 then en<='1';
- elsif cnt2<70 then en<='0';
- elsif cnt2=100 then cnt2:=0;current_s<=s2;
- end if;
- when s2=> ----顯示開(kāi)關(guān)控制指令:D顯示開(kāi),C光標(biāo)開(kāi),B光標(biāo)閃爍
- cnt3:=cnt3+1;
- if cnt3<10 then rw<='0';rs<='0';
- elsif cnt3<20 then data<=x"0f";----00001111
- elsif cnt3<30 then en<='1';
- elsif cnt3<70 then en<='0';
- elsif cnt3=100 then cnt3:=0;current_s<=s3;
- end if;
- when s3=> -----輸入模式設(shè)置指令,寫入一個(gè)字符后光標(biāo)右移,顯示屏不
- cnt4:=cnt4+1; -----移動(dòng)
- if cnt4<10 then rw<='0';rs<='0';
- elsif cnt4<20 then data<=x"06";---00000110
- elsif cnt4<30 then en<='1';
- elsif cnt4<70 then en<='0';
- elsif cnt4=100 then cnt4:=0;current_s<=s5;
- end if;
-
- when s4=> -----displayclear 顯示清屏
- cnt5:=cnt5+1;
- if cnt5<10 then rw<='0';rs<='0';
- elsif cnt5<20 then data<=x"01";---00000001
- elsif cnt5<30 then en<='1';
- elsif cnt5<70 then en<='0';
- elsif cnt5=100 then cnt5:=0;current_s<=s5;
- end if;
-
- when s5=> -----寫DDRAM地址0X00+0X80
- cnt6:=cnt6+1;
- if cnt6<10 then rw<='0';rs<='0';
- elsif cnt6<20 then data<=x"80";---10000000
- elsif cnt6<30 then en<='1';
- elsif cnt6<70 then en<='0';
- elsif cnt6=100 then cnt6:=0;current_s<=s6;
- end if;
- when s6=>
- cnt7:=cnt7+1;
- if cnt7<10 then rw<='0';rs<='1';----WRITE DATA
- elsif cnt7<20 then data<=date_buf(i);--date_buf(i);時(shí)分秒的數(shù)據(jù)的ASCII碼
- elsif cnt7<30 then en<='1'; ---執(zhí)行時(shí)間為40us
- elsif cnt7<70 then en<='0';
- elsif cnt7=100 then
- if i=9 then cnt7:=0;current_s<=s7;i:=0;
- else i:=i+1;current_s<=s6;cnt7:=0;end if;
- end if;
-
- when s7=>if cnt8<8000 then cnt8:=cnt8+1;
- else cnt8:=0;current_s<=s5;----循環(huán)等待8000的計(jì)數(shù)器,回到S5,繼續(xù)寫入DDRAM地 ------址,進(jìn)而
- end if;-------------------------寫入數(shù)據(jù)
- when others=>null;
- end case;
- end if;
- end process;
- end lcd1602;
復(fù)制代碼 |