您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 基于DS1302和89C52的6位共阳数码管可调日历时钟闹钟秒表器(有整点报时)
HC#includereg51.h//头文件#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitDATA=P1^1;//位定义1302芯片的接口,数据输出端定义在P1.1引脚sbitRST=P1^2;//位定义1302芯片的接口,复位端口定义在P1.1引脚sbitSCLK=P1^0;//位定义1302芯片的接口,时钟输出端口定义在P1.1引脚sbitK1=P3^0;sbitK2=P3^1;sbitK6=P3^2;sbitK4=P3^5;sbitK5=P3^6;sbitK7=P3^3;sbitK3=P3^4;sbitK8=P3^7;sbitALARM_BEEP=P1^3;ucharkedaoru=0,qw1=0;ucharsecond2,minute2,hour2,second3,minute3,hour3,second4,minute4,hour4,second5,minute5,hour5,second6,minute6,hour6,second7,minute7,hour7;ucharhour1=100;uchare=1;uchartmp=0;ucharqw=0;ucharg,f,p,q,h,m,second1,minute1,year1=1,month1,day1,jilu;uchardisbuffer[12];disbuffer1[6];uchardisplaytable[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};ucharchocode[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};ucharsecond,minute,hour,year,month,day,flag,flag1,naozhong,miaobiao=0;voiddelaynus(ucharn){uchari;for(i=0;in;i++);}voiddelayms(ucharn){uchari;while(n--)HCfor(i=0;i120;i++);}voiddelay(uinte){uintm;for(m=0;me;m++){}}voidwrite1302(uchardat){uchari;SCLK=0;//拉低SCLK,为脉冲上升沿写入数据做好准备delaynus(2);//稍微等待,使硬件做好准备for(i=0;i8;i++)//连续写8个二进制位数据{DATA=dat&0x01;//取出dat的第0位数据写入1302delaynus(2);//稍微等待,使硬件做好准备SCLK=1;//上升沿写入数据delaynus(2);//稍微等待,使硬件做好准备SCLK=0;//重新拉低SCLK,形成脉冲dat=1;//将dat的各数据位右移1位,准备写入下一个数据位}}voidwriteset1302(ucharCmd,uchardat){RST=0;//禁止数据传递SCLK=0;//确保写数居前SCLK被拉低RST=1;//启动数据传输delaynus(2);//稍微等待,使硬件做好准备write1302(Cmd);//写入命令字write1302(dat);//写数据SCLK=1;//将时钟电平置于已知状态RST=0;//禁止数据传递}unsignedcharread1302(void){uchari,dat;delaynus(2);//稍微等待,使硬件做好准备for(i=0;i8;i++)//连续读8个二进制位数据{dat=1;//将dat的各数据位右移1位,因为先读出的是字节的最低位if(DATA==1)//如果读出的数据是1dat|=0x80;//将1取出,写在dat的最高位SCLK=1;//将SCLK置于高电平,为下降沿读出delaynus(2);//稍微等待SCLK=0;//拉低SCLK,形成脉冲下降沿HCdelaynus(2);//稍微等待}returndat;//将读出的数据返回}ucharreadset1302(ucharCmd){unsignedchardat;RST=0;//拉低RST178/192SCLK=0;//确保写数居前SCLK被拉低RST=1;//启动数据传输write1302(Cmd);//写入命令字dat=read1302();//读出数据SCLK=1;//将时钟电平置于已知状态RST=0;//禁止数据传递returndat;//将读出的数据返回}voidsetprotect(bitflag)//设置保护{if(flag==1)writeset1302(0x8e,0x80);//写入保护指令elsewriteset1302(0x8e,0x00);//写入不保护指令}voidstoptime(bitflag)//是否将时钟停止{uchardat;dat=readset1302(0x81);setprotect(0);if(flag)writeset1302(0x80,dat|0x80);elsewriteset1302(0x80,dat&0x7f);}voidinit_ds1302(void){setprotect(0);//根据写状态寄存器命令字,写入不保护指令writeset1302(0x80,((45/10)4|(45%10)));//根据写秒寄存器命令字,写入秒的初始值writeset1302(0x82,((59/10)4|(59%10)));//根据写分寄存器命令字,写入分的初始值writeset1302(0x84,((8/10)4|(8%10)));//根据写小时寄存器命令字,写入小时的初始值writeset1302(0x86,((5/10)4|(5%10)));//riwriteset1302(0x88,((12/10)4|(12%10)));//yuewriteset1302(0x8c,((13/10)4|(13%10)));//nianHC}voiddisplay1(void)//调时{if(miaobiao==0){if(K2==0){delay(10);if(K2==0){f++;delay(10);if(f==7){f=1;}while(!K2)delay(10);switch(f){case1:flag=1;e=1;kedaoru=1;miaobiao=0;break;case2:kedaoru=1;break;case3:kedaoru=1;break;case4:e=2;kedaoru=1;break;case5:kedaoru=1;break;case6:kedaoru=1;break;default:break;}}}}if(kedaoru==1){if(K6==0){delay(10);if(K6==0){delay(10);while(!K6)delay(10);switch(f){case1:second++;if(second==60)second=0;break;case2:minute++;if(minute==60)minute=0;break;HCcase3:hour++;if(hour==24)hour=0;break;case4:day++;if(day==32)day=1;break;case5:month++;if(month==13)month=1;break;case6:year++;if(year==100)year=0;break;default:break;}}}}if(kedaoru==1){if(K7==0){delay(10);if(K7==0){delay(10);while(!K7)delay(10);switch(f){case1:second--;if(second==-1)second=59;break;case2:minute--;if(minute==-1)minute=59;break;case3:hour--;if(hour==-1)hour=23;break;case4:day--;if(day==0)day=31;break;case5:month--;if(month==0)month=12;break;case6:year--;if(year==-1)year=99;break;default:break;}}}}if((K3==0)&&(kedaoru==1))//保存闹钟{second1=second;minute1=minute;hour1=hour;year1=year;month1=month;day1=day;delay(10);HCflag=0;f=0;naozhong=0;kedaoru=0;return;}if((K4==0)&&(kedaoru==1))//保存时间{delay(10);setprotect(0);//根据写状态寄存器命令字,写入不保护指令writeset1302(0x80,((second/10)4|(second%10)));//根据写秒寄存器命令字,写入秒的值writeset1302(0x82,((minute/10)4|(minute%10)));//根据写分寄存器命令字,写入分的值writeset1302(0x84,((hour/10)4|(hour%10)));//根据写小时寄存器命令字,写入小时的值writeset1302(0x86,((day/10)4|(day%10)));//riwriteset1302(0x88,((month/10)4|(month%10)));//yuewriteset1302(0x8c,((year/10)4|(year%10)));flag=0;f=0;kedaoru=0;return;}if(kedaoru==0){if(K8==0){delay(10);if(K8==0){q++;delay(10);if(q==5){q=1;}while(!K8)delay(10);}}}switch(q){case1:flag=1;e=1;second=0;minute=0;hour=0;EA=1;miaobiao=1;kedaoru=0;jilu=0;break;case2:e=1;TR0=1;kedaoru=0;miaobiao=1;//启动break;case3:e=1;TR0=0;kedaoru=0;miaobiao=1;//暂停break;case4:e=1;q=2;kedaoru=0;miaobiao=1;break;default:break;}HCif((miaobiao==1)&&(K3==0)){second=0;minute=0;hour=0;EA=0;q=1;}if((miaobiao==1)&&(jilu==0)){if(K6==0){delay(10);if(K6==0);{h++;delay(10);if(h==6){h=1;}while(!K6)switch(h){case1:second2=second;minute2=minute;hour2=hour;break;case2:second3=second;minute3=minute;hour3=hour;break;case3:second4=second;minute4=minute;hour4=hour;break;case4:second5=second;minute5=minute;hour5=hour;break;case5:q=3;jilu=1;q=1;break;default:break;}}}}if(miaobiao==1){if(K7==0){delay(10);if(K7==0);HC{m++;delay(10);if(m==5){m=1;}while(!K7)switc
本文标题:基于DS1302和89C52的6位共阳数码管可调日历时钟闹钟秒表器(有整点报时)
链接地址:https://www.777doc.com/doc-2569169 .html