小柳给大家谈谈时钟芯片ds1302电路图,以及ds1302时钟芯片工作原理图应用的知识点,希望对你所遇到的问题有所帮助。
1、由于不知道你是用什么单片机写的,我就写下了DS1302的程序,楼主可复制修改就可以了,不是很难,楼主记得给分哦typedef struct SYSTEMTIME{uchar Second;uchar Minute;uchar Hour;uchar Week;uchar Day;uchar Month;uchar Year;uchar DateString[9];uchar TimeString[9];}SYSTEMTIME; //定义的时间类型sbit DS1302_RST=P1^3; //DS1302复位线引脚sbit DS1302_CLK=P1^4; //DS1302时钟线引脚sbit DS1302_IO=P1^5; //DS1302数据线引脚/**************DS1302一个写字节******************/void DS1302InputByte(uchar d) //实时时钟写入一字节(内部函数){uchar i;ACC=d;for (i=8;i>0;i--){DS1302_IO=ACC0; //相当于汇编中的 RRCDS1302_CLK=1;DS1302_CLK=0; //发一个高跳变到低的脉冲ACC=ACC>>1;}}/**************DS1302读一个字节******************/uchar DS1302OutputByte(void) //实时时钟读取一字节(内部函数){uchar i;for (i=8;i>0;i--){ACC=ACC>>1; //相当于汇编中的 RRCACC7=DS1302_IO;DS1302_CLK=1;DS1302_CLK=0; //发一个高跳变到低的脉冲}return(ACC);}/****************DS1302写数据********************/void Write_1302(uchar ucAddr, uchar ucDa){DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr); //地址,命令DS1302InputByte(ucDa); //写1Byte数据DS1302_CLK=1;DS1302_RST=0;}/****************DS1302读数据********************/uchar Read1302(uchar ucAddr) //读取DS1302某地址的数据{uchar ucData;DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1; //enableDS1302InputByte(ucAddr|0x01); //地址,命令ucData=DS1302OutputByte(); //读1Byte数据DS1302_CLK=1;DS1302_RST=0;return(ucData);}/**************DS1302写保护设定******************/void DS1302_SetProtect(bit flag) //是否写保护{if (flag)Write_1302(0x8E,0x10); //WP=1,不能写入elseWrite_1302(0x8E,0x00); //WP=0,可以写入}/**************DS1302设置时间函数****************/void DS1302_SetTime(uchar Address, uchar Value){DS1302_SetProtect(0);Write_1302(Address, ((Value/10)}/*****************DS1302初始化******************/void Init_DS1302(void){uchar Second=Read1302(DS1302_SECOND);if (Second&0x80) //如果第七为1(表明没有启动), 则启动时钟DS1302_SetTime(DS1302_SECOND,0);}/**************DS1302时间转换函数***************/void DS1302_GetTime(SYSTEMTIME *Time){uchar ReadValue;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); //转换年}。
本文到这结束,希望上面文章对大家有所帮助。