您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > (绝对正确)单片机使用DS1302设计万年历
#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint#defineAM(X)X#definePM(X)(X+12)#defineDS1302_SECOND0x80#defineDS1302_MINUTE0x82#defineDS1302_HOUR0x84#defineDS1302_WEEK0x8a#defineDS1302_DAY0x86#defineDS1302_MONTH0x88#defineDS1302_YEAR0x8c#defineDS1302_RAM(X)(0xc0+(x)*2)#defineoutP0bitkey_flag1=0;sbitDS1302_CLK=P2^6;sbitDS1302_IO=P2^7;sbitDS1302_RST=P2^5;sbitACC0=ACC^0;sbitACC7=ACC^7;sbitRS=P2^0;sbitRW=P2^1;sbitE=P2^2;ucharsec_add=0,min_add=0,hou_add=0,day_add=0,mon_add=0,yea_add=0,week_add;uintint_time=0;voiddelay(uintj)//1ms延时子程序{uchari=250;for(;j0;j--){while(i--);i=249;while(i--);i=250;}}voidcheck_busy(void){uchardt;do{dt=0xff;E=0;RS=0;RW=1;E=1;dt=out;}while(dt&0x80);E=0;}voidwrite_command(ucharcom){check_busy();E=0;RS=0;RW=0;out=com;E=1;_nop_();E=0;delay(1);}voidwrite_data(uchardat){check_busy();E=0;RS=1;RW=0;out=dat;E=1;_nop_();E=0;delay(1);}voidlcd_initial(void){write_command(0x38);write_command(0x0c);write_command(0x06);write_command(0x01);delay(1);}voidstring(ucharad,uchar*s){write_command(ad);while(*s0){write_data(*s++);}}/*********DS1302********/typedefstructSYSTEM_TIME{ucharSecond;ucharMinute;ucharHour;ucharWeek;ucharDay;ucharMonth;ucharYear;ucharDateString[9];ucharTimeString[9];}SYSTEMTIME;SYSTEMTIMEadjusted;voidDS1302InputByte(uchard)//时钟写入一字节{uchari;ACC=d;for(i=8;i0;i--){DS1302_IO=ACC0;DS1302_CLK=1;DS1302_CLK=0;ACC=ACC1;}}ucharDS1302OutputByte(void)//时钟读取一字节{uchari;for(i=8;i0;i--){ACC=ACC1;ACC7=DS1302_IO;DS1302_CLK=1;DS1302_CLK=0;;}return(ACC);}voidWrite1302(ucharucAddr,ucharucDa){DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr);DS1302InputByte(ucDa);DS1302_CLK=1;DS1302_RST=0;}ucharRead1302(ucharucAddr){ucharucData;DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr|0X01);ucData=DS1302OutputByte();DS1302_CLK=1;DS1302_RST=0;return(ucData);}uchar*DateToBCD(SYSTEMTIME*Time){ucharD[8];D[0]=Time-Second/104+Time-Second%10;D[1]=Time-Minute/104+Time-Minute%10;D[2]=Time-Hour/104+Time-Hour%10;D[5]=Time-Week/104+Time-Week%10;D[4]=Time-Month/104+Time-Month%10;D[3]=Time-Day/104+Time-Day%10;D[6]=Time-Year/104+Time-Year%10;returnD;}voidDS1302_SetProtect(bitflag)//是否写保护{if(flag)Write1302(0x8E,0x80);elseWrite1302(0x8E,0x00);}voidDS1302_SetTime(ucharAddress,ucharvalue)//设置时间函数{DS1302_SetProtect(0);Write1302(Address,((value/10)4|(value%10)));}voidDS1302_GetTime(SYSTEMTIME*Time)//获取时间{ucharReadValue;ReadValue=Read1302(0x81);Time-Second=((ReadValue&0x70)4)*10+(ReadValue&0x0F);//将BCD码转换为十进制数ReadValue=Read1302(0x83);Time-Minute=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(0x85);Time-Hour=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(0x87);Time-Day=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(0x8b);Time-Week=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(0x89);Time-Month=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(0x8d);Time-Year=((ReadValue&0x70)4)*10+(ReadValue&0x0F);}voidDateToStr(SYSTEMTIME*Time)//将日期信息转换为字符串{Time-DateString[0]=Time-Year/10+'0';Time-DateString[1]=Time-Year%10+'0';Time-DateString[2]='-';Time-DateString[3]=Time-Month/10+'0';Time-DateString[4]=Time-Month%10+'0';Time-DateString[5]='-';Time-DateString[6]=Time-Day/10+'0';Time-DateString[7]=Time-Day%10+'0';Time-DateString[8]='\0';}voidTimeToStr(SYSTEMTIME*Time)//将时间信息转换为字符串{Time-TimeString[0]=Time-Hour/10+'0';Time-TimeString[1]=Time-Hour%10+'0';Time-TimeString[2]=':';Time-TimeString[3]=Time-Minute/10+'0';Time-TimeString[4]=Time-Minute%10+'0';Time-TimeString[5]=':';Time-TimeString[6]=Time-Second/10+'0';Time-TimeString[7]=Time-Second%10+'0';Time-TimeString[8]='\0';}voidInitial_DS1302(void){ucharSecond;Second=Read1302(DS1302_SECOND);if(Second&0x80){DS1302_SetTime(DS1302_SECOND,0);}}voidDS1302_TimeStop(bitflag){ucharData;Data=Read1302(DS1302_SECOND);DS1302_SetProtect(0);if(flag)Write1302(DS1302_SECOND,Data|0x80);elseWrite1302(DS1302_SECOND,Data&0x7F);}uchar*WeekToStr(SYSTEMTIMETime){uinti;uchar*z;i=Time.Week;switch(i){case1:z=sun;break;case2:z=mon;break;case3:z=tue;break;case4:z=wen;break;case5:z=thu;break;case6:z=fri;break;case7:z=sat;break;}returnz;}intkey_scan(){inti=0;uinttemp;P1=0xf0;delay(40);temp=P1;if(temp!=0xf0){i=1;}else{i=0;}returni;}ucharkey_value(){uintv[4][3]={'2','1','0','5','4','3','8','7','6','b','a','9'};uinttemp,n=0,m=0;ucharvalue;P1=0xfe;temp=P1;if(temp!=0xfe)m=0;P1=0xfd;temp=P1;if(temp!=0xfd)m=1;P1=0xfb;temp=P1;if(temp!=0xfb)m=2;P1=0xf7;temp=P1;if(temp!=0xf7)m=3;P1=0xef;temp=P1;if(temp!=0xef)n=0;P1=0xdf;temp=P1;if(temp!=0xdf)n=1;P1=0xbf;temp=P1;if(temp!=0xbf)n=2;value=v[m][n];returnvalue;}voidadjust(void){if(key_scan()&&key_flag1)switch(key_value()){case'0':sec_add++;break;case'1':min_add++;break;case'2':hou_add++;break;case'3':day_add++;break;case'4':mon_add++;break;case'5':yea_add++;break;case'6':week_add++;break;default:break;}adjusted.Second+=sec_add;adjusted.Minute+=min_add;adjusted.Hour+=hou_add;adjusted.Day+=day_add;adju
本文标题:(绝对正确)单片机使用DS1302设计万年历
链接地址:https://www.777doc.com/doc-4211564 .html