问答

基于DS1302的51单片机数字时钟程序(可设置时间)。亲~程序借参考下~

提问者:y2787121422013-04-28 00:00

最佳答案

仅供参考,不懂再问我,哈哈…… -------------------------------------------------------------- #include #include #include"clock.h" #include"daima.h" uchar t_hour=0x16; uchar t_min=0x23; uchar t_mon=0x07; uchar t_day=0x09; uchar t_year=0x11; //flag1=0; //引脚定义 sbit T_RST=P1^5; sbit T_CLK=P1^3; sbit T_IO=P1^4; sbit ACC0=ACC^0; sbit ACC7=ACC^7; bit flag1=0; uchar x=0x10,y=0x30; //闹钟初始化 uchar idata t_sec,sec1,sec2; uchar idata mon1,mon2; uchar idata min1,min2; uchar idata hour1,hour2; uchar idata day1,day2; uchar idata year1,year2; uchar week,week0; uchar code week1[]="一"; uchar code week2[]="二"; uchar code week3[]={0xc8,0xfd}; //三的内码失效,该代码等效为“三” uchar code week4[]="四"; uchar code week5[]="五"; uchar code week6[]="六"; uchar code week7[]="日"; /******************************************* 向1302写一个字节 *******************************************/ void input_BYTE(uchar dat) { uchar i; ACC=dat; for(i=8;i>0;i--) { T_IO=ACC0; T_CLK=1; T_CLK=0; ACC=(ACC>>1); } } /******************************************* 1302读出一个字节 *******************************************/ uchar output_BYTE() { uchar i; for(i=8;i>0;i--) { ACC=(ACC>>1); ACC7=T_IO; T_CLK=1; T_CLK=0; } return (ACC); } /******************************************* 写数据 *******************************************/ void write_1302(uchar add,uchar dat) { T_RST=0; T_CLK=0; T_RST=1; input_BYTE(add); input_BYTE(dat); T_CLK=1; T_RST=0; } /******************************************* 读数据 *******************************************/ uchar read_1302(uchar add) { uchar inf; T_RST=0; T_CLK=0; T_RST=1; input_BYTE(add); inf=output_BYTE(); T_CLK=1; T_RST=0; return (inf); } /******************************* 将闹钟时间写入1302寄存器中 *********************************/ /*void write_clock(uchar x,uchar y) { write_1302(0xc0,x); //写闹钟的小时部分 write_1302(0xc2,y); //写闹钟的分钟部分 } */ /******************************* 将闹钟时间从1302寄存器中读出来 *********************************/ /*uchar* read_clock() { uchar *temp; temp[0]=read_1302(0xc1); //读小时 temp[1]=read_1302(0xc3); //读分 return temp; } */ void init_1302() { write_1302(0x8e,0x00);//关闭写保护; write_1302(0x90,0xaa);//设置充电方式; // write_1302(0xc0,x); //将闹钟的小时写入1302 // write_1302(0xc2,y); //将闹钟的分钟写入1302 /* write_1302(0x80,0x00);//秒寄存器初始化; write_1302(0x82,0x23);//分....... write_1302(0x84,0x16);//时....... write_1302(0x86,0x09);//日........ write_1302(0x88,0x07);//月....... write_1302(0x8a,0x06);//星期... write_1302(0x8c,0x11);//年......*/ write_1302(0x8e,0x80);//打开写保护; } void show_1302_time(uchar line,uchar col) { uchar i=0; uchar temp1[11]; t_sec=read_1302(0x81);//读秒 ; sec1=t_sec&0x0f; sec2=(t_sec>>4); t_min=read_1302(0x83);//读分 ; min1=t_min&0x0f; min2=(t_min>>4); t_hour=read_1302(0x85);//读小时 ; hour1=t_hour&0x0f; hour2=(t_hour>>4); temp1[0]=hour2+'0'; temp1[1]=hour1+'0'; temp1[2]=':'; temp1[3]=min2+'0'; temp1[4]=min1+'0'; temp1[5]=':'; temp1[6]=sec2+'0'; temp1[7]=sec1+'0'; temp1[8]='\0'; lcd_pos(line,col); while(temp1[i]!='\0') { lcd_wdat(temp1[i]); i++; } } void show_1302_date(uchar line,uchar col) { uchar i=0; uchar temp[12]; t_year=read_1302(0x8d);//读年 ; year1=t_year&0x0f; year2=(t_year>>4); t_mon=read_1302(0x89);//读月 ; mon1=t_mon&0x0f; mon2=(t_mon>>4); t_day=read_1302(0x87);//读日; day1=t_day&0x0f; day2=(t_day>>4); temp[0]='2'; temp[1]='0'; temp[2]=year2+'0'; temp[3]=year1+'0'; temp[4]='/'; temp[5]=mon2+'0'; temp[6]=mon1+'0'; temp[7]='/'; temp[8]=day2+'0'; temp[9]=day1+'0'; temp[10]='\0'; // temp[11]='\0'; lcd_pos(line,col); while(temp[i]!='\0') { lcd_wdat(temp[i]); i++; } } void show_1302_week(uchar line,uchar col) { uchar i=0; uchar temp1[]="星期"; lcd_pos(line,col); /* week=read_1302(0x8b);//读星期 ; week0=week&0x0f; */ while(temp1[i]!='\0') { lcd_wdat(temp1[i]); i++; } week=read_1302(0x8b);//读星期 ; week0=week&0x0f; lcd_pos(line,col+2); switch(week0) { case 1: for(i=0;i<2;i++) { lcd_wdat(week1[i]); } break; case 2: for(i=0;i<2;i++) { lcd_wdat(week2[i]); } break; case 3: for(i=0;i<2;i++) { lcd_wdat(week3[i]); } break; case 4: for(i=0;i<2;i++) { lcd_wdat(week4[i]); } break; case 5: for(i=0;i<2;i++) { lcd_wdat(week5[i]); } break; case 6: for(i=0;i<2;i++) { lcd_wdat(week6[i]); } break; case 7: for(i=0;i<2;i++) { lcd_wdat(week7[i]); } break; } } /*void beep() { uchar *naozhong; // write_clock(hou,min); naozhong=read_clock(); if(naozhong[0]==t_hour) { beeper=0; } else { beeper=1; } } */ void beep() { t_hour=read_1302(0x85);//读小时 ; t_min=read_1302(0x83);//读分 ; /* if((x==t_hour)&&(y==t_min)) { beeper=0; flag1=1; } */ if((read_1302(0xc1)==t_hour)&&(read_1302(0xc3)==t_min)) { beeper=0; flag1=1; } else { beeper=1; flag1=0; } } /*void show_naozhong(uchar line,uchar col) { uchar temp[6],i=0; uchar *temp1; temp1=read_clock(); temp[0]=(temp1[0]>>4)+'0'; temp[1]=(temp1[0]&0x0f)+'0'; temp[2]=':'; temp[3]=(temp1[1]>>4)+'0'; temp[4]=(temp1[1]&0x0f)+'0'; temp[5]='\0'; lcd_pos(line,col); while(temp[i]!='\0') { lcd_wdat(temp[i]); i++; } } */ void show_naozhong(uchar line,uchar col) { uchar temp[6],i=0; /* temp[0]=(x>>4)+'0'; temp[1]=(x&0x0f)+'0'; temp[2]=':'; temp[3]=(y>>4)+'0'; temp[4]=(y&0x0f)+'0'; temp[5]='\0'; lcd_pos(line,col); while(temp[i]!='\0') { lcd_wdat(temp[i]); i++; } */ temp[0]=(read_1302(0xc1)>>4)+'0'; temp[1]=(read_1302(0xc1)&0x0f)+'0'; temp[2]=':'; temp[3]=(read_1302(0xc3)>>4)+'0'; temp[4]=(read_1302(0xc3)&0x0f)+'0'; temp[5]='\0'; lcd_pos(line,col); while(temp[i]!='\0') { lcd_wdat(temp[i]); i++; } }

回答者:sfdsag2016-04-28 00:00

DS 5相关问题

相关阅读

DS 5频道

报价:21.99-34.59
级别:中型车
排量:1.6T 1.8T 
变速箱:-

车友关注

最新标签

按字母分类:
ABCDEFGHIJKLMNOPQRSTWXYZ0-9