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