程序:

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
#include"reg51.h"
sbit RS = P3^0;
sbit RW = P3^1;
sbit E = P3^2;
unsigned int count=0;
unsigned int hour=0,min=0,sec=0;
unsigned char str[]={"0123456789"};
unsigned char str1[]={"Clock:"};
void delay(unsigned int t)
{
int i=0,j=0;
for(i=0;i<t;i++)
for(j=0;j<=100;j++);
}

void writedat(unsigned char dat) //写入命令
{
RS= 1;
RW= 0;
E = 0;
P2=dat;
delay(5);
E=1;
E=0;
}

void writecom(unsigned char com) //写入命令
{
RS= 0;
RW= 0;
E = 0;
P2=com;
delay(5);
E=1;
E=0;
E=1;
}

void display()
{
int i=0;
unsigned char temp0=0,temp1=0,temp2=0,temp3=0,temp4=0,temp5=0;
temp0=hour/10;
temp1=hour%10;
temp2=min/10;
temp3=min%10;
temp4=sec/10;
temp5=sec%10;
writecom(0x80);
delay(5);
while(str[i]!='\0')
{
writedat(str1[i]);
delay(5);
i++;
}
writecom(0x80+0x40+4);
delay(5);
writedat(str[temp0]);
delay(5);
writedat(str[temp1]);
delay(5);
writedat(':');
delay(5);
writedat(str[temp2]);
delay(5);
writedat(str[temp3]);
delay(5);
writedat(':');
delay(5);
writedat(str[temp4]);
delay(5);
writedat(str[temp5]);
delay(5);
}


void initlcd() //初始化
{
writecom(0x38);
writecom(0x0c);
writecom(0x06);
writecom(0x01);
}
void inittimer()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}

void main()
{
initlcd();
inittimer();
while(1)
{
display();
}
}

void timer0_isr() interrupt 1
{
TH0=(65536-50000)/256; //无法重装载
TL0=(65536-50000)%256;
count++;
if(count==20)
{
sec++;
count=0;
}
if(sec==60)
{
min++;
sec=0;
}
if(min==60)
{
hour++;
min=0;
}
if(hour==24)
{
hour=0;
}
}