0%

2. 8051驱动 LCD1602 例子

配套电路图

C程序代码

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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <8051.h>
#define true 1
#define false 0
#define HIGH 1
#define LOW 0

/**
* LCD1602 的测试例子
*
* @author 鸿则<[email protected]>
* @Date 2018-02-11
*/

// LCD1602 define
#define LCD1602_E P2_6
#define LCD1602_RS P2_4
#define LCD1602_RW P2_5

// length is 16
unsigned char line1[] = " hello world! ";
unsigned char line2[] = "~!@#$%^&*()_+[]?";

void delay(unsigned int multiple)
{
unsigned int i;
for(; multiple>0; multiple--)
for(i=100; i>0; i--);
}

void writeCommand(unsigned char command)
{
LCD1602_E = LOW;
LCD1602_RS = LOW;
LCD1602_RW = LOW;
P0 = command;
delay(10);
LCD1602_E = HIGH;
delay(20);
LCD1602_E = LOW;
}

void writeData(unsigned char data)
{
LCD1602_E = LOW;
LCD1602_RS = HIGH;
LCD1602_RW = LOW;
P0 = data;
delay(10);
LCD1602_E = HIGH;
delay(20);
LCD1602_E = LOW;
}

void initLCD1602()
{
delay(300);
writeCommand(0x38);
delay(100);
writeCommand(0x38);
delay(100);
writeCommand(0x38);
writeCommand(0x38);
writeCommand(0x08);
writeCommand(0x01);
writeCommand(0x06);
writeCommand(0x0C);
}

void setup()
{
unsigned char i;
initLCD1602();
for(i=0; i<16; i++)
{
writeCommand(0x80 + i);
writeData(line1[i]);
delay(10);
}
for(i=0; i<16; i++)
{
writeCommand(0x80 + 0x40 + i);
writeData(line2[i]);
delay(10);
}
}

void loop()
{

}

void main()
{
setup();
while(true) loop();
}