1电子设计竞赛报告题目:使用DS12C887时钟芯片设计高精度时钟学部信息科学与工程学部专业电气一类学号2009480102姓名田海茂2011年3月12日装订线河北大学工商学院2目录一摘要…………………………………………………3二系统组成与功能说明………………………………3三硬件电路设计与分析…………………………………2四程序流程说明…………………………………………3附录1……………………………………………………….3摘要本系统利用单片机(STC89C52)和DS12C887时钟芯片实现对时间的控制。在1602液晶上显示年,月,日,星期,时,分,秒,并且按秒实时更新显示;具有闹铃报警功能;具有更改功能,能任意更改时间;利用DS12C887自身掉电可继续走时的特性,实现断电时间不停,再次上电时时间仍准确显示在液晶上的功能。以上各种功能依据一定的联系,组成一个比较完整的系统。一系统组成与功能说明系统可划分为几个功能模块,如图1所示。图1通过STC89C52单片机按键的扫描函数实现对更改时间功能的控制。在外部断电情况下DS12C887仍能继续走时,再次上电时将准确时间传给单片机,从而实现时间不停的功能,当闹钟时间到时,DS12C887发出中断申请,单片机控制液晶停止走时,通过蜂鸣器发出闹铃声,并且按键与蜂鸣器相连,按下按键的同时蜂鸣器会发出响声。采用个性化的液晶显示,当开始供电时以屏幕的平移为起始,时间信息从右往左平移至中央。二硬件电路设计与分析硬件电路原理图如下所示单片机STC89C52DS12C887按键蜂鸣器液晶屏4最小系统图51602液晶与单片机连接图DS12C887与单片机连接图6⑴以STC89C52为核心制作出单片机最小系统。⑵1602液晶为5V电压驱动,带背光,可显示两行,每行16个字符,符合本作品需要。⑶DS12C887具有闹铃以及外部掉电自身继续走时功能,为理想的时钟芯片。⑷选择蜂鸣器,通过用三极管对其放大,实现所想要的功能。⑸选择RS232,使得计算机的9.6V的电压能够与该系统5V得以匹配,与此同时也实现了我们直接用电脑直接对其系统直接拷贝程序。三程序流程说明图主程序流程图附件1#includereg52.h#defineucharunsignedchar#defineuintunsignedint初始化按键检查从DS12C887读取数据液晶显示数据7sbitrs=P3^5;sbitlcden=P3^4;sbits1=P3^6;//功能键sbits2=P3^1;//增大键sbits3=P3^2;//减小键sbitbeep=P2^3;//蜂鸣器sbitdscs=P1^4;sbitdsas=P1^5;sbitdsrw=P1^6;sbitdsds=P1^7;sbitdsirq=P3^3;ucharcount,s1num,flag;bitflag1;charmiao,shi,fen,nian,yue,ri,week;ucharcodetable[]=20--;//液晶固定显示ucharcodetable1[]=::;ucharcodetable2[]=Helloeveryone!;ucharcodetable3[]=Nicetomeetyou!;voidwrite_ds(uchar,uchar);voidset_alarm(uchar,uchar,uchar);ucharread_ds(uchar);voidset_time();ucharamiao,ashi,afen;8voiddelay(uintz)//延时函数{uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voiddi()//按键时蜂鸣器发音{beep=0;delay(100);beep=1;}voiddidi()//闹钟时蜂鸣器发音{beep=0;delay(50);beep=1;delay(100);beep=0;delay(50);beep=1;}9voidwrite_com(ucharcom)//写液晶命令函数{rs=0;lcden=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;}voidwrite_date(uchardate)//写液晶具体数据函数{rs=1;lcden=0;P0=date;delay(5);lcden=1;delay(5);lcden=0;}10voidinit()//程序初始化{ucharnum;EA=1;//打开总中断EX1=1;//开外部中断1IT1=1;//设置负跳变沿触发方式lcden=0;week=1;//set_time();//给DS12C887设置时间函数set_alarm(2,2,10);//设置闹铃函数write_ds(0x0B,0x26);//设置DS12C88724小时模式write_com(0x38);//1602液晶初始化write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80);for(num=0;num16;num++)//写入液晶平移内容{write_date(table2[num]);delay(5);}11write_com(0x80+0x40);for(num=0;num16;num++){write_date(table3[num]);delay(5);}write_com(0x80+0x10);//写入液晶固定内容for(num=0;num16;num++){write_date(table[num]);delay(5);}write_com(0x80+0x50);for(num=0;num16;num++){write_date(table1[num]);delay(5);}}voidwrite_sfm(ucharadd,uchardate)//在液晶上写入时分秒{12ucharshi,ge;shi=date/10;ge=date%10;write_com(0x80+0x50+add);write_date(0x30+shi);write_date(0x30+ge);}voidwrite_sfm1(ucharadd,uchardate)//在液晶上写入年月日{ucharshi,ge;shi=date/10;ge=date%10;write_com(0x80+0x10+add);write_date(0x30+shi);write_date(0x30+ge);}voidwrite_week(ucharwe)//星期函数{write_com(0x80+0x10+13);switch(we){case1:13write_date('M');delay(5);write_date('O');delay(5);write_date('N');break;case2:write_date('T');delay(5);write_date('U');delay(5);write_date('E');break;case3:write_date('W');delay(5);write_date('E');delay(5);write_date('D');break;case4:write_date('T');delay(5);write_date('H');delay(5);write_date('U');break;case5:write_date('F');delay(5);write_date('R');delay(5);14write_date('I');break;case6:write_date('S');delay(5);write_date('A');delay(5);write_date('T');break;case7:write_date('S');delay(5);write_date('U');delay(5);write_date('N');break;}}voidkeyscan(){if(flag1==1)//闹铃响{if(s2==0){delay(5);if(s2==0)15{while(!s2);di();flag1=0;//中断闹铃}}if(s3==0){delay(5);if(s3==0){while(!s3);di();flag1=0;//中断闹铃}}}if(s1==0){16delay(5);if(s1==0){s1num++;flag=1;flag1=0;while(!s1);di();if(s1num==1){write_com(0x80+0x50+10);write_com(0x0f);//光标停止位置不断闪烁}if(s1num==2){write_com(0x80+0x50+7);}if(s1num==3){write_com(0x80+0x50+4);}17if(s1num==4){write_com(0x80+0x10+13);}if(s1num==5){write_com(0x80+0x10+10);}if(s1num==6){write_com(0x80+0x10+7);}if(s1num==7){write_com(0x80+0x10+4);}if(s1num==8){s1num=0;write_com(0x0c);//光标停止闪烁write_ds(0,miao);//将此时时间信息写入DS12C887write_ds(2,fen);18write_ds(4,shi);write_ds(6,week);write_ds(7,ri);write_ds(8,yue);write_ds(9,nian);flag=0;}}}if(s1num!=0){if(s2==0){delay(1);if(s2==0){while(!s2);di();if(s1num==1){miao++;19if(miao==60)miao=0;write_sfm(10,miao);//将变化后的时间写入液晶write_com(0x80+0x50+10);}if(s1num==2){fen++;if(fen==60)fen=0;write_sfm(7,fen);write_com(0x80+0x50+7);}if(s1num==3){shi++;if(shi==24)shi=0;write_sfm(4,shi);write_com(0x80+0x50+4);20}if(s1num==4){week++;if(week==8)week=1;write_week(week);write_com(0x80+0x10+13);}if(s1num==5){ri++;if(ri==32)ri=1;write_sfm1(10,ri);write_com(0x80+0x10+10);}if(s1num==6){yue++;if(yue==13)yue=1;21write_sfm1(7,yue);write_com(0x80+0x10+7);}if(s1num==7){nian++;if(nian==100)nian=0;write_sfm1(4,nian);write_com(0x80+0x10+4);}}}if(s3==0){delay(1);if(s3==0){while(!s3);di();if(s1num==1)22{miao--;if(miao==-1)miao=59;write_sfm(10,miao);write_com(0x80+0x50+10);}if(s1num==2){fen--;if(fen==-1)fen=59;write_sfm(7,fen);write_com(0x80+0x50+7);}if(s1num==3){shi--;if(shi==-1)shi=23;write_sfm(4,shi);write_com(0x80+0x50+4);23}if(s1num==4){week--;i