本人呕心沥血制作的一个基于ds12c887的电子钟的详细教程,程序已经在protues中仿真,仿真图在下方!!花了血本制作的,我花了将近一个月时间探索出来的电子钟(其间断断续续编),在这里给同学们一个好的案例,这个程序由用51,1602,ds12c887,4个按键,一个喇叭。下面还介绍了总线的画法,以及实际中连接编程时的重要注意事项,附上了时序图,对程序的解释很是详细,看了好的帮我顶下,谢谢大家!------------------------血顶猎鹰AD[0..7]LCDRSLCDENDSCSDSASDSRWDSDSS1S2S3INT1S4ALERSTX1X2VCCGNDGNDDSASDSRWDSDSDSCSGNDLCDENVCCGNDLCDRSGNDVCCS2S4S3VCCP23S1P23INT1AD0AD1AD2AD3AD4AD5AD6AD7AD0AD1AD2AD3AD4AD5AD6AD7AD7AD6AD5AD4AD3AD2AD1AD0XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U0AT89C51RESET18SQW23AS14R/W15DS17CS13IRQ19MOT1AD[0..7]VCC24GND12U7DS12C887D714D613D512D411D310D29D18D07E6RW5RS4VSS1VDD2VEE3234567891S2S4BUTTONS3BUTTONLS1SOUNDERS1R15kC130pFR25kS1是功能选择键,按下这个键后再按s2,s3可以对时钟进行调整,s4是闹铃设置键,按下后按s2,s3可以调整闹铃时间。仿真时有一个小bug,当按下闹铃后再按s1,再退出时得再重新按一遍闹铃才可以正常运行,估计是软件的原因。我用的是7.7的,所以有这个芯片,这是我从论坛上找的版本低的解决办法,大家可以看下仿真软件如果版本低可以这样将库文件和dll文件复制到7.5SP3的library和models,仿真时,每次一定要给寄存器初始化,否则时钟不能正常运行.正式硬件运行时仅初次化时使用一下,将以下内容屏蔽后重新编译,再下载程序到89C51.//初始化DS12C887工作方式write_ds(0x0A,0x20);//打开振荡器write_ds(0x0B,0x26);//设置24小时模式,数据二进制格式,开启闹铃中断set_time();//设置上电默认时间,调试的时候用对这三个进行屏蔽,还有把settime()这个子函数屏蔽掉仿真软件中bus总线画法如下,拖出总线,点lbl给总线标上AD[0..7],再标细线时有没发现框里多了这些标号相同的电气连接相同,好了,去连吧。下面是程序,主要参照郭天祥那本书的,关键地方加了很详细的注释#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedcharsbitlcdrs=P2^4;sbitlcden=P2^6;sbits1=P3^0;//功能键sbits2=P3^1;//增大键sbits3=P3^2;//减小键sbits4=P3^6;//闹铃查看键sbitbeep=P2^3;//蜂鸣器sbitdscs=P1^4;sbitdsas=P1^5;sbitdsrw=P1^6;sbitdsds=P1^7;sbitdsirq=P3^3;//中断bitflag1,flag_ri;//定义标志位flag1设置闹铃ucharcount,s1num,flag,t0_num;charmiao,shi,fen,year,month,day,week,amiao,afen,ashi;charcodetable[]=20--;//20--ucharcodetable1[]=::;//::voidwrite_ds(uchar,uchar);ucharread_ds(uchar);voidset_time();voidread_alarm();voidset_alarm(uchar,uchar,uchar);voiddelayms(uintz)//延时毫秒的程序{uintx,y;for(x=z;x0;x--)for(y=112;y0;y--);}voiddi(){beep=0;delayms(100);beep=1;}voidwrite_cmd(ucharcmd){lcden=0;lcdrs=0;P0=cmd;_nop_();lcden=1;delayms(1);lcden=0;_nop_();}voidwrite_dat(uchardat){lcden=0;lcdrs=1;P0=dat;_nop_();lcden=1;delayms(1);lcden=0;_nop_();}voidlcdint(){lcden=0;write_cmd(0x38);//显示模式设置write_cmd(0x0c);//开显示,不显示光标,不闪烁write_cmd(0x06);//写一个字符后地址加1,屏幕不移write_cmd(0x01);//清屏delayms(1);}voidinit(){ucharnum;EA=1;//开总中断EX1=1;//开外部中断1IT1=1;//外部中断下降沿触发flag1=0;t0_num=0;s1num=0;week=1;write_ds(0x0A,0x20);//打开振荡器write_ds(0x0B,0x26);//设置24小时模式,数据二进制格式,开启闹铃中断set_time();//设置上电默认时间,调试的时候用lcdint();write_cmd(0x80);for(num=0;num15;num++){write_dat(table[num]);}write_cmd(0xc0);for(num=0;num11;num++){write_dat(table1[num]);}}voidwrite_sfm(ucharadd,chardate)//写时间,2位一起写{charshi,ge;shi=date/10;ge=date%10;write_cmd(0xc0+add);write_dat(0x30+shi);write_dat(0x30+ge);}voidwrite_nyr(ucharadd,chardate)//年月日{charshi,ge;shi=date/10;ge=date%10;write_cmd(0x80+add);write_dat(0x30+shi);write_dat(0x30+ge);}voidwrite_week(charwe)//星期{write_cmd(0x80+12);switch(we){case1:write_dat('M');write_dat('O');write_dat('N');break;case2:write_dat('T');write_dat('U');write_dat('E');break;case3:write_dat('W');write_dat('E');write_dat('D');break;case4:write_dat('T');write_dat('H');write_dat('U');break;case5:write_dat('F');write_dat('R');write_dat('I');break;case6:write_dat('S');write_dat('A');write_dat('T');break;case7:write_dat('S');write_dat('U');write_dat('N');break;}}voidkeyscan(){if(flag_ri==1){if((s1==0)||(s2==0)||(s3==0)||(s4==0))//按任意键取消闹钟报警{delayms(5);if((s1==0)||(s2==0)||(s3==0)||(s4==0)){while(!(s1&&s2&&s3&&s4));di();flag_ri=0;}}}if(s1==0)//如果功能按键1按下{delayms(5);//去抖动if(s1==0){while(!s1);//等待松手s1num++;//记下次数di();//蜂鸣器响if(flag1==1)//设置闹铃为1时才对s1num调整,只调秒,分,时{if(s1num3){s1num=1;}}flag=1;//在调整时间时标志位flag为1,不进行正常操作switch(s1num){case1:write_cmd(0xc0+10);write_cmd(0x0f);break;//按一下秒钟指针显示闪烁case2:write_cmd(0xc0+7);break;case3:write_cmd(0xc0+4);break;case4:write_cmd(0x80+12);break;case5:write_cmd(0x80+9);break;case6:write_cmd(0x80+6);break;case7:write_cmd(0x80+3);break;default://用default可以解决s1num跑飞s1num=0;write_cmd(0x0c);//按了8次关闪烁,flag=0,退出调整,显示时间flag=0;write_ds(0,miao);//将调整后的时间写入ds12c887write_ds(2,fen);write_ds(4,shi);write_ds(6,week);write_ds(7,day);write_ds(8,month);write_ds(9,year);break;}}}if(s1num!=0)//S1有按下过,检测S2,S3{if(s2==0)//如果按键2按下执行加{delayms(5);if(s2==0){while(!s2);di();switch(s1num){case1:miao++;if(miao59){miao=0;}write_sfm(10,miao);write_cmd(0x80+0x40+10);break;case2:fen++;if(fen59){fen=0;}write_sfm(7,fen);write_cmd(0x80+0x40+7);break;case3:shi++;if(shi23){shi=0;}write_sfm(4,shi);write_cmd(0x80+0x40+4);break;case4:week++;if(week7){week=1;}write_week(week);write_cmd(0x80+12);break;case5:day++;if(day31){day=1;}write_nyr(9,day);write_cmd(0x80+9);break;case6:month++;if(month12){month=1;}write_nyr(6,month);write_cmd(0x80+6);break;case7:year++;if(year99){year=0;}write_nyr(3,year);write_cmd(0x80+3);break;}}}if(s3==0){delayms(5);if(s3==0){while(!s3);di(