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

Posted yllinux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于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 )
以上是关于基于51单片机的万年历(算法实现)的主要内容,如果未能解决你的问题,请参考以下文章

基于51单片机+DS1302万年历+LCD1602显示+按键播报时间+温控风扇+按键控灯

基于51单片机+DS1302万年历+LCD1602显示+按键播报时间+温控风扇+按键控灯

电子时钟万年历+51单片机+1602液晶屏+DS1302+DS18B20+按键

基于单片机的电子万年历的设计

51单片机+LCD12864的万年历Proteus仿真

基于单片机的多功能时钟闹钟万年历控制系统设计