標(biāo)題:
用1602液晶顯示屏實現(xiàn)時鐘
[打印本頁]
作者:
sunyuanguo
時間:
2016-5-10 11:33
標(biāo)題:
用1602液晶顯示屏實現(xiàn)時鐘
一、本次實驗所需器材
1、Arduino板
2、1602液晶板:也叫1602字符型液晶,它是一種專門用來顯示字母、數(shù)字、符號等的點陣型液晶模塊。它由若干個5X7或者5X11等點陣字符位組成,每個點陣字符位都可以顯示一個字符,每位之間有一個點距的間隔,每行之間也有間隔,起到了字符間距和行間距的作用,正因為如此所以它不能很好地顯示圖形。目前,盡管各廠家對其各自產(chǎn)品命名不盡相同,但均提供幾乎都同樣規(guī)格的1602模塊或者兼容模塊。1602最初采用的LCD控制器是HD44780,在各廠家生產(chǎn)的1602模塊中,基本上也都采用了與之兼容的控制IC,所以從特性上基本上是一樣的。因此,我們買到的1602模塊,在端口標(biāo)記上可能有所不同,有的從左向右,有的從右向左,但特性上是一樣的。本實驗中使用的1602板,最里面的孔為1號,最靠近邊上的那個為16號。
1602實物圖片:
1602的規(guī)格情況:
顯示容量
16*2個字符
芯片工作電壓
4.5-5.5V
工作電流
2,0MA(5.0V)
模塊最佳工作電壓
5.0V
字符尺寸
2.95*4.35(WXH)mm
1602管腳介紹:
第1腳:VSS為電源負(fù)極
第2腳:VCC接5V電源正極
第3腳:V0為液晶顯示器對比度調(diào)整端,接正電源時對比度最弱,接地電源時對比度最高(對比度過高時會產(chǎn)生“鬼影”,使用時可以通過一個10K的電位器調(diào)整對比度,本實驗使用了一個1K
Ω
電阻)。
第4腳:RS為寄存器選擇,高電平1時選擇數(shù)據(jù)寄存器、低電平0時選擇指令寄存器。
第5腳:RW為讀寫信號線,高電平(1)時進行讀操作,低電平(0)時進行寫操作。
第6腳:E(或EN)端為使能(enable)端,高電平(1)時讀取信息,負(fù)跳變時執(zhí)行指令。
第7~14腳:D0~D7為8位雙向數(shù)據(jù)端。
第15~16腳:背燈電源。15腳背光正極,16腳背光負(fù)極。
1602字符集介紹:
1602液晶模塊內(nèi)部的字符發(fā)生存儲器已經(jīng)存儲了160個不同的點陣字符圖形,這些字符有:阿拉伯?dāng)?shù)字、英文字母的大小寫、常用的符號、和日文假名等,每一個字符都有一個固定的代碼,比如大寫的英文字母“A”的代碼是01000001B(41H),顯示時模塊把地址41H中的點陣字符圖形顯示出來,我們就能看到字母“A”。
1602的16進制ASCII碼表地址可從百度搜索,這里不在列出,只寫用法。如:感嘆號!的ASCII為0x21,字母B的ASCII為0x42。
1602指令集:
3、電阻:1K
Ω
(1個)
4、杜邦線:若干
5、面包板:一個
二、8位連接法實驗
1、硬件連接方式
2、代碼:
[url=]
[/url]
int
DI =
12
;
int
RW =
11
;
int
DB[] = {
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
};
//
使用數(shù)組來定義總線需要的管腳
int
Enable =
2
;
void
LcdCommandWrite(
int
value) {
//
定義所有引腳
int
i =
0
;
for
(i=DB[
0
]; i <= DI; i++)
//
總線賦值
{ digitalWrite(i,value
&
01
);
//
因為1602液晶信號識別是D7-D0(不是D0-D7),這里是用來反轉(zhuǎn)信號。
value >>=
1
; } digitalWrite(Enable,LOW); delayMicroseconds(
1
); digitalWrite(Enable,HIGH); delayMicroseconds(
1
);
//
延時1ms
digitalWrite(Enable,LOW); delayMicroseconds(
1
);
//
延時1ms
}
void
LcdDataWrite(
int
value) {
//
定義所有引腳
int
i =
0
; digitalWrite(DI, HIGH); digitalWrite(RW, LOW);
for
(i=DB[
0
]; i <= DB[
7
]; i++
) { digitalWrite(i,value
&
01
); value
>>=
1
; } digitalWrite(Enable,LOW); delayMicroseconds(
1
); digitalWrite(Enable,HIGH); delayMicroseconds(
1
); digitalWrite(Enable,LOW); delayMicroseconds(
1
);
//
延時1ms
}
void
setup (
void
) {
int
i =
0
;
for
(i=Enable; i <= DI; i++
) { pinMode(i,OUTPUT); } delay(
100
);
//
短暫的停頓后初始化LCD
//
用于LCD控制需要
LcdCommandWrite(
0x38
);
//
設(shè)置為8-bit接口,2行顯示,5x7文字大小
delay(
64
); LcdCommandWrite(
0x38
);
//
設(shè)置為8-bit接口,2行顯示,5x7文字大小
delay(
50
); LcdCommandWrite(
0x38
);
//
設(shè)置為8-bit接口,2行顯示,5x7文字大小
delay(
20
); LcdCommandWrite(
0x06
);
//
輸入方式設(shè)定
//
自動增量,沒有顯示移位
delay(
20
); LcdCommandWrite(
0x0E
);
//
顯示設(shè)置
//
開啟顯示屏,光標(biāo)顯示,無閃爍
delay(
20
); LcdCommandWrite(
0x01
);
//
屏幕清空,光標(biāo)位置歸零
delay(
100
); LcdCommandWrite(
0x80
);
//
顯示設(shè)置
//
開啟顯示屏,光標(biāo)顯示,無閃爍
delay(
20
); }
void
loop (
void
) { LcdCommandWrite(
0x01
);
//
屏幕清空,光標(biāo)位置歸零
delay(
10
); LcdCommandWrite(
0x80
+
3
); delay(
10
);
//
寫入歡迎信息
LcdDataWrite(
'
H
'
); LcdDataWrite(
'
E
'
); LcdDataWrite(
'
L
'
); LcdDataWrite(
'
L
'
); LcdDataWrite(
'
O
'
); LcdDataWrite(
'
'
); LcdDataWrite(
'
W
'
); LcdDataWrite(
'
O
'
); LcdDataWrite(
'
R
'
); LcdDataWrite(
'
L
'
); LcdDataWrite(
'
D
'
); delay(
10
); LcdCommandWrite(
0xc0
+
2
);
//
定義光標(biāo)位置為第二行第二個位置
delay(
10
); LcdDataWrite(
'
w
'
); LcdDataWrite(
'
i
'
); LcdDataWrite(
'
k
'
); LcdDataWrite(
'
i
'
); LcdDataWrite(
'
c
'
); LcdDataWrite(
'
o
'
); LcdDataWrite(
'
d
'
); LcdDataWrite(
'
e
'
); LcdDataWrite(
'
.
'
); LcdDataWrite(
'
n
'
); LcdDataWrite(
'
e
'
); LcdDataWrite(
'
t
'
); delay(
5000
); LcdCommandWrite(
0x01
);
//
屏幕清空,光標(biāo)位置歸零
delay(
10
); LcdDataWrite(
'
I
'
); LcdDataWrite(
'
'
); LcdDataWrite(
'
a
'
); LcdDataWrite(
'
m
'
); LcdDataWrite(
'
'
); LcdDataWrite(
'
x
'
); LcdDataWrite(
'
i
'
); LcdDataWrite(
'
a
'
); LcdDataWrite(
'
o
'
); LcdDataWrite(
'
w
'
); LcdDataWrite(
'
u
'
); LcdDataWrite(
'
y
'
); LcdDataWrite(
'
i
'
); delay(
3000
); LcdCommandWrite(
0x02
);
//
設(shè)置模式為新文字替換老文字,無新文字的地方顯示不變。
delay(
10
); LcdCommandWrite(
0x80
+
5
);
//
定義光標(biāo)位置為第一行第6個位置
delay(
10
); LcdDataWrite(
'
t
'
); LcdDataWrite(
'
h
'
); LcdDataWrite(
'
e
'
); LcdDataWrite(
'
'
); LcdDataWrite(
'
a
'
); LcdDataWrite(
'
d
'
); LcdDataWrite(
'
m
'
); LcdDataWrite(
'
i
'
); LcdDataWrite(
'
n
'
); delay(
5000
); }
[url=]
[/url]
3、實驗效果
三、四位連接法
4位連接法,可以省出幾個數(shù)字端口。
1、連接方法
2、代碼
[url=]
[/url]
int
LCD1602_RS=
12
;
int
LCD1602_RW=
11
;
int
LCD1602_EN=
10
;
int
DB[] = {
6
,
7
,
8
,
9
};
char
str1[]=
"
Welcome to
"
;
char
str2[]=
"
wikicode.net
"
;
char
str3[]=
"
This is the
"
;
char
str4[]=
"
4-bit interface
"
;
void
LCD_Command_Write(
int
command) {
int
i,temp; digitalWrite( LCD1602_RS,LOW); digitalWrite( LCD1602_RW,LOW); digitalWrite( LCD1602_EN,LOW); temp
=command &
0xf0
;
for
(i=DB[
0
]; i <=
9
; i++
) { digitalWrite(i,temp
&
0x80
); temp
<<=
1
; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(
1
); digitalWrite( LCD1602_EN,LOW); temp
=(command &
0x0f
)<<
4
;
for
(i=DB[
0
]; i <=
9
; i++
) { digitalWrite(i,temp
&
0x80
); temp
<<=
1
; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(
1
); digitalWrite( LCD1602_EN,LOW); }
void
LCD_Data_Write(
int
dat) {
int
i=
0
,temp; digitalWrite( LCD1602_RS,HIGH); digitalWrite( LCD1602_RW,LOW); digitalWrite( LCD1602_EN,LOW); temp
=dat &
0xf0
;
for
(i=DB[
0
]; i <=
9
; i++
) { digitalWrite(i,temp
&
0x80
); temp
<<=
1
; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(
1
); digitalWrite( LCD1602_EN,LOW); temp
=(dat &
0x0f
)<<
4
;
for
(i=DB[
0
]; i <=
9
; i++
) { digitalWrite(i,temp
&
0x80
); temp
<<=
1
; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(
1
); digitalWrite( LCD1602_EN,LOW); }
void
LCD_SET_XY(
int
x,
int
y ) {
int
address;
if
(y ==
0
) address =
0x80
+
x;
else
address =
0xC0
+
x; LCD_Command_Write(address); }
void
LCD_Write_Char(
int
x,
int
y,
int
dat) { LCD_SET_XY( x, y ); LCD_Data_Write(dat); }
void
LCD_Write_String(
int
X,
int
Y,
char
*
s) { LCD_SET_XY( X, Y );
//
設(shè)置地址
while
(*s)
//
寫字符串
{ LCD_Data_Write(
*
s); s
++
; } }
void
setup (
void
) {
int
i =
0
;
for
(i=
6
; i <=
12
; i++
) { pinMode(i,OUTPUT); } delay(
100
); LCD_Command_Write(
0x28
);
//
4線 2行 5x7
delay(
50
); LCD_Command_Write(
0x06
); delay(
50
); LCD_Command_Write(
0x0c
); delay(
50
); LCD_Command_Write(
0x80
); delay(
50
); LCD_Command_Write(
0x01
); delay(
50
); }
void
loop (
void
) { LCD_Command_Write(
0x01
); delay(
50
); LCD_Write_String(
3
,
0
,str1);
//
第1行,第4個地址起
delay(
50
); LCD_Write_String(
2
,
1
,str2);
//
第2行,第2個地址起
delay(
5000
); LCD_Command_Write(
0x01
);
//
delay(
50
); LCD_Write_String(
0
,
0
,str3); delay(
50
); LCD_Write_String(
0
,
1
,str4); delay(
5000
); }
[url=]
[/url]
這里僅做了顯示字符串的方法,也可以利用LCD_Write_Char這個過程來顯示字符,如:LCD_Write_Char(1,0,0x3a)顯示“:”,這里不再舉例,在下面的時鐘的代碼中,會有所利用。
3、實現(xiàn)效果
四、利用Arduino+1602實現(xiàn)時鐘
1、硬件連接:采用四位連接法。
2、代碼
[url=]
[/url]
int
LCD1602_RS=
12
;
int
LCD1602_RW=
11
;
int
LCD1602_EN=
10
;
int
DB[] = {
6
,
7
,
8
,
9
};
char
logtxt[]=
"
Local Time
"
;
char
*sec[
60
]={
"
00
"
,
"
01
"
,
"
02
"
,
"
03
"
,
"
04
"
,
"
05
"
,
"
06
"
,
"
07
"
,
"
08
"
,
"
09
"
,
"
10
"
,
"
11
"
,
"
12
"
,
"
13
"
,
"
14
"
,
"
15
"
,
"
16
"
,
"
17
"
,
"
18
"
,
"
19
"
,
"
20
"
,
"
21
"
,
"
22
"
,
"
23
"
,
"
24
"
,
"
25
"
,
"
26
"
,
"
27
"
,
"
28
"
,
"
29
"
,
"
30
"
,
"
31
"
,
"
32
"
,
"
33
"
,
"
34
"
,
"
35
"
,
"
36
"
,
"
37
"
,
"
38
"
,
"
39
"
,
"
40
"
,
"
41
"
,
"
42
"
,
"
43
"
,
"
44
"
,
"
45
"
,
"
46
"
,
"
47
"
,
"
48
"
,
"
49
"
,
"
50
"
,
"
51
"
,
"
52
"
,
"
53
"
,
"
54
"
,
"
55
"
,
"
56
"
,
"
57
"
,
"
58
"
,
"
59
"
};
void
LCD_Command_Write(
int
command) {
int
i,temp; digitalWrite( LCD1602_RS,LOW); digitalWrite( LCD1602_RW,LOW); digitalWrite( LCD1602_EN,LOW); temp
=command &
0xf0
;
for
(i=DB[
0
]; i <=
9
; i++
) { digitalWrite(i,temp
&
0x80
); temp
<<=
1
; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(
1
); digitalWrite( LCD1602_EN,LOW); temp
=(command &
0x0f
)<<
4
;
for
(i=DB[
0
]; i <=
9
; i++
) { digitalWrite(i,temp
&
0x80
); temp
<<=
1
; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(
1
); digitalWrite( LCD1602_EN,LOW); }
void
LCD_Data_Write(
int
dat) {
int
i=
0
,temp; digitalWrite( LCD1602_RS,HIGH); digitalWrite( LCD1602_RW,LOW); digitalWrite( LCD1602_EN,LOW); temp
=dat &
0xf0
;
for
(i=DB[
0
]; i <=
9
; i++
) { digitalWrite(i,temp
&
0x80
); temp
<<=
1
; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(
1
); digitalWrite( LCD1602_EN,LOW); temp
=(dat &
0x0f
)<<
4
;
for
(i=DB[
0
]; i <=
9
; i++
) { digitalWrite(i,temp
&
0x80
); temp
<<=
1
; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(
1
); digitalWrite( LCD1602_EN,LOW); }
void
LCD_SET_XY(
int
x,
int
y ) {
int
address;
if
(y ==
0
) address =
0x80
+
x;
else
address =
0xC0
+
x; LCD_Command_Write(address); }
void
LCD_Write_Char(
int
x,
int
y,
int
dat) { LCD_SET_XY( x, y ); LCD_Data_Write(dat); }
void
LCD_Write_String(
int
X,
int
Y,
char
*
s) { LCD_SET_XY( X, Y );
//
設(shè)置地址
while
(*s)
//
寫字符串
{ LCD_Data_Write(
*
s); s
++
; } }
void
setup (
void
) {
int
i =
0
;
for
(i=
6
; i <=
12
; i++
) { pinMode(i,OUTPUT); } delay(
100
); LCD_Command_Write(
0x28
);
//
4線 2行 5x7
delay(
50
); LCD_Command_Write(
0x06
); delay(
50
); LCD_Command_Write(
0x0c
); delay(
50
); LCD_Command_Write(
0x80
); delay(
50
); LCD_Command_Write(
0x01
); delay(
50
); }
void
loop (
void
) {
for
(
int
index=
0
,minindex=
59
,hourindex=
12
;index<
60
;index++)
//
這里來設(shè)定現(xiàn)在時間,目前設(shè)定為12:59:00
{ LCD_Command_Write(
0x01
); delay(
50
); LCD_Write_String(
3
,
0
,logtxt); LCD_Write_String(
3
,
1
,sec[hourindex]); LCD_Write_Char(
6
,
1
,
0x3a
);//顯示: LCD_Write_String(
7
,
1
,sec[minindex]); LCD_Write_Char(
10
,
1
,
0x3a
); LCD_Write_String(
11
,
1
,sec[index]); delay(
950
);
if
(index==
59
) { index
=-
1
; minindex
++
;
if
(minindex==
60
) { minindex
=
0
; hourindex
++
;
if
(hourindex==
24
) { hourindex
=
0
; } } } } }
[url=]
[/url]
作者:
暗雙魂
時間:
2017-11-1 14:59
謝謝樓主的分享
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1