More Related Content
More from Nattawut Kathaisong
More from Nattawut Kathaisong (8)
บทที่ 11 การควบคุมจอแสดงผล lcd
- 2. 11.8 จอแสดงผล LCD
80 81 82 83 84 85 86 87 88 89 8A 8B 8D 8E
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF
GN
D
VC
C
VO RS
R/
W
E D0 D1 D2 D3 D4 D5 D6 D7 A K
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
รูปที่ 11.1 จอแสดงผล LCD ขนาด 16 X 2
รูปที่ 11.2 ขาสัญญาณของ LCD
- 3. ตารางที่ 11.1 หน้าที่การทางานขาสัญญาณของจอ LCD
ตาแหน่งขา หน้าที่การทางาน
1 GND หรือ Vss เป็นขากกราวด์ของจอ LCD
2 VDD เป็นขาสัญญาณไฟกระแสตรง 5 โวลต์
3 VEE เป็นขาสัญญาณไฟที่ใช้ปรับความคมชัดในการแสดงผลของจอ LCD
4 RS เป็นขาสัญญาณที่ใช้ควบคุมถ้าขา RS=0 ข้อมูลที่ส่งมาทางขา D0-D7 จะเป็นคาสั่งแต่ถ้าขา RS=1
จะเป็นข้อมูลที่ส่งออกมาแสดงผล
5 R/W เป็นขาสัญญาณควบคุมการอ่านและเขียน ถ้าขา R/W=0 แสดงว่าต้องการเขียนข้อมูลลงจอ
LCD แต่ถ้า R/W=1 ต้องการอ่านข้อมูลจากจอ LCD
6 E เป็นขาสัญญาณอีนาเบิ้ล (Enable) คือขาสัญญาณที่จะต้องส่งสัญญาณพัลส์ 1 ลูกเมื่อมีการส่งข้อมูล
หรือคาสั่งมายังจอแสดงผล LCD
7-14 D0-D7 ขาสัญญาณข้อมูล
15 A เป็นขาสัญญาณไฟกระแสตรง 5 โวลต์ เมื่อต้องการให้จอ LCD สว่าง
16 K เป็นขาสัญญาณกราวด์เมื่อต้องการให้จอ LCD สว่าง
- 5. ตารางที่ 11.2 คาสั่งควบคุมการทางานของ LCD
คาสั่ง RS E คาอธิบาย
01H
06H
0CH
38H
0
0
0
0
ลบหน้าจอแสดงผล LCD
เลื่อนเคอร์เซอร์ไปทางขวา
เปิดจอแสดงผลแต่ไม่แสดง
เคอร์เซอร์
กาหนดให้ LCD ทางานแบบ 8 บิต
แสดงผลแบบ 16 ตัวอักษร 2
บรรทัดที่ความละเอียด 5 X 7 จุด
- 8. หลักการเขียนโปรแกรมควบคุมการแสดงผลของจอ LCD
#include <reg51.h>
sbit rs=P3^0;
sbit e=P3^2;
void delay(int x);
void init_LCD();
void write(char dat,int add);
void enable();
void main()
{
init_LCD();
while(1)
{
write('M',0x80);
write('C',0x81);
write('S',0x82);
write('-',0x83);
write('5',0x84);
write('1',0x85);
write(' ',0x86);
write('2',0x87);
write('0',0x88);
write('0',0x89);
write('8',0x8A);
}
}
void delay(int x)
{
for(x=x;x>0;x--);
}
void enable()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- 11. โปรแกรมวนรอบส่งข้อความ RAJABHAT ออกจอแสดงผล LCD
#include <reg51.h>
sbit rs=P3^0;
sbit e=P3^2;
void delay(int x);
void init_LCD();
void write(char dat,int add);
void enable();
void main()
{char text[8]={'R','A','J','A','B','H','A','T'},i;
init_LCD();
while(1)
{
for(i=0;i<8;i++)
write(text[i],0x84+i);
}
}
void delay(int x)
{for(x=x;x>0;x--);
}
void enable()
{ e=1;
delay(500);
e=0;
delay(500);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- 12. void init_LCD()
{ //LCD 16*2
delay(20000);
rs=0;
P2=0x38;
enable();
//clear
rs=0;
P2=0x01;
enable();
//Cursor shift right
rs=0;
P2=0x06;
enable();
//Display LCD
rs=0;
P2=0x0C;
enable();
}
void write(char dat,int add)
{ //add locate
rs=0;
P2=add;
enable();
//write data to LCD
rs=1;
P2=dat;
enable();
}
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
ต่อ
- 14. โปรแกรมนับ 0 ถึง 9 แสดงผลบนจอ LCD
#include <reg51.h>
sbit rs=P3^0;
sbit e=P3^2;
void delay(int x);
void init_LCD();
void write(char dat,int add);
void enable();
void main()
{char num[10]={'0','1','2','3','4','5','6','7','8','9'};
char text[5]={'C','o','u','n','t'};
int count,i;
init_LCD();
while(1)
{ for(i=0;i<5;i++)
write(text[i],0x84+i);
for(count=0;count<10;count++)
{ write(num[count],0x8a);
delay(5000);
}
}
}
void delay(int x)
{for(x=x;x>0;x--);
}
void enable()
{ e=1;
delay(500);
e=0;
delay(500);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
- 15. void init_LCD()
{ //LCD 16*2
delay(20000);
rs=0;
P2=0x38;
enable();
//clear
rs=0;
P2=0x01;
enable();
//Cursor shift right
rs=0;
P2=0x06;
enable();
//Display LCD
rs=0;
P2=0x0C;
enable();
}
void write(char dat,int add)
{ //add locate
rs=0;
P2=add;
enable();
//write data to LCD
rs=1;
P2=dat;
enable();
}
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
56
57
58
59
60
61
ต่อ
- 17. โปรแกรมนับ 00 ถึง 59
#include <reg51.h>
sbit rs=P3^0;
sbit e=P3^2;
void delay(int x);
void init_LCD();
void write(unsigned char dat, add);
void enable();
void main()
{char num[10]={'0','1','2','3','4','5','6','7','8','9'};
char text[5]={'C','o','u','n','t'};
int count,i;
init_LCD();
while(1)
{ for(i=0;i<5;i++)
write(text[i],0x84+i);
for(count=0;count<60;count++)
{ write(num[count],0x8a);
delay(5000);
}
}
}
void delay(int x)
{for(x=x;x>0;x--);
}
void enable()
{ e=1;
delay(500);
e=0;
delay(500);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
- 20. โปรแกรมนาฬิกา
#include <reg51.h>
sbit rs=P3^0;
sbit e=P3^2;
void delay(int x);
void init_LCD();
void write(unsigned char dat, add);
void enable();
void main()
{char num[10]={'0','1','2','3','4','5','6','7','8','9'};
char text[5]={'C','L','O','C','K'};
int hour,minit,i;
init_LCD();
while(1)
{ for(i=0;i<5;i++)
write(text[i],0x86+i);
for(hour=0;hour<24;hour++)
for(minit=0;minit<60;minit++)
{ write(num[hour/10],0xC6);
write(num[hour%10],0xC7);
write(':',0xC8);
write(num[minit/10],0xC9);
write(num[minit%10],0xCA);
}
}
}
void delay(int x)
{for(x=x;x>0;x--);
}
void enable()
{ e=1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- 21. delay(500);
e=0;
delay(500);
}
void init_LCD()
{ //LCD 16*2
delay(20000);
rs=0;
P2=0x38;
enable();
//clear
rs=0;
P2=0x01;
enable();
//Cursor shift right
rs=0;
P2=0x06;
enable();
//Display LCD
rs=0;
P2=0x0C;
enable();
}
void write(unsigned char dat, add)
{ //add locate
rs=0;
P2=add;
enable();
//write data to LCD
rs=1;
P2=dat;
enable();
}
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
56
57
58
59
60
61
62
63
64
ต่อ
- 23. 11.5 สรุปท้ายบท
หลักการเขียนโปรแกรมควบคุมการแสดงผลของจอ LCD เริ่มจาก
กาหนดชนิดของจอแสดงผล LCD ขนาด 16 ตัวอักษร 2 บรรทัด ส่งข้อมูล
แบบ 8 บิต ด้วยคาสั่ง P2=0X38; กาหนดให้เคอร์เซอร์เลื่อนไปทางซ้ายด้วย
คาสั่ง P2=0X06; กาหนดเปิดจอแสดงผลแต่ไม่แสดงเคอร์เซอร์ด้วยคาสั่ง
P2=0X0C; กาหนดตาแหน่งที่ต้องการแสดงผลด้วยคาสั่ง P2=add; จากนั้นส่ง
ข้อมูลออกแสดงผลด้วยคาสั่ง P2=data; โดยที่ data คือข้อมูลที่ต้องส่งออกและ
add คือตาแหน่งที่ต้องการส่งออก