您的位置首页百科问答

ds1302的中文资料谁有,给我上传一份。

ds1302的中文资料谁有,给我上传一份。

的有关信息介绍如下:

ds1302的中文资料谁有,给我上传一份。

#define DS1302_second 0x80#define DS1302_minute 0x82#define DS1302_hour 0x84#define DS1302_day 0x86#define DS1302_week 0x8a#define DS1302_month 0x88#define DS1302_year 0x8c#define DS1302_nsecond 0x80#define DS1302_nminute 0x82#define DS1302_nhour 0x84#define DS_CLK P10 //定义DS1302 时钟输入端口#define DS_DAT P11 //定义DS1302 串行数据输入输出端口(双向)#define DS_RST P12 //定义DS1302 复位/片选端口#define fm T1 uchar DateBuf[8]={2,0,0,0,0,0,0,0};uchar TimeBuf[8]={0,16,0,0,0,0,0,0};/********************************************************函数名称:DS1302单字节读函数功能描述:读取某一地址的数据全局变量:无参数说明:address为地址变量;number为所送的数据返回说明:返回指定地址的数据设 计 人:版 本:说 明:********************************************************/uchar DS1302Read(uchar address){ uchar i; DS_CLK=0; //复位各脚 DS_DAT=0; DS_RST=0; DS_RST=1; //允许读写数据 for (i=8;i>0;i--) //地址输入,8位 { DS_CLK=0; //上升沿数据输入 DS_DAT=address&0x01; //送出一位数据 address>>=1; //右移一位 DS_CLK=1; //上升沿 } for (i=8;i>0;i--) //数据读出 { DS_CLK=0; //下降沿数据读出 address>>=1; //把数据右移 if (DS_DAT) address=address|0x80; //读取数据 DS_CLK=1; DS_CLK=1; } //时钟置高位 DS_CLK=1; //片选无效 DS_RST=0; return(address);}/********************************************************函数名称:DS1302单字节写函数功能描述:读取某一地址的数据全局变量:无参数说明:address为地址变量;number为所送的数据返回说明:返回指定地址的数据设 计 人:版 本:说 明:********************************************************/void DS1302Write(uchar address,uchar number){ uchar i; DS_CLK=0; DS_DAT=0; DS_RST=0; DS_RST=1; /*地址输入*/ for (i=16;i>0;i--) { DS_CLK=0; if (i>8) { DS_DAT=address&0x01; address>>=1; } else { DS_DAT=number&0x01; number>>=1; } DS_CLK=1; } DS_CLK=1; DS_RST=0;}/********************************************************//**是否写保护**/void DS1302_Protect(bit flag){ if(flag) DS1302Write(0x8e,0x80); //禁止写 else DS1302Write(0x8e,0x00); //允许写}/********************************************************//**时间设置**/void Set1302Time(uchar addr,uchar value){ DS1302_Protect(0); DS1302Write(addr,((value/10)<<4|(value%10))); //高4位为十位,低4位为个位 DS1302_Protect(1);}/********************************************************///时间增减设置*void TimeAdj(uchar adr,bit flag){ uchar dat; dat=DS1302Read(adr); if(flag) DS1302Write(adr,dat+1); else DS1302Write(adr,dat-1);}/********************************************************//**12/24小时时间设置**/void DaySet(bit flag) //flag为1为12时制,为0则为24小时制{ uchar hour; hour=(DS1302Read(0x85)&0x7f); //保留小时寄存器中原有的时间值 DS1302_Protect(0) ; if(flag) { DS1302Write(0x84,0x80|hour); } else { DS1302Write(0x84,0x00|hour); } DS1302_Protect(1);}/***************************************************** void GetTime(systemtime *time) { uchar readvalue; readvalue=DS1302Read(DS1302_second+1); time->second=((readvalue&0x70)>>4)*10+(readvalue&0x0f); readvalue=DS1302Read(DS1302_minute+1); time->minute=((readvalue&0x70)>>4)*10+(readvalue&0x0f); readvalue=DS1302Read(DS1302_hour+1); time->hour=((readvalue&0x30)>>4)*10+(readvalue&0x0f); readvalue=DS1302Read(DS1302_day+1); time->day=((readvalue&0x30)>>4)*10+(readvalue&0x0f); readvalue=DS1302Read(DS1302_week+1); time->week=(readvalue&0x07); readvalue=DS1302Read(DS1302_month+1); time->month=((readvalue&0x01)>>4)*10+(readvalue&0x0f); readvalue=DS1302Read(DS1302_year+1); time->year=((readvalue&0xf0)>>4)*10+(readvalue&0x0f); datastring[0]=time->year/2; datastring[1]=time->year%2; datastring[2]=time->month/10; datastring[3]=time->month%10; datastring[4]=time->day/10; datastring[5]=time->day%10; datastring[6]=time->hour/10; datastring[7]=time->hour%10; datastring[8]=time->minute/10; datastring[9]=time->minute%10; datastring[10]=time->second/10; datastring[11]=time->second%10; datastring[12]=time->week; } **/ /********************************************************/void GetDate( ) { DateBuf[2]=DS1302Read(0x8d)/16; DateBuf[3]=DS1302Read(0x8d)%16; DateBuf[4]=DS1302Read(0x89)/16; DateBuf[5]=DS1302Read(0x89)%16; DateBuf[6]=DS1302Read(0x87)/16; DateBuf[7]=DS1302Read(0x87)%16;}/********************************************************/void GetTime( ) { TimeBuf[0]=DS1302Read(0x8b)%16; TimeBuf[2]=DS1302Read(0x85)/16; TimeBuf[3]=DS1302Read(0x85)%16; TimeBuf[4]=DS1302Read(0x83)/16; TimeBuf[5]=DS1302Read(0x83)%16; TimeBuf[6]=DS1302Read(0x81)/16; TimeBuf[7]=DS1302Read(0x81)%16; }/********************************************************//**初始时间设置为2008-08-08 08时08分08秒**/void InitTime(void){ DaySet(0); Set1302Time(DS1302_second,0x00); Set1302Time(DS1302_minute,0x24); Set1302Time(DS1302_hour,0x01); Set1302Time(DS1302_day,0x01d); Set1302Time(DS1302_month,0x04); Set1302Time(DS1302_year,0x09); Set1302Time(DS1302_week,0x05); Set1302Time(DS1302_nsecond,0x00); Set1302Time(DS1302_nminute,0x24); Set1302Time(DS1302_nhour,0x01); }/********************************************************//**DS1302初始化**/void Init_DS1302(void){ uchar second=DS1302Read(DS1302_second+1); if(second&0x80) Set1302Time(DS1302_second,0);}