首页 > 代码库 > 基于51单片机的万年历(算法实现)

基于51单片机的万年历(算法实现)

基于51单片机的万年历,用到了单片机独立键盘、数码管、LED灯模块实现。

想要简单还是DS1302好用。

  1 /**************************************************
  2 
  3 作者:纟彖氵戋   博客:http://www.cnblogs.com/yllinux/
  4   
  5 时间:2017年6月7日
  6     
  7 目标:利用单片机独立键盘、数码管、LED灯模块实现万年历(算法实现)
  8       
  9 ***************************************************/
 10 #include <reg52.h>
 11 #define uchar unsigned char
 12 #define uint unsigned int
 13 
 14 uchar a[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80 };  //字形码表0~9和.
 15 uchar b[] = { 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff };  //数码管位码表1~8和0
 16 uchar c[] = { 0x3f,0xf3,0xcf };
 17 uint year = 2100;
 18 uchar month = 2, day = 14;
 19 uchar hour = 8, minute = 40, second = 0;
 20 uchar count;  //时钟计数
 21 uchar n = 1;  //年月日调位指示
 22 uchar j = 1;  //时分秒调位指示
 23 
 24 sbit Key1 = P3 ^ 0; //计时停止
 25 sbit Key2 = P3 ^ 1;    //调位
 26 sbit Key3 = P3 ^ 2;    //加一    
 27 sbit Key4 = P3 ^ 3; //切换
 28 sbit LED1 = P1 ^ 0;  //可删此灯,有冲突
 29 sbit we   = P2 ^ 7;
 30 sbit du   = P2 ^ 6;
 31 
 32 void Delayms(uint z)
 33 {
 34     uint x, y;
 35     for (x = 0; x < z; x++)
 36         for (y = 0; y < 114; y++);
 37 }
 38 
 39 //时分秒显示函数
 40 void Dispaly1(uchar hour, uchar minute, uchar second)
 41 {
 42     //数码管1
 43     P0 = b[8];
 44     we = 1;
 45     we = 0;
 46     P0 = a[hour / 10];
 47     du = 1;
 48     du = 0;
 49     P0 = b[0];
 50     we = 1;
 51     we = 0;
 52     Delayms(1);
 53     //数码管2    
 54     P0 = b[8];
 55     we = 1;
 56     we = 0;
 57     P0 = a[hour % 10];
 58     du = 1;
 59     du = 0;
 60     P0 = b[1];
 61     we = 1;
 62     we = 0;
 63     Delayms(1);
 64     //数码管2小数点
 65     P0 = b[8];
 66     we = 1;
 67     we = 0;
 68     P0 = a[10];
 69     du = 1;
 70     du = 0;
 71     P0 = b[1];
 72     we = 1;
 73     we = 0;
 74     Delayms(1);
 75     //数码管3
 76     P0 = b[8];
 77     we = 1;
 78     we = 0;
 79     P0 = a[minute / 10];
 80     du = 1;
 81     du = 0;
 82     P0 = b[2];
 83     we = 1;
 84     we = 0;
 85     Delayms(1);
 86     //数码管4    
 87     P0 = b[8];
 88     we = 1;
 89     we = 0;
 90     P0 = a[minute % 10];
 91     du = 1;
 92     du = 0;
 93     P0 = b[3];
 94     we = 1;
 95     we = 0;
 96     Delayms(1);
 97     //数码管4小数点
 98     P0 = b[8];
 99     we = 1;
100     we = 0;
101     P0 = a[10];
102     du = 1;
103     du = 0;
104     P0 = b[3];
105     we = 1;
106     we = 0;
107     Delayms(1);
108     //数码管5    
109     P0 = b[8];
110     we = 1;
111     we = 0;
112     P0 = a[second / 10];
113     du = 1;
114     du = 0;
115     P0 = b[4];
116     we = 1;
117     we = 0;
118     Delayms(1);
119     //数码管6
120     P0 = b[8];
121     we = 1;
122     we = 0;
123     P0 = a[second % 10];
124     du = 1;
125     du = 0;
126     P0 = b[5];
127     we = 1;
128     we = 0;
129     Delayms(1);
130 }
131 
132 
133 //年月日显示函数
134 void Dispaly2(uint year, uchar month, uchar day)
135 {
136     //数码管1
137     P0 = b[8];
138     we = 1;
139     we = 0;
140     P0 = a[year / 1000];
141     du = 1;
142     du = 0;
143     P0 = b[0];
144     we = 1;
145     we = 0;
146     Delayms(1);
147     
148     //数码管2    
149     P0 = b[8];
150     we = 1;
151     we = 0;
152     P0 = a[(year % 1000) / 100];
153     du = 1;
154     du = 0;
155     P0 = b[1];
156     we = 1;
157     we = 0;
158     Delayms(1);
159     
160     //数码管3
161     P0 = b[8];
162     we = 1;
163     we = 0;
164     P0 = a[(year % 100) / 10];
165     du = 1;
166     du = 0;
167     P0 = b[2];
168     we = 1;
169     we = 0;
170     Delayms(1);
171     
172     //数码管4
173     P0 = b[8];
174     we = 1;
175     we = 0;
176     P0 = a[year % 10];
177     du = 1;
178     du = 0;
179     P0 = b[3];
180     we = 1;
181     we = 0;
182     Delayms(1);
183     
184     //数码管4小数点
185     P0 = b[8];
186     we = 1;
187     we = 0;
188     P0 = a[10];
189     du = 1;
190     du = 0;
191     P0 = b[3];
192     we = 1;
193     we = 0;
194     Delayms(1);
195     
196     //数码管5
197     P0 = b[8];
198     we = 1;
199     we = 0;
200     P0 = a[month / 10];
201     du = 1;
202     du = 0;
203     P0 = b[4];
204     we = 1;
205     we = 0;
206     Delayms(1);
207     
208     //数码管6    
209     P0 = b[8];
210     we = 1;
211     we = 0;
212     P0 = a[month % 10];
213     du = 1;
214     du = 0;
215     P0 = b[5];
216     we = 1;
217     we = 0;
218     Delayms(1);
219     //数码管6小数点
220     P0 = b[8];
221     we = 1;
222     we = 0;
223     P0 = a[10];
224     du = 1;
225     du = 0;
226     P0 = b[5];
227     we = 1;
228     we = 0;
229     Delayms(1);
230     //数码管7
231     P0 = b[8];
232     we = 1;
233     we = 0;
234     P0 = a[day / 10];
235     du = 1;
236     du = 0;
237     P0 = b[6];
238     we = 1;
239     we = 0;
240     Delayms(1);
241     //数码管8
242     P0 = b[8];
243     we = 1;
244     we = 0;
245     P0 = a[day % 10];
246     du = 1;
247     du = 0;
248     P0 = b[7];
249     we = 1;
250     we = 0;
251     Delayms(1);
252 }
253 
254 void Keyscan1()
255 {
256     static    uchar i = 0;
257     if (Key1 == 0)
258     {
259         Delayms(5); //消抖    
260         if (Key1 == 0)
261             while (!Key1); //等待按键弹起
262             i++;
263     }
264     if (i % 2 == 1)
265     {
266         LED1 = 0;
267         TR0 = 0;
268     }
269     if (i % 2 == 0)
270     {
271         LED1 = 1;
272         TR0 = 1;
273     }
274     if (Key2 == 0)
275     {
276         Delayms(5);
277         if (Key2 == 0)
278             while (!Key2);
279             j++;
280             if (j == 3)
281                 j = 0;
282     }
283     if (j % 3 == 1)
284     {
285         if (Key3 == 0)
286         {
287             Delayms(5);
288             if (Key3 == 0)
289                 while (!Key3);
290                 second++;
291                 if (second == 60)
292                     second = 0;
293         }
294     }
295     if (j % 3 == 2)
296     {
297         if (Key3 == 0)
298         {
299             Delayms(5);
300             if (Key3 == 0)
301                 while (!Key3);
302                 minute++;
303                 if (minute == 60)
304                     minute = 0;
305         }
306     }
307     if (j % 3 == 0)
308     {
309         if (Key3 == 0)
310         {
311             Delayms(5);
312             if (Key3 == 0)
313                 while (!Key3);
314                 hour++;
315                 if (hour == 24)
316                     hour = 0;
317         }
318     }
319 }
320 
321 void Keyscan2()
322 {
323     static    uchar m = 0;
324     if (Key1 == 0)
325     {
326         Delayms(5);
327         if (Key1 == 0)
328             while (!Key3);
329             m++;
330             if (m == 3)
331                 m = 0;
332     }
333     if (m % 2 == 1)
334     {
335         LED1 = 0;
336         TR0 = 0;
337     }
338     if (m % 2 == 0)
339     {
340         LED1 = 1;
341         TR0 = 1;
342     }
343     if (Key2 == 0)
344     {
345         Delayms(5);
346         if (Key2 == 0)
347             while (!Key2);
348             n++;
349     }
350     if (n % 3 == 1)
351     {
352         if (Key3 == 0)
353         {
354             Delayms(5);
355             if (Key3 == 0)
356                 while (!Key3);
357                 day++;
358                 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))  //是闰年
359                 {
360                     if (month == 12 || month == 1 ||  month == 3 ||  month == 5 ||  month == 7 ||  month == 8 ||  month == 10)
361                     {
362                         if (day == 32)
363                         {
364                             day = 0;
365                         }
366                     }
367                     if ( month == 4 ||  month == 6 ||  month == 9 || month == 11 )
368                     {
369                         if (day == 31)
370                         {
371                             day = 0;
372                         }
373                     }
374                     if (month == 2)
375                     {
376                         if (day == 29)
377                         {
378                             day = 0;
379                         }
380                     }
381                 }                
382                 else    //不是闰年
383                 {
384                     if (month == 12 || month == 1 ||  month == 3 ||  month == 5 ||  month == 7 ||  month == 8 ||  month == 10)
385                     {
386                         if (day == 32)
387                         {
388                             day = 0;
389                         }
390                     }
391                     if ( month == 4 ||  month == 6 ||  month == 9 || month == 11 )
392                     {
393                         if (day == 31)
394                         {
395                             day = 0;
396                         }
397                     }
398                     if (month == 2)
399                     {
400                         if (day == 30)
401                         {
402                             day = 0;
403                         }
404                     }
405                 }
406                 
407         }
408     }
409     if (n % 3 == 2)
410     {
411         if (Key3 == 0)
412         {
413             Delayms(5);
414             if (Key3 == 0)
415                 while (!Key3);
416                 month++;
417                 if (month == 12)
418                     month = 0;
419         }
420     }
421     if (n % 3 == 0)
422     {
423         if (Key3 == 0)
424         {
425             Delayms(5);
426             if (Key3 == 0)
427                 while (!Key3);
428                 year++;
429                 if (year == 99)
430                     year = 0;
431         }
432     }
433     
434 }
435 
436 void main()
437 {
438     TMOD = 0x01;
439     TH0  = 0x4c;
440     //TH0 = (65536 - 10000) / 256;
441     TL0  = 0x00;
442     //TL0 = (65536 - 10000) % 256;
443     EA = 1;
444     ET0 = 1;
445     TR0 = 1;
446     while (1)
447     {
448         static    uchar h = 0;
449         if (Key4 == 0)
450         {
451             Delayms(5);
452             if (Key4 == 0)
453                 while (!Key4);
454                 h++;
455         }
456         if (h % 2 == 1)  //按Key4奇次数显示年月日
457         {
458             P1 = c[n % 3];  //年月日调位判断灯
459             Dispaly2(year, month, day);
460             Keyscan2();
461         }
462         if (h % 2 == 0)  //按Key4偶次数显示时分秒
463         {
464             P1 = c[j % 3];  //时分秒调位判断灯
465             Dispaly1(hour, minute, second);
466             Keyscan1();
467         }
468     }
469 }
470 
471 void time0_int(void) interrupt 1
472 {
473     //TH0 = (65536 - 10000) / 256;
474     //TL0 = (65536 - 10000) % 256;
475     TH0  = 0x4c;    //公式:定时时间t = (2^16 - T0初值) * 振荡周期 * 12         (振荡周期 * 12 即机器周期)
476     TL0  = 0x00;    //T0 = 2^16 - t * 11059200 / 12         (此定时时间为 50ms, T1 = 19456 = 0x4c00)
477     count++;
478     if (count == 20)
479     {
480         count = 0;
481         second++;
482         if (second == 60)
483         {
484             second = 0;
485             minute++;
486             if (minute == 60)
487             {
488                 minute = 0;
489                 hour++;
490                 if (hour == 24)
491                 {
492                     hour = 0;
493                     day++;
494                     if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))    //是闰年
495                     {
496                         if (month == 12 || month == 1 ||  month == 3 ||  month == 5 ||  month == 7 ||  month == 8 ||  month == 10)
497                         {
498                             if (day == 31)
499                             {
500                                 day = 0;
501                                 month++;
502                                 if (month == 12)
503                                 {
504                                     month = 1;
505                                     year++;
506                                     if (year == 99)
507                                     {
508                                         year = 0;
509                                     }
510                                 }
511                             }
512                         }
513                         if ( month == 4 ||  month == 6 ||  month == 9 || month == 11 )
514                         {
515                             if (day == 30)
516                             {
517                                 day = 0;
518                                 month++;
519                             }
520                         }
521                         if (month == 2)
522                         {
523                             if (day == 28)
524                             {
525                                 day = 0;
526                                 month++;
527                             }
528                         }
529                     }
530                     else    //不是闰年
531                     {
532                         if (month == 12 || month == 1 ||  month == 3 ||  month == 5 ||  month == 7 ||  month == 8 ||  month == 10)
533                         {
534                             if (day == 31)
535                             {
536                                 day = 0;
537                                 month++;
538                                 if (month == 12)
539                                 {
540                                     month = 1;
541                                     year++;
542                                     if (year == 99)
543                                     {
544                                         year = 0;
545                                     }
546                                 }
547                             }
548                         }
549                         if ( month == 4 ||  month == 6 ||  month == 9 || month == 11 )
550                         {
551                             if (day == 30)
552                             {
553                                 day = 0;
554                                 month++;
555                             }
556                         }
557                         if (month == 2)
558                         {
559                             if (day == 29)
560                             {
561                                 day = 0;
562                                 month++;
563                             }
564                         }
565                         
566                     }
567                 }
568             }
569         }
570     }
571 }

--

--

--

参考:

http://www.cnblogs.com/LXSYD-C/p/6364888.html

 

 

如有错误还请指出,如有侵权还请告知,如需转载请注明出处!                                              

本人博客:http://www.cnblogs.com/yllinux/

基于51单片机的万年历(算法实现)