51单片机实现万年历利用AT89S52单片机的P0口来和另外几个口来控制1602液晶的显示和P1口还有其它口来控制ds12887时钟芯片。设置四个按键,1个定义为时间设置功能键,一个定义为闹钟设置功能键,另外两个用来调节时间的增减。原理图:pcb图:源程序:#includereg52.h#defineuintunsignedint#defineucharunsignedcharsbitbeep=P2^0;//蜂鸣器控制口sbittimeadd=P2^1;//调节时间加sbittimesub=P2^2;//调节时间减sbittimeclk=P2^3;//闹钟设定键sbittimefun=P2^4;//时间设定键sbitlcdrs=P2^5;//液晶的命令和数据控制口sbitledrw=P2^6;//液晶的写数据口sbitlcden=P2^7;//液晶显示模块的使能端sbittimeds=P3^3;//时钟芯片地址闸sbittimerw=P3^4;//时钟芯片读写sbittimeas=P3^5;//时钟芯片地址闸sbittimeen=P3^6;//时钟芯片片选ucharcodetable[]=20--week;ucharcodetable1[]=::;//要显示的字符串charnum,shi,fen,miao,nian=2000,yue,ri,week,numfun,anumfun,flag,flag1;ucharleap;voiddelay(uintx){uchari,j;for(i=x;i0;i--)for(j=110;j0;j--);}voiddidi(){beep=0;delay(1000);beep=1;delay(1000);}voidwrite_com(ucharcom){lcdrs=0;P0=com;delay(5);lcden=1;delay(20);lcden=0;}voidwrite_date(uchardate){lcdrs=1;P0=date;delay(5);lcden=1;delay(20);lcden=0;}voidwrite_sfm(ucharadd,uchardate){ucharshi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_date(0x30+shi);write_date(0x30+ge);}voidwrite_nyr(ucharadd,uchardate){ucharshi,ge;shi=date/10;ge=date%10;write_com(0x80+add);write_date(0x30+shi);write_date(0x30+ge);}voidwrite_zhou(ucharadd,uchardate){write_com(0x80+add);write_date(0x30+date);}voidwrite_ds(ucharadd,uchardate){timeen=0;timeas=1;timeds=1;timerw=1;P1=add;timeas=0;timerw=0;P1=date;timerw=1;timeas=1;timeen=1;}ucharread_ds(ucharadd){uchards_date;timeas=1;timeds=1;timerw=1;timeen=0;P1=add;timeas=0;timeds=0;P1=0xff;ds_date=P1;timeds=1;timeas=1;timeen=1;returnds_date;}voidkeyscan(){if(flag1==1)//用来关闭闹钟铃声{if(timeadd==0){delay(5);if(timeadd==0){while(!timeadd==0);flag1=0;}}if(timesub==0){delay(5);if(timesub==0){while(!timesub==0);flag1=0;}}}if(timefun==0){delay(5);if(timefun==0){while(!timefun);flag=1;numfun++;if(numfun==1){write_com(0x80+0x40+0x0a);write_com(0x0f);}if(numfun==2){write_com(0x80+0x40+0x07);write_com(0x0f);}if(numfun==3){write_com(0x80+0x40+0x04);write_com(0x0f);}if(numfun==4){write_com(0x80+0x0f);write_com(0x0f);}if(numfun==5){write_com(0x80+0x09);write_com(0x0f);}if(numfun==6){write_com(0x80+0x06);write_com(0x0f);}if(numfun==7){write_com(0x80+0x03);write_com(0x0f);}if(numfun==8){numfun=0;flag=0;}}}if(numfun!=0){if(timeadd==0){delay(5);while(!timeadd);if(numfun==1){miao++;if(miao==60)miao=0;write_sfm(9,miao);write_com(0x80+0x40+9);write_ds(0,miao);}if(numfun==2){fen++;if(fen==60)fen=0;write_sfm(6,fen);write_com(0x80+0x40+6);write_ds(2,fen);}if(numfun==3){shi++;if(shi==24)shi=0;write_sfm(3,shi);write_com(0x80+0x40+3);write_ds(4,shi);}if(numfun==4){week++;if(week==8)week=1;write_zhou(0x0f,week);write_com(0x80+0x0f);write_ds(6,week);}if(numfun==5){if(nian==0){switch(yue){case1:ri++;if(ri==32)ri=1;break;case2:ri++;if(ri==30)ri=1;break;case3:ri++;if(ri==32)ri=1;break;case4:ri++;if(ri==31)ri=1;break;case5:ri++;if(ri==32)ri=1;break;case6:ri++;if(ri==31)ri=1;break;case7:ri++;if(ri==32)ri=1;break;case8:ri++;if(ri==32)ri=1;break;case9:ri++;if(ri==31)ri=1;break;case10:ri++;if(ri==32)ri=1;break;case11:ri++;if(ri==31)ri=1;break;case12:ri++;if(ri==32)ri=1;break;}}elseif(leap&&yue==2){ri++;if(ri==30)ri=1;}elseif(yue==2&&leap==0){ri++;if(ri==29)ri=1;}elseswitch(yue){case1:ri++;if(ri==32)ri=1;break;case3:ri++;if(ri==32)ri=1;break;case4:ri++;if(ri==31)ri=1;break;case5:ri++;if(ri==32)ri=1;break;case6:ri++;if(ri==31)ri=1;break;case7:ri++;if(ri==32)ri=1;break;case8:ri++;if(ri==32)ri=1;break;case9:ri++;if(ri==31)ri=1;break;case10:ri++;if(ri==32)ri=1;break;case11:ri++;if(ri==31)ri=1;break;case12:ri++;if(ri==32)ri=1;break;}write_nyr(8,ri);write_com(0x80+0x08);write_ds(7,ri);}if(numfun==6){yue++;if(yue==13)yue=0;write_nyr(5,yue);write_com(0x80+0x05);write_ds(8,yue);}if(numfun==7){nian++;if(nian==100)nian=0;leap=nian%4==0&&nian%100!=0;write_nyr(2,nian);write_com(0x80+2);write_ds(9,nian);}}if(timesub==0){delay(5);while(!timesub);if(numfun==1){miao--;if(miao==-1)miao=59;write_sfm(9,miao);write_com(0x80+0x40+9);write_ds(0,miao);}if(numfun==2){fen--;if(fen==-1)fen=59;write_sfm(6,fen);write_com(0x80+0x40+6);write_ds(2,fen);}if(numfun==3){shi--;if(shi==-1)shi=23;write_sfm(3,shi);write_com(0x80+0x40+3);write_ds(4,shi);}if(numfun==4){week--;if(week==0)week=7;write_zhou(0x0f,week);write_com(0x80+0x0f);write_ds(6,week);}if(numfun==5){if(nian==0){switch(yue){case1:ri--;if(ri==0)ri=31;break;case2:ri--;if(ri==0)ri=29;break;case3:ri--;if(ri==0)ri=31;break;case4:ri--;if(ri==0)ri=30;break;case5:ri--;if(ri==0)ri=31;break;case6:ri--;if(ri==0)ri=30;break;case7:ri--;if(ri==0)ri=31;break;case8:ri--;if(ri==0)ri=31;break;case9:ri--;if(ri==0)ri=30;break;case10:ri--;if(ri==0)ri=31;break;case11:ri--;if(ri==0)ri=30;break;case12:ri--;if(ri==0)ri=31;break;}}elseif(leap&&yue==2){ri--;if(ri==0)ri=29;}elseif(yue==2&&leap==0){ri--;if(ri==0)ri=28;}elseswitch(yue){case1:ri--;if(ri==0)ri=31;break;case3:ri--;if(ri==0)ri=31;break;case4:ri--;if(ri==0)ri=30;break;case5:ri--;if(ri==0)ri=31;break;case6:ri--;if(ri==0)ri=30;break;case7:ri--;if(ri==0)ri=31;break;case8:ri--;if(ri==0)ri=31;break;case9:ri--;if(ri==0)ri=30;break;case10:ri--