//--------------------------------------------------------------------<br />#define uchar unsigned char<br />#define uint unsigned int<br />#define success 1<br />#define failure 0<br /><br />sbit ACC0 = ACC^0;<br />sbit ACC7 = ACC^7;<br />typedef struct {<br /> unsigned char second;<br /> unsigned char minute;<br /> unsigned char hour;<br /> unsigned char date;<br /> unsigned char month;<br /> unsigned char week;<br /> unsigned char year;<br /> } TIME;<br />extern TIME idata g_time;<br />/*********************************************************************/<br />// 当前时间格式为: 秒 分 时 日 月 星期 年<br />uchar code time_init[]={0x40,0x04,0x16,0x09,0x02,0x04,0x06};<br />//--------------------------------------------------------------------<br />void init_ds1302(void)<br />{<br /> T_RST = 0;<br /> T_CLK = 0;<br /> T_RST = 1;<br />}<br />//--------------------------------------------------------------------<br />void stop_ds1302(void)<br />{<br /> T_CLK = 1;<br /> T_RST = 0;<br />}<br />//********************************************************************<br />void write_byte_ds1302(unsigned char l_data) // 1100us<br />{<br /> uchar i;<br /> ACC = l_data;<br /> for(i=8; i>0; i--)<br /> {<br /> T_IO = ACC0;<br /> T_CLK = 1;<br /> T_CLK = 0;<br /> ACC = ACC >> 1; // 2us<br /> }<br />}<br />//********************************************************************<br />unsigned char read_byte_ds1302(void)<br />{<br /> uchar i;<br /> for(i=8; i>0; i--)<br /> {<br /> ACC = ACC >> 1;<br /> if(ACC&0x01) // 2us<br /> ACC |= 0x80; // 1us<br /> ACC7 = T_IO; // 3us<br /> T_CLK = 1;<br /> T_CLK = 0;<br /> }<br /> return ACC;<br />}<br />//-----------------------------------------------------------<br />void cancle_ds1302_wp(void)<br />{<br /> init_ds1302();<br /> write_byte_ds1302(0x8e); // 地址,命令数?<br /> write_byte_ds1302(0x00); // 写1Byte?<br /> stop_ds1302();<br />}<br />//-----------------------------------------------------------<br />void set_ds1302_wp(void)<br />{<br /> init_ds1302();<br /> write_byte_ds1302(0x8e); // 地址,命令<br /> write_byte_ds1302(0x80); // 写1Byte数据<br /> stop_ds1302();<br />}<br />//********************************************************************<br />void read_time_ds1302_burst(void)<br />{<br /> init_ds1302();<br /> write_byte_ds1302(0xbf); // 0xbf:时钟多字节读命令<br /> g_time.second = read_byte_ds1302();<br /> g_time.minute = read_byte_ds1302();<br /> g_time.hour = read_byte_ds1302();<br /> g_time.date = read_byte_ds1302();<br /> g_time.month = read_byte_ds1302();<br /> g_time.week = read_byte_ds1302();<br /> g_time.year = read_byte_ds1302();<br /> stop_ds1302();<br />}<br />/********************************************************************<br />* 说明: 先写地址,后写数据(时钟多字节方式)<br />* 输入: pSecDa: 时钟数据地址 格式为: 秒 分 时 日 月 星期 年 控制<br />* 7Byte (BCD码) + 1Byte(控制字)<br />***********************************************************************/<br />void set_time_ds1302_burst(void *pSecDa)<br />{<br /> uchar i;<br /> cancle_ds1302_wp();<br /> init_ds1302();<br /> write_byte_ds1302(0xbe); // 0xbe:时钟多字节写命令<br /> for(i=0; i<8; i++)<br /> {<br /> write_byte_ds1302(*(uchar*)pSecDa);<br /> ((uchar*)pSecDa)++;<br /> }<br /> stop_ds1302();<br />}<br />/********************************************************************<br />* 说明: 先写地址,后写命令/数据?<br />* Address: 0<-->31<br />* Command = (1 1 x x x x x 0)0xc0|(address<<1)<br />***********************************************************************/<br />void write_one_byte_ds1302(unsigned char Address, unsigned char Data)<br />{<br /> cancle_ds1302_wp();<br /> init_ds1302();<br /> write_byte_ds1302(0xc0|(Address<<1)); /* 写地址,命令 */<br /> write_byte_ds1302(Data); /* 写1Byte数据*/<br /> stop_ds1302();<br /> set_ds1302_wp();<br />}<br />/********************************************************************<br />* 说明: 先写地址,后读命令/数据<br />* Address: 0<-->31<br />* 1 1 x x x x x 1<br />* Command = (1 1 x x x x x 1)0xc1|(address<<1)<br />***********************************************************************/<br />unsigned char read_one_byte_ds1302(unsigned char Address)<br />{<br /> unsigned char rtn_value;<br /> if(Address>30)<br /> Address -= 30;<br /> init_ds1302();<br /> write_byte_ds1302(0xc1|(Address<<1)); /* 写地址,命令 */<br /> rtn_value = read_byte_ds1302(); /* 读1Byte数据 */<br /> stop_ds1302();<br /> return(rtn_value);<br />}<br />/********************************************************<br />* 说明: 先写地址,后读命令/数据 涓流充电]<br />* command 0x90(write) 0x91(read)<br />* tcs ds ds rs rs<br />* (1 0 1 0) 0 1 0 1 0xa5 Imax = (5-0.7)/2k = 2.2MA<br />***********************************************************************/<br />//void trickle_ds1302(unsigned char ds, unsigned char rs)<br />void trickle_ds1302(void)<br />{<br /> volatile uchar i;<br /> cancle_ds1302_wp();<br /> init_ds1302();<br /> i=0;<br /> write_byte_ds1302(0x90); /* 写地址,命令 */<br /> //write_byte_ds1302(0xa0|((ds&0x03)<<2)|(rs&0x03)); /* 读1Byte数据 */<br /> i = 1;<br /> write_byte_ds1302(0xa5); /* 读1Byte数据 */<br /> i = 2;<br /> stop_ds1302();<br /> set_ds1302_wp();<br />}<br />/**********************************************************************<br />* 说明: 先写地址,后读命令/数据 涓流充电]<br />* Imax = (5-0.7)/2k = 2.2MA<br />***********************************************************************/<br />bit initiate_ds1302(void)<br />{<br /> unsigned char value;<br /> value = read_one_byte_ds1302(29);<br /> if(value == 0xaa)<br /> {<br /> value = read_one_byte_ds1302(30);<br /> if(value == 0x55)<br /> return (success);<br /> }<br /> cancle_ds1302_wp();<br /> init_ds1302();<br /> write_byte_ds1302(0x80); // start clock (1)<br /> write_byte_ds1302(0x00);<br /> write_byte_ds1302(0x84); // set pm (2)<br /> write_byte_ds1302(0x00);<br /> write_byte_ds1302(0x90); // set trickle (3)<br /> write_byte_ds1302(0xa5);<br /> stop_ds1302();<br /> set_ds1302_wp();<br /> set_time_ds1302_burst(time_init);// initinal time (4)<br /> write_one_byte_ds1302(29,0xaa);<br /> write_one_byte_ds1302(30,0x55);<br /> value = read_one_byte_ds1302(29);<br /> if(value == 0xaa)<br /> {<br /> value = read_one_byte_ds1302(30);<br /> if(value == 0x55)<br /> return (success);<br /> else<br /> return (failure);<br /> }<br /> else<br /> return (failure);<br />}<br />//---------------------------------------------------------------------<br /><br />修改时间,我调用的是write_one_byte_ds1302(unsigned char Address, unsigned char Data);不知道对不对,如果我想修改年份 Address 应该是多少?实在不解,还望各位大虾指教! |
|