sbit rst=P2^0;
sbit io=P2^1;
sbit sclk=P2^2;
//--------常数宏-----------//
#define second_write 0x80
#define minute_write 0x82
#define hour_wirte 0x84
#define day_write 0x86
#define week_write 0x8A
#define month_write 0x88
#define year_write 0x8c
#define second_read 0x81
#define minute_read 0x83
#define hour_read 0x85
#define day_read 0x87
#define week_read 0x8b
#define month_read 0x89
#define year_read 0x8d
//--------------操作宏---------//
#define sclk_high sclk=1;
#define sclk_low sclk=0;
#define io_high io=1;
#define io_low io=0;
#define io_read io
#define rst_high rst=1;
#define rst_low rst=0;
//--------保存时间数据结构体--------
struct
{
unsigned int Second;
unsigned int Minute;
unsigned int Hour;
unsigned int Day;
unsigned int Week;
unsigned int Month;
unsigned int Year;
}
CurrentTime;
/***************************
*Function static void ds1302write(unsigned Content)*
*unsigned content 要写的字节
*****************************************/
static void ds1302_write(unsigned int Content)
{
unsigned int i=8;
for(;i>0;i--)
{
if(Content&0x01)
{
io_high
}
else
{
io_low
}
Content>>=1;
sclk_high
sclk_low
}
}
/************************************/
static unsigned ds1302_read(void)
{
unsigned int i,value;
io_high
for(i=8;i>0;i--)
{
value>>=1;
if(io_read)
{
value|=0x80;
}
else
{
value&=0x7f;
}
sclk_high
sclk_low
}
return value;
}
/***********************************
* function :unsingned 1302writebyte(unsigned int addess)
Description:从DS1302指定的地址独处一个字节的内容
addess:数据地址
*************************************/
unsigned int 1302write_byte(unsigned int addess)
{
unsigned int readvalue;
rst_low
sclk_low;
rst_high
ds1302_wirte(addess);
readvalue=1302_read();
rst_low
sclk_high
return readvalue;
}
/***********************************************
*function void clock_init(void);
*Description:初始化1302时钟寄存器的值
*
***************************************************/
void clock_init(void)
{
if(1302read_byte(0xc1)!=0xf0)
{
1302write_byte(0x8e,0x00)//允许写操作
1302write_byte(year_write,0x08);//年
1302write_byte(week_write,0x04);//星期
1302write_byte(month_write,0x12);//月
1302write_byte(day_write,0x011);//日
1302write_byte(hour_write,0x13);//时
1302write_byte(minute_write,0x06);//分
1302write_byte(second_write,0x04);//秒
1302write_byte(0x90,0xa5); //充电
1302write_byte(0xc0,0xf0); //判断是否初始化
1302write_byte(0x8e,0x80); //禁止写操作
}
}
void clock_updata(void)
{
CurrentTime.Second=1302read_byte(second_read);
CurrentTime.Minute=1302read_byte(minute_read);
CurrentTime.Hour=1302read_byte(hour_read);
CurrentTime.Day=1302read_byte(day_read);
CurrentTime.Month=1302read_byte(month_read);
CurrentTime.Week=1302read_byte(week_read);
CurrentTime.Year=1302read_byte(year_read);
}
|