|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2012-7-4 11:28:44
|
显示全部楼层
本帖最后由 yangkuan85988 于 2012-7-4 11:30 编辑
void write_byte_DS1302(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCL_DS1302=0;
IO_DS1302=dat&0X01;
dat>>=1;
SCL_DS1302=1;
_nop_();
}
}
unsigned char read_byte_DS1302()
{
unsigned char i,temp;
for(i=0;i<8;i++)
{
SCL_DS1302=0;
temp>>=1;
temp=IO_DS1302;
SCL_DS1302=1;
}
return temp;
}
void write_DS1302(unsigned char command,unsigned char date)
{
RST_DS1302=0;
_nop_();
SCL_DS1302=0;
_nop_();
IO_DS1302=1;
_nop_();
RST_DS1302=1;
_nop_();
write_byte_DS1302(command);
write_byte_DS1302(date);
RST_DS1302=0;
_nop_();
}
unsigned char read_DS1302(unsigned char command)
{
unsigned char temp1,temp2,temp;
RST_DS1302=0;
_nop_();
RST_DS1302=1;
_nop_();
SCL_DS1302=0;
_nop_();
IO_DS1302=1;
_nop_();
write_byte_DS1302(command);
temp1=read_byte_DS1302();
RST_DS1302=0;
_nop_();
temp2=temp1/16;
temp1=temp1%16; //数据格式转换
temp=temp2*10+temp1;
return temp;
}
void init_DS1302()
{
RST_DS1302=0;
write_DS1302(PROTECT,0X00); //去除写保护
write_DS1302(WRITE_YEAR,0X12); //初始化年 2012年
write_DS1302(WRITE_WEEK,0X03); //初始化星期 星期三
write_DS1302(WRITE_MONTH,0X07); //初始化月 7月
write_DS1302(WRITE_DAY,0X04); //初始化天 4号
write_DS1302(WRITE_HOUR,0X08); //初始化小时 8点
write_DS1302(WRITE_MINUTE,0X17); //初始化分钟 17分
write_DS1302(WRITE_SECOND,0X00); //初始化秒钟 0秒
write_DS1302(PROTECT,0X80); //写保护
}
void read_time()
{
unsigned char second,minute,hour,week,day,month,year;
write_DS1302(PROTECT,0X00); //去除写保护
year =read_DS1302(READ_YEAR);
month =read_DS1302(READ_MONTH);
day =read_DS1302(READ_DAY);
week =read_DS1302(READ_WEEK);
hour =read_DS1302(READ_HOUR);
minute=read_DS1302(READ_MINUTE);
second=read_DS1302(READ_SECOND);
write_DS1302(PROTECT,0X80); //写保护
table_time1[2]=year/10;
table_time1[3]=year%10;
table_time1[5]=month/10;
table_time1[6]=month%10;
table_time1[8]=day/10;
table_time1[9]=day%10;
table_time1[13]=week;
table_time2[0]=hour/10;
table_time2[1]=hour%10;
table_time2[3]=minute/10;
table_time2[4]=minute%10;
table_time2[6]=second/10;
table_time2[7]=second%10;
} |
|