/****************************************************** #include < reg52.h> DS1302时钟程序 *******************************************************/sbit ACC_0 = ACC^0;sbit ACC_1 = ACC^1;sbit ACC_2 = ACC^2;sbit ACC_3 = ACC^3;sbit ACC_4 = ACC^4;sbit ACC_5 = ACC^5;sbit ACC_6 = ACC^6;sbit ACC_7 = ACC^7; //**************************sbit T_SCLK = P3^5; // DS1302时钟信号 7脚sbit T_DIO= P3^6; // DS1302数据信号 6脚sbit T_CE = P3^7;//**************************uchar sec,min,hour,day,month,week,year; //时间变量//**************************uchar DS1302_addr[]={ 0x80, //0,写入秒(Second)寄存器 0x81, //1,读出秒(Second)寄存器 0x82, //2,写入分(Minute)寄存器 0x83, //3,读出分(Minute)寄存器 0x84, //4,写入小时(Hour)寄存器 0x85, //5,读出小时(Hour)寄存器 0x86, //6,写入日(Day)寄存器 0x87, //7,读出日(Day)寄存器 0x88, //8,写入月份(Month)寄存器 0x89, //9,读出月份(Month)寄存器 0x8a, //10,写入周(Week)寄存器 0x8b, //11,读出周(Week)寄存器 0x8c, //12,写入年份(Year)寄存器 0x8d, //13,读出年份(Year)寄存器 0x8e, //14,写保护_寄存器 0x8f //15,涓流充电} ;//***************写入一字节***********************void DS1302_Input_Byte(uchar Input) //向时钟IC写入一字节{ uchar i; ACC =Input; for(i=8; i>0; i--) { T_DIO = ACC_0; //相当于汇编中的 RRC T_SCLK = 1; T_SCLK = 0; ACC = ACC >> 1; } }//**************读取一字节()*************************uchar DS1302_Output_Byte(void) //从时钟IC读取一字节(){ uchar i; for(i=8; i>0; i--) { ACC>>=1; T_DIO= 1; ACC_7 = T_DIO; T_SCLK = 1; //相当于汇编中的 RRC T_SCLK = 0; } return(ACC); }//**************向时钟IC写数据*************************void DS1302_Write_one( uchar addr,dat ) // 写入地址、数据子程序 { T_CE=0; //T_CE引脚为低,数据传送中止 T_SCLK=0; //清零时钟总线 T_CE = 1; //T_CE引脚为高,逻辑控制有效 DS1302_Input_Byte(addr); // 地址,命令 DS1302_Input_Byte(dat); // 写1Byte数据 T_SCLK = 1; T_CE = 0; }//************从数据读取数据*****************************uchar DS1302_Read ( uchar addr ) //数据读取子程序{ uchar date; T_CE=0; T_SCLK=0; T_CE = 1; DS1302_Input_Byte(addr); // 地址,命令 date = DS1302_Output_Byte(); // 读1Byte数据 T_SCLK = 1; T_CE = 0; return(date);}//************从数据读取数据****************************void DS1302_Write( uchar sec_w,min_w,hour_w,day_w,month_w,week_w,year_w ){ DS1302_Write_one(0x8e,0x00); DS1302_Write_one(0x80,sec_w); DS1302_Write_one(0x82,min_w); DS1302_Write_one(0x84,hour_w); DS1302_Write_one(0x86,day_w); DS1302_Write_one(0x88,month_w); DS1302_Write_one(0x8a,week_w); DS1302_Write_one(0x8c,year_w); DS1302_Write_one(0x8e,0x80);} //************从数据读取数据*****************************/*void DS1302_readtime() { sec=DS1302_Read(0x81); //读秒 min=DS1302_Read(0x83); //读分 hour=DS1302_Read(0x85); //读时 day=DS1302_Read(0x87); //读日 month=DS1302_Read(0x89); //读月 year=DS1302_Read(0x8d); //读年 week=DS1302_Read(0x8b); //读星期}*/ //************数码管DS1302显示*****************************void DS1302_SMG_display(){ /* DS1302_readtime(); SMG_display(6,sec%0x10); delay(1); SMG_display(5,sec/0x10); delay(1); SMG_display(4,min%0x10); delay(1); SMG_display(3,min/0x10); delay(1); SMG_display(2,hour%0x10); delay(1); SMG_display(1,hour/0x10); delay(1); *///////////////////////////////////////////// uchar i=0,a=6,*p; p=DS1302_addr; *p++; for(i=0;i<3;i++) { SMG_display(a,DS1302_Read(*p)%0x10); a--; delay(0); SMG_display(a,DS1302_Read(*p)/0x10); delay(0); a--; *p++; *p++; } delay(0); }//注意:有些单片机,需将T_DIO信号脚接个上拉 ,否会出现数据读出错误.
-电子元器件采购网(www.ruidan.com)是本土元器件目录分销商,采用“小批量、现货、样品”销售模式,致力于满足客户多型号、高质量、快速交付的采购需求。 自建高效智能仓储,拥有自营库存超过50,000种,提供一站式正品现货采购、个性化解决方案、选型替代等多元化服务。