-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD12232.c
371 lines (330 loc) · 8.35 KB
/
LCD12232.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define PD1 61 //二分屏
sbit k1=P1^0;//定义按键k1停止计时
sbit k2=P1^1;//定义按键k2小时加一
sbit k3=P1^6;//定义按键k3分钟加一
sbit k4=P1^7;//定义按键k4确认
uchar Page_;//页地址寄存器
uchar Code_;//字符代码寄存器
uchar Command;//指令寄存器
uchar LCDData;//数据寄存器
uchar Column;//列地址
//uint ssec=0,sec=55,min=59,hor=23;
uint ssec=0,sec=0,min=0,hor=0;
uchar DisplayData[14];
xdata uchar CWADD1 _at_ 0x8000;//定义数据以及指令地址
xdata uchar DWADD1 _at_ 0x8001;
xdata uchar CRADD1 _at_ 0x8002;
xdata uchar DRADD1 _at_ 0x8003;
xdata uchar CWADD2 _at_ 0x8004;
xdata uchar DWADD2 _at_ 0x8005;
xdata uchar CRADD2 _at_ 0x8006;
xdata uchar DRADD2 _at_ 0x8007;
//定义字符内码值表
code uchar Tab[][8]=
{
{0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00},//0
{0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00},//1
{0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00},//2
{0x00,0x21,0x41,0x49,0x4D,0x33,0x00,0x00},//3
{0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00},//4
{0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00},//5
{0x00,0x3C,0x4A,0x49,0x49,0x31,0x00,0x00},//6
{0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00},//7
{0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00},//8
{0x00,0x46,0x49,0x49,0x29,0x1E,0x00,0x00},//9
{0x00,0x24,0x54,0x54,0x38,0x40,0x00,0x00},//A//小写
{0x00,0x7F,0x28,0x44,0x44,0x38,0x00,0x00},//B
{0x00,0x38,0x44,0x44,0x44,0x08,0x00,0x00},//C
{0x00,0x38,0x44,0x44,0x28,0x7F,0x00,0x00},//D
{0x00,0x38,0x54,0x54,0x54,0x08,0x00,0x00},//E
{0x00,0x08,0x7E,0x09,0x09,0x02,0x00,0x00},//F
{0x00,0x98,0xA4,0xA4,0xA4,0x78,0x00,0x00},//G 10
{0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00},//H 11
{0x00,0x00,0x00,0x79,0x00,0x00,0x00,0x00},//I 12
{0x00,0x00,0x80,0x88,0x79,0x00,0x00,0x00},//J 13
{0x00,0x7F,0x10,0x28,0x44,0x40,0x00,0x00},//K 14
{0x00,0x00,0x41,0x7F,0x40,0x00,0x00,0x00},//L 15
{0x00,0x78,0x04,0x78,0x04,0x78,0x00,0x00},//M 16
{0x00,0x04,0x78,0x04,0x04,0x78,0x00,0x00},//n 17
{0x00,0x38,0x44,0x44,0x44,0x38,0x00,0x00},//O 18
{0x00,0xFC,0x24,0x24,0x24,0x18,0x00,0x00},//P 19
{0x00,0x18,0x24,0x24,0x24,0xFC,0x00,0x00},//Q 1a
{0x00,0x04,0x78,0x04,0x04,0x08,0x00,0x00},//R 1b
{0x00,0x48,0x54,0x54,0x54,0x24,0x00,0x00},//S 1c
{0x00,0x04,0x3F,0x44,0x44,0x24,0x00,0x00},//T 1d
{0x00,0x3C,0x40,0x40,0x3C,0x40,0x00,0x00},//U 1e
{0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00},//V 1f
{0x00,0x3C,0x40,0x3C,0x40,0x3C,0x00,0x00},//W 20
{0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00},//X 21
{0x00,0x9C,0xA0,0xA0,0x90,0x7C,0x00,0x00},//Y 22
{0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00},//Z 23
{0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00},//: 24
{0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00},//. 25
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
};// 空 删除用26
void Delay(uchar i)
{
while(i--);
}
void Timer0Init()
{
TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
TH0=0Xd8; //给定时器赋初值,定时10ms
TL0=0Xf0;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
PT0=0;
}
void Datapros()
{
DisplayData[0]=0x1d;
DisplayData[1]=0x12;
DisplayData[2]=0x16;
DisplayData[3]=0x0e;
DisplayData[4]=0x26;
DisplayData[5]=0x26;
//显示time:五个字符
DisplayData[6]=hor/10;
DisplayData[7]=hor%10;
DisplayData[8]=0x24;
DisplayData[9]=min/10;
DisplayData[10]=min%10;
DisplayData[11]=0x24;
DisplayData[12]=sec/10;
DisplayData[13]=sec%10;
//分别计算时分秒显示
}
void WriteDataE1()
{
while(CRADD1&0x80); //判忙 1忙 0等
DWADD1=LCDData;
}
void WriteDataE2()
{
while(CRADD2&0x80);
DWADD2=LCDData;
}
void WriteCommandE1()
{
while(CRADD1&0x80);
CWADD1=Command;
}
void WriteCommandE2()
{
while(CRADD2&0x80);
CWADD2=Command;
}
void LCDinit()//初始化
{
Command=0xe2; //复位
WriteCommandE1();
WriteCommandE2();
Command=0xa4; //静态驱动
WriteCommandE1();
WriteCommandE2();
Command=0xa9; //刷新率1/32
WriteCommandE1();
WriteCommandE2();
Command=0xa1; //ADC
WriteCommandE1();
WriteCommandE2();
Command=0xc0; //行
WriteCommandE1();
WriteCommandE2();
Command=0xaf; //开显示
WriteCommandE1();
WriteCommandE2();
}
void Clear()//清屏
{
uchar i,j;
i=0;
do
{
Command=(i+0xb8);
WriteCommandE1();
WriteCommandE2();
Command=0x00;
WriteCommandE1();
WriteCommandE2();
j=0x50;//页内八十列
do
{
LCDData=0x00;
WriteDataE1();
WriteDataE2();
}
while(--j!=0);
}
while(++i!=4);//是否四页全部完成
}
void WriteEnglish()
{
uchar i,j,k;
i=0;
j=0;
while(j<1)
{
Command=((Page_+j)&0x03)|0xb8;
WriteCommandE1();
WriteCommandE2();
k=Column;
while(k<Column+8)
{
if(k<PD1)//写左屏
{
Command=k;
WriteCommandE1();
LCDData=Tab[Code_][i];
WriteDataE1();
}
else//写右屏
{
Command=k-PD1;
WriteCommandE2();
LCDData=Tab[Code_][i];
WriteDataE2();
};
i++;
if(++k>=PD1*2)break;
};
j++;
};
}
void Show()
{
Page_=0x02;//在第二页显示
Column=0x00;
Code_=DisplayData[0];
WriteEnglish();
Column=0x08;
Code_=DisplayData[1];
WriteEnglish();
Column=0x10;
Code_=DisplayData[2];
WriteEnglish();
Column=0x18;
Code_=DisplayData[3];
WriteEnglish();
Column=0x20;
Code_=DisplayData[4];
WriteEnglish();
Column=0x28;
Code_=DisplayData[5];
WriteEnglish();
Column=0x30;
Code_=DisplayData[6];
WriteEnglish();
Column=0x38;
Code_=DisplayData[7];
WriteEnglish();
Column=0x40;
Code_=DisplayData[8];
WriteEnglish();
Column=0x48;
Code_=DisplayData[9];
WriteEnglish();
Column=0x50;
Code_=DisplayData[10];
WriteEnglish();
Column=0x58;
Code_=DisplayData[11];
WriteEnglish();
Column=0x60;
Code_=DisplayData[12];
WriteEnglish();
Column=0x68;
Code_=DisplayData[13];
WriteEnglish();
Column=0x70;
Code_=0x26;
WriteEnglish();
}
void Time() //K2 hor K3 min k4 ok
{
while(1)
{
if(k2==0) //检测按键K2是否按下
{
Delay(1000); //消除抖动 一般大约10ms
if(k2==0) //再次判断按键是否按下
{
hor+=1;
if(hor==24)
hor=0;
}
while(!k2); //检测按键是否松开
}
if(k3==0) //检测按键K3是否按下
{
Delay(1000); //消除抖动 一般大约10ms
if(k3==0) //再次判断按键是否按下
{
min+=1;
if(min==60)
min=0;
}
while(!k3); //检测按键是否松开
}
if(k4==0)//检测按键K4是否按下
{
while(!k4); //检测按键是否松开
TR0=1;
break;
}
Datapros();
Show();
}
}
void main()
{
Timer0Init(); //定时器0初始化
LCDinit(); //初始化
Clear(); //清屏
while(1)
{
Datapros();
Show();
if(k1==0) //检测按键K1是否按下
{
Delay(1000); //延时消抖
if(k1==0)
{
while(!k1); //检测按键是否松开
//light();
TR0=0;
Time(); //调时间函数
Timer0Init(); //定时器0初始化
}
}
}
}
void Timer0() interrupt 1
{
TH0=0Xd8; //给定时器赋初值,定时10ms
TL0=0Xf0;
ssec++;
if(ssec>=10) //1sec
{
ssec=0;
sec++;
if(sec>=60) //1min
{
sec=0;
min++;
if(min>=60) //1hor
{
min=0;
hor++;
if(hor>23) //day
{
hor=0;
}
}
}
}
}