用1602模拟电子钟功能
2010-08-17 16:39 李龙江 阅读(603) 评论(0) 编辑 收藏 举报
实现一个简单的电子钟功能,按S1键可选择调整时、分、秒,s2上调,s3下调
未实现调整日期功能
2
3 /*
4 li 8/17/2010
5 */
6 #include <reg52.h>
7 #define uint unsigned int
8 #define uchar unsigned char
9 sbit lcden=P3^4;
10 sbit lcdrs=P3^5;
11 sbit dula=P2^6;
12 sbit wela=P2^7;
13
14 sbit s1=P3^0;
15 sbit s2=P3^1;
16 sbit s3=P3^2;
17 sbit vss=P3^7;
18 uchar timeNum,i,Number,keyNum; //timeNum定时器计数,i For循环计数 Number时间临时变量 keyNum显示指针位置
19 char shi,fen,miao;
20 uchar code date[]="2010-08-17";
21 uchar code time[]="00:00:00";
22 //初始化
23 void init()
24 {
25 TMOD=0x01;
26 TH0=0xEE;
27 TL0=0x00;
28 ET0=1;
29 EA=1;
30 TR0=1;
31 dula=0;
32 wela=0;
33 }
34
35 //定时器
36 void timer() interrupt 1
37 {
38 TH0=0xEE;
39 TL0=0x00;
40 timeNum++;
41 }
42
43 //简单延时
44 void delay(uchar x)
45 {
46 uchar i;
47 while (x--)
48 for (i=0;i<110;i++);
49 }
50
51 //写命令
52 void write_com(uchar com)
53 {
54 lcdrs=0;
55 P0=com;
56 delay(1);
57 lcden=1;
58 lcden=0;
59 }
60
61 //写数据
62 void write_data(uchar date)
63 {
64 lcdrs=1;
65 P0=date;
66 delay(1);
67 lcden=1;
68 lcden=0;
69 }
70
71 //拆分十位和各位并显示
72 void sfm(uchar add,uchar Num)
73 {
74 uchar shi,ge;
75 shi=Num/10;
76 ge=Num%10;
77 write_com(0x80+0x40+add);
78 write_data(0x30+shi);
79 write_data(0x30+ge);
80 }
81
82 //1602初始化显示
83 void display()
84 {
85 write_com(0x38);
86 write_com(0x0C);
87 write_com(0x06);
88 write_com(0x01);
89 write_com(0x80);
90 for (i=0;i<10;i++)
91 {
92 write_data(date[i]);
93 }
94 write_com(0x80+0x40);
95 for (i=0;i<8;i++)
96 {
97 write_data(time[i]);
98 }
99 }
100
101 //扫描键盘
102 void keysacn()
103 {
104 vss=0;
105 if (s1==0)
106 {
107 delay(5);
108 if (s1==0)
109 {
110 keyNum++;
111 while (!s1);
112 write_com(0x0F);
113 TR0=0;
114 }
115 switch (keyNum)
116 {
117 case 1:
118 write_com(0x80+0x40+7);
119 break;
120 case 2:
121 write_com(0x80+0x40+4);
122 break;
123 case 3:
124 write_com(0x80+0x40+1);
125 break;
126 case 4:
127 keyNum=0;
128 write_com(0x0C);
129 TR0=1;
130 break;
131 }
132 }
133 if(keyNum!=0)
134 {
135 if(s2==0)
136 {
137 delay(5);
138 if (s2==0)
139 {
140 while(!s2);
141 switch (keyNum)
142 {
143 case 1:
144 miao++;
145 if(miao==60)
146 miao=0;
147 sfm(6,miao);
148 write_com(0x80+0x40+7);
149 break;
150 case 2:
151 fen++;
152 if(fen==60)
153 fen=0;
154 sfm(3,fen);
155 write_com(0x80+0x40+4);
156 break;
157 case 3:
158 shi++;
159 if(shi==24)
160 shi=0;
161 sfm(0,shi);
162 write_com(0x80+0x40+1);
163 break;
164 }
165 }
166 }
167 if(s3==0)
168 {
169 delay(5);
170 if (s3==0)
171 {
172 while(!s3);
173 switch (keyNum)
174 {
175 case 1:
176 miao--;
177 if(miao==-1)
178 miao=59;
179 sfm(6,miao);
180 write_com(0x80+0x40+7);
181 break;
182 case 2:
183 fen--;
184 if(fen==-1)
185 fen=59;
186 sfm(3,fen);
187 write_com(0x80+0x40+4);
188 break;
189 case 3:
190 shi--;
191 if(shi==-1)
192 shi=23;
193 sfm(0,shi);
194 write_com(0x80+0x40+1);
195 break;
196 }
197 }
198 }
199 }
200 }
201 void main()
202 {
203 init();
204 display();
205 while (1)
206 {
207 if (timeNum==200)
208 {
209 timeNum=0;
210 miao++;
211 if (miao==60)
212 {
213 miao=0;
214 fen++;
215 if (fen==60)
216 {
217 fen=0;
218 shi++;
219 if (shi==24)
220 {
221 shi=0;
222 }sfm(0,shi);
223 }sfm(3,fen);
224 }sfm(6,miao);
225 }
226 keysacn();
227 }
228 }
3 /*
4 li 8/17/2010
5 */
6 #include <reg52.h>
7 #define uint unsigned int
8 #define uchar unsigned char
9 sbit lcden=P3^4;
10 sbit lcdrs=P3^5;
11 sbit dula=P2^6;
12 sbit wela=P2^7;
13
14 sbit s1=P3^0;
15 sbit s2=P3^1;
16 sbit s3=P3^2;
17 sbit vss=P3^7;
18 uchar timeNum,i,Number,keyNum; //timeNum定时器计数,i For循环计数 Number时间临时变量 keyNum显示指针位置
19 char shi,fen,miao;
20 uchar code date[]="2010-08-17";
21 uchar code time[]="00:00:00";
22 //初始化
23 void init()
24 {
25 TMOD=0x01;
26 TH0=0xEE;
27 TL0=0x00;
28 ET0=1;
29 EA=1;
30 TR0=1;
31 dula=0;
32 wela=0;
33 }
34
35 //定时器
36 void timer() interrupt 1
37 {
38 TH0=0xEE;
39 TL0=0x00;
40 timeNum++;
41 }
42
43 //简单延时
44 void delay(uchar x)
45 {
46 uchar i;
47 while (x--)
48 for (i=0;i<110;i++);
49 }
50
51 //写命令
52 void write_com(uchar com)
53 {
54 lcdrs=0;
55 P0=com;
56 delay(1);
57 lcden=1;
58 lcden=0;
59 }
60
61 //写数据
62 void write_data(uchar date)
63 {
64 lcdrs=1;
65 P0=date;
66 delay(1);
67 lcden=1;
68 lcden=0;
69 }
70
71 //拆分十位和各位并显示
72 void sfm(uchar add,uchar Num)
73 {
74 uchar shi,ge;
75 shi=Num/10;
76 ge=Num%10;
77 write_com(0x80+0x40+add);
78 write_data(0x30+shi);
79 write_data(0x30+ge);
80 }
81
82 //1602初始化显示
83 void display()
84 {
85 write_com(0x38);
86 write_com(0x0C);
87 write_com(0x06);
88 write_com(0x01);
89 write_com(0x80);
90 for (i=0;i<10;i++)
91 {
92 write_data(date[i]);
93 }
94 write_com(0x80+0x40);
95 for (i=0;i<8;i++)
96 {
97 write_data(time[i]);
98 }
99 }
100
101 //扫描键盘
102 void keysacn()
103 {
104 vss=0;
105 if (s1==0)
106 {
107 delay(5);
108 if (s1==0)
109 {
110 keyNum++;
111 while (!s1);
112 write_com(0x0F);
113 TR0=0;
114 }
115 switch (keyNum)
116 {
117 case 1:
118 write_com(0x80+0x40+7);
119 break;
120 case 2:
121 write_com(0x80+0x40+4);
122 break;
123 case 3:
124 write_com(0x80+0x40+1);
125 break;
126 case 4:
127 keyNum=0;
128 write_com(0x0C);
129 TR0=1;
130 break;
131 }
132 }
133 if(keyNum!=0)
134 {
135 if(s2==0)
136 {
137 delay(5);
138 if (s2==0)
139 {
140 while(!s2);
141 switch (keyNum)
142 {
143 case 1:
144 miao++;
145 if(miao==60)
146 miao=0;
147 sfm(6,miao);
148 write_com(0x80+0x40+7);
149 break;
150 case 2:
151 fen++;
152 if(fen==60)
153 fen=0;
154 sfm(3,fen);
155 write_com(0x80+0x40+4);
156 break;
157 case 3:
158 shi++;
159 if(shi==24)
160 shi=0;
161 sfm(0,shi);
162 write_com(0x80+0x40+1);
163 break;
164 }
165 }
166 }
167 if(s3==0)
168 {
169 delay(5);
170 if (s3==0)
171 {
172 while(!s3);
173 switch (keyNum)
174 {
175 case 1:
176 miao--;
177 if(miao==-1)
178 miao=59;
179 sfm(6,miao);
180 write_com(0x80+0x40+7);
181 break;
182 case 2:
183 fen--;
184 if(fen==-1)
185 fen=59;
186 sfm(3,fen);
187 write_com(0x80+0x40+4);
188 break;
189 case 3:
190 shi--;
191 if(shi==-1)
192 shi=23;
193 sfm(0,shi);
194 write_com(0x80+0x40+1);
195 break;
196 }
197 }
198 }
199 }
200 }
201 void main()
202 {
203 init();
204 display();
205 while (1)
206 {
207 if (timeNum==200)
208 {
209 timeNum=0;
210 miao++;
211 if (miao==60)
212 {
213 miao=0;
214 fen++;
215 if (fen==60)
216 {
217 fen=0;
218 shi++;
219 if (shi==24)
220 {
221 shi=0;
222 }sfm(0,shi);
223 }sfm(3,fen);
224 }sfm(6,miao);
225 }
226 keysacn();
227 }
228 }