分享一下单片机数字钟制作
时分秒
年月日
星期和闹钟时间
成品正面图
单片机源程序如下:
- #include<reg51.h>
- #include<intrins.h>
- unsigned char code distab[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- unsigned char data yue=5 ,ri=15 ,shi=7 ,fen=59 ,miao=58 ,int_cnt=0 ,nshi=8 ,nfen=0,xq,xianshi=0,xuanze=0;
- unsigned int data nian=2018,j,m,y;
- bit x=0,t=0,b=0;
- sbit k0=P1^1;
- sbit k1=P1^2;
- sbit k2=P1^3;
- sbit k3=P1^4;
- sbit LED=P1^0;
- void mDelay(unsigned int Delay)
- {
- unsigned int i;
- for(;Delay>0;Delay--)
- {
- for(i=0;i<114;i++){;}
- }
- }
- void displayshijian()
- {
- P2=0xff;
- if( ( xuanze==1||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[miao%10]+0x80);
- P2=0xfe;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==1||LED==0 )&&j>100 )P0=0xff;
- else P0=~distab[miao/10];
- P2=0xfd;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==2||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[fen%10]+0x80);
- P2=0xfb;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==2||LED==0 )&&j>100 )P0=0xff;
- else P0=~distab[fen/10];
- P2=0xf7;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==3||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[shi%10]+0x80);
- P2=0xef;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==3||LED==0 )&&j>100 )P0=0xff;
- else P0=~distab[shi/10];
- P2=0xdf;
- mDelay(1);
- }
- void displaynyr()
- {
- P2=0xff;
- if( ( xuanze==1||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[ri%10]+0x80);
- P2=0xfe;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==1||LED==0 )&&j>100 )P0=0xff;
- else P0=~distab[ri/10];
- P2=0xfd;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==2||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[yue%10]+0x80);
- P2=0xfb;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==2||LED==0 )&&j>100 )P0=0xff;
- else P0=~distab[yue/10];
- P2=0xf7;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==3||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[nian%10]+0x80);
- P2=0xef;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==3||LED==0 )&&j>100 )P0=0xff;
- else P0=~distab[(nian%100)/10];
- P2=0xdf;
- mDelay(1);
- }
- void displaynaozhong()
- {
- P2=0xff;
- if( ( xuanze==1||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[nfen%10]+0x80);
- P2=0xfe;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==1||LED==0 )&&j>100 )P0=0xff;
- else P0=~distab[nfen/10];
- P2=0xfd;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==2||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[nshi%10]+0x80);
- P2=0xfb;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==2||LED==0 )&&j>100 )P0=0xff;
- else P0=~distab[nshi/10];
- P2=0xf7;
- mDelay(1);
-
- P2=0xff;
- if( ( xuanze==3||LED==0 )&&j>100 )P0=0xff;
- else P0=~(distab[xq]+0x80);
- P2=0xdf;
- mDelay(1);
- }
- void chushihua()
- {
- TMOD=0x01; //定时方式1
- TH0=(19456/256);
- TL0=(19456%256);
- EA=1;ET0=1; //开T0中断
- TR0=1; //启动T0
- int_cnt=0;
- }
- void chuli2()
- {
- if(yue==12)
- {
- if(nian==2099)
- {
- nian=2000;
- yue=1;
- ri=1;
- }
- else
- {
- nian++;
- yue=1;
- ri=1;
- }
- }
- else
- {
- yue++;
- ri=1;
- }
- }
- void chuli1()
- {
- if(miao==60)
- {
- fen++;
- miao=0;
- x=0;
- if(fen==60)
- {
- shi++;
- fen=0;
- if(shi==24)
- {
- ri++;
- shi=0;
- if( ( (yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12) )&&( ri==32 ) )
- {
- chuli2();
- }
- else if( ( (yue==4)||(yue==6)||(yue==9)||(yue==11) )&&( ri==31 ) )
- {
- chuli2();
- }
- else if( ( ( nian%4!=0 )||( nian==2000 ) )&&( yue==2 )&&( ri==29 ) )
- {
- chuli2();
- }
- else if( ( nian%4==0 )&&( nian!=2000 )&&( yue==2 )&&( ri==30 ) )
- {
- chuli2();
- }
- }
- }
- }
- }
- void display()
- {
- switch(xianshi)
- {
- case 0:displayshijian();break;
- case 1:displaynyr();break;
- case 2:displaynaozhong();break;
- default:;
- }
- }
- void key()
- {
- if(!k0)
- {
- mDelay(1);
- if(!k0)
- {
- if(LED==1)
- {
- xianshi++;
- if(xianshi==3)xianshi=0;
- }
- else{x=1;t=0;}
- while(!k0){display();chuli1();}
- }
- }
- if(!k1)
- {
- mDelay(1);
- if(!k1)
- {
- if(LED==1)
- {
- xuanze++;
- if(xuanze==4)xuanze=0;
- }
- else{x=1;t=0;}
- while(!k1){display();chuli1();}
- }
- }
- if(!k2)
- {
- mDelay(1);
- if(!k2)
- {
- if(xianshi==0&&xuanze==1){miao++;if(miao==60)miao=0;}
- if(xianshi==0&&xuanze==2){fen++;if(fen==60)fen=0;}
- if(xianshi==0&&xuanze==3){shi++;if(shi==24)shi=0;}
- if(xianshi==1&&xuanze==1)
- {
- ri++;
- if( ( (yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12) )&&( ri==32 ) )
- {
- ri=1;
- }
- else if( ( (yue==4)||(yue==6)||(yue==9)||(yue==11) )&&( ri==31 ) )
- {
- ri=1;
- }
- else if( ( ( nian%4!=0 )||( nian==2000 ) )&&( yue==2 )&&( ri==29 ) )
- {
- ri=1;
- }
- else if( ( nian%4==0 )&&( nian!=2000 )&&( yue==2 )&&( ri==30 ) )
- {
- ri=1;
- }
- }
- if(xianshi==1&&xuanze==2)
- {
- yue++;
- if(yue==13)
- yue=1;
- if( ( (yue==4)||(yue==6)||(yue==9)||(yue==11) )&&( ri>30 ) )
- {
- ri=30;
- }
- else if( ( ( nian%4!=0 )||( nian==2000 ) )&&( yue==2 )&&( ri>28 ) )
- {
- ri=28;
- }
- else if( ( nian%4==0 )&&( nian!=2000 )&&( yue==2 )&&( ri>29 ) )
- {
- ri=29;
- }
- }
- if(xianshi==1&&xuanze==3){nian++;if(nian==2100)nian=2000;}
- if(xianshi==2&&xuanze==1){nfen++;if(nfen==60)nfen=0;}
- if(xianshi==2&&xuanze==2){nshi++;if(nshi==24)nshi=0;}
- if(xuanze==0&&LED==0){x=1;t=0;} //x置1,闹钟取消
- if(xuanze==0&&LED==1)b=0;
- while(!k2){display();chuli1();}
- }
- }
- if(!k3)
- {
- mDelay(1);
- if(!k3)
- {
- if(xianshi==0&&xuanze==1){miao--;if(miao>60)miao=59;}
- if(xianshi==0&&xuanze==2){fen--;if(fen>60)fen=59;}
- if(xianshi==0&&xuanze==3){shi--;if(shi>24)shi=23;}
- if(xianshi==1&&xuanze==1)
- {
- ri--;
- if(ri==0)
- {
- if( (yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12) )
- {
- ri=31;
- }
- else if( (yue==4)||(yue==6)||(yue==9)||(yue==11) )
- {
- ri=30;
- }
- else if( ( ( nian%4!=0 )||( nian==2000 ) )&&( yue==2 ) )
- {
- ri=28;
- }
- else if( ( nian%4==0 )&&( nian!=2000 )&&( yue==2 ) )
- {
- ri=29;
- }
- }
- }
- if(xianshi==1&&xuanze==2)
- {
- yue--;
- if(yue==0)
- yue=12;
- if( (yue==4)||(yue==6)||(yue==9)||(yue==11)&&(ri>30) )
- {
- ri=30;
- }
- else if( ( ( nian%4!=0 )||( nian==2000 ) )&&( yue==2 )&&(ri>28) )
- {
- ri=28;
- }
- else if( ( nian%4==0 )&&( nian!=2000 )&&( yue==2 )&&(ri>29) )
- {
- ri=29;
- }
- }
- if(xianshi==1&&xuanze==3){nian--;if(nian==1999)nian=2099;}
- if(xianshi==2&&xuanze==1){nfen--;if(nfen>60)nfen=59;}
- if(xianshi==2&&xuanze==2){nshi--;if(nshi>24)nshi=23;}
- if(xuanze==0&&LED==0)t=1; //t置1,闹钟取消,延时3分钟
- if(xuanze==0&&LED==1)b=1;
- while(!k3){display();chuli1();}
- }
- }
- }
- void jsxq()
- {
- if(yue==1||yue==2)
- {
- m=yue+12;
- y=nian-1;
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
数字钟(仿真+源程序).zip
(74.71 KB, 下载次数: 194)
|