#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedchar#defineDateP3//数码管数据口#definecomP1sbitDS1302_CLK=P1^7;//实时时钟时钟线引脚sbitDS1302_IO=P2^6;//实时时钟数据线引脚sbitDS1302_RST=P1^6;//实时时钟复位线引脚sbitACC0=ACC^0;sbitACC7=ACC^7;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};charhide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year;//秒,分,时到日,月,年位闪的计数sbitSet=P2^0;//模式切换键sbitUp=P2^1;//加法按钮sbitDown=P2^2;//减法按钮sbitout=P2^3;//立刻跳出调整模式按钮sbitdi=P2^4;//蜂鸣器chardone,count,temp,flag,up_flag,down_flag;ucharTempBuffer[5],week_value[2];voidshow_time();//显示程序/************************功能:延时,延时时间大概为ms。**********************/voiddelayms(intx){inti,j;for(i=110;i0;i--)for(j=x;j0;j--);}voidbeep(){di=1;delayms(400);di=0;}/**************************名称:数码管显示*************************/xianshi(ucharshi1,ucharshi2,ucharfen1,ucharfen2,ucharmiao1,ucharmiao2){Date=table[shi1];com=0xfe;delayms(10);Date=table[shi2];com=0xfd;delayms(10);Date=table[fen1];com=0xfb;delayms(10);Date=table[fen2];com=0xf7;delayms(10);Date=table[miao1];com=0xef;delayms(10);Date=table[miao2];com=0xdf;delayms(10);}/***********1602液晶显示部分子程序*********/sbitLcdRs=P2^5;sbitLcdRw=P2^6;sbitLcdEn=P2^7;sfrDBPort=0x80;//内部等待函数****************unsignedcharLCD_Wait(void){LcdRs=0;LcdRw=1;_nop_();LcdEn=1;_nop_();LcdEn=0;returnDBPort;}//向LCD写入命令或数据******************#defineLCD_COMMAND0//Command#defineLCD_DATA1//Data#defineLCD_CLEAR_SCREEN0x01//清屏#defineLCD_HOMING0x02//光标返回原点voidLCD_Write(bitstyle,unsignedcharinput){LcdEn=0;LcdRs=style;LcdRw=0;_nop_();DBPort=input;_nop_();//注意顺序LcdEn=1;_nop_();//注意顺序LcdEn=0;_nop_();LCD_Wait();}//设置显示模式***********************#defineLCD_SHOW0x04//显示开#defineLCD_HIDE0x00//显示关#defineLCD_CURSOR0x02//显示光标#defineLCD_NO_CURSOR0x00//无光标#defineLCD_FLASH0x01//光标闪动#defineLCD_NO_FLASH0x00//光标不闪动voidLCD_SetDisplay(unsignedcharDisplayMode){LCD_Write(LCD_COMMAND,0x08|DisplayMode);}//设置输入模式****************************#defineLCD_AC_UP0x02#defineLCD_AC_DOWN0x00//default#defineLCD_MOVE0x01//画面可平移#defineLCD_NO_MOVE0x00//defaultvoidLCD_SetInput(unsignedcharInputMode){LCD_Write(LCD_COMMAND,0x04|InputMode);}//初始化LCD*************************voidLCD_Initial(){LcdEn=0;LCD_Write(LCD_COMMAND,0x38);//8位数据端口,2行显示,5*7点阵LCD_Write(LCD_COMMAND,0x38);LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR);//开启显示,无光标LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN);//清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE);//AC递增,画面不动}//液晶字符输入的位置************************voidGotoXY(unsignedcharx,unsignedchary){if(y==0)LCD_Write(LCD_COMMAND,0x80|x);if(y==1)LCD_Write(LCD_COMMAND,0x80|(x-0x40));}//将字符输出到液晶显示voidPrint(unsignedchar*str){while(*str!='\0'){LCD_Write(LCD_DATA,*str);str++;}}/***********DS1302时钟部分子程序*********/typedefstruct__SYSTEMTIME__{unsignedcharSecond;unsignedcharMinute;unsignedcharHour;unsignedcharWeek;unsignedcharDay;unsignedcharMonth;unsignedcharYear;unsignedcharDateString[11];unsignedcharTimeString[9];}SYSTEMTIME;//定义的时间类型SYSTEMTIMECurrentTime;#defineAM(X)X#definePM(X)(X+12)//转成24小时制#defineDS1302_SECOND0x80//时钟芯片的寄存器位置,存放时间#defineDS1302_MINUTE0x82#defineDS1302_HOUR0x84#defineDS1302_WEEK0x8A#defineDS1302_DAY0x86#defineDS1302_MONTH0x88#defineDS1302_YEAR0x8CvoidDS1302InputByte(unsignedchard){//实时时钟写入一字节(内部函数)unsignedchari;ACC=d;for(i=8;i0;i--){DS1302_IO=ACC0;//相当于汇编中的RRCDS1302_CLK=1;DS1302_CLK=0;ACC=ACC1;}}unsignedcharDS1302OutputByte(void){//实时时钟读取一字节(内部函数)unsignedchari;for(i=8;i0;i--){ACC=ACC1;//相当于汇编中的RRCACC7=DS1302_IO;DS1302_CLK=1;DS1302_CLK=0;}return(ACC);}voidWrite1302(unsignedcharucAddr,unsignedcharucDa){DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr);//地址,命令DS1302InputByte(ucDa);//写1Byte数据DS1302_CLK=1;DS1302_RST=0;}unsignedcharRead1302(unsignedcharucAddr){//读取DS1302某地址的数据unsignedcharucData;DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr|0x01);//地址,命令ucData=DS1302OutputByte();//读1Byte数据DS1302_CLK=1;DS1302_RST=0;return(ucData);}voidDS1302_GetTime(SYSTEMTIME*Time){//获取时钟芯片的时钟数据到自定义的结构型数组unsignedcharReadValue;ReadValue=Read1302(DS1302_SECOND);Time-Second=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(DS1302_MINUTE);Time-Minute=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(DS1302_HOUR);Time-Hour=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(DS1302_DAY);Time-Day=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(DS1302_WEEK);Time-Week=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(DS1302_MONTH);Time-Month=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=Read1302(DS1302_YEAR);Time-Year=((ReadValue&0x70)4)*10+(ReadValue&0x0F);}voidDateToStr(SYSTEMTIME*Time){//将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[]if(hide_year2){Time-DateString[0]='2';Time-DateString[1]='0';Time-DateString[2]=Time-Year/10+'0';Time-DateString[3]=Time-Year%10+'0';}else{Time-DateString[0]='';Time-DateString[1]='';Time-DateString[2]='';Time-DateString[3]='';}Time-DateString[4]='/';if(hide_month2){Time-DateString[5]=Time-Month/10+'0';Time-DateString[6]=Time-Month%10+'0';}else{Time-Date