📄 ds1302.h
字号:
#ifndef _DS1302
#define _DS1302
#define uchar unsigned char
/*********************DS1302 端口设置**********************/
sbit SCLK=P2^5; //DS1302时钟
sbit IO=P2^6; //DS1302 IO
sbit RST = P2^7; //DS1302复位
/*********************读写寄存器地址***********************/
code unsigned char write_addr[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
code unsigned char read_addr[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
/******************************************************/
/* 函数名称:DS1302_Write_Byte(unsigned char temp)
/* 功 能:向DS1302写入一个字节
/* 入口参数:temp
/* 出口参数:无
/******************************************************/
void DS1302_Write_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++) //循环8次 写入数据
{
SCLK=0;
IO=temp&0x01; //每次传输低字节
temp>>=1; //右移一位
SCLK=1;
}
}
/******************************************************/
/* 函数名称:DS1302_Write( unsigned char address,unsigned char dat )
/* 功 能:向DS1302写入数据
/* 入口参数:address,dat
/* 出口参数:无
/******************************************************/
void DS1302_Write( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCLK=0;
_nop_();
RST=1;
_nop_(); //启动
DS1302_Write_Byte(address); //发送地址
DS1302_Write_Byte(dat); //发送数据
RST=0; //恢复
}
/******************************************************/
/* 函数名称:DS1302_Read ( unsigned char address )
/* 功 能:向DS1302读取数据
/* 入口参数:address
/* 出口参数:temp
/******************************************************/
unsigned char DS1302_Read ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCLK=0;
_nop_();
RST=1;
_nop_();
DS1302_Write_Byte(address);
for (i=0;i<8;i++) //循环8次读取数据
{
if(IO)
temp|=0x80; //每次传输低字节
SCLK=0;
temp>>=1; //右移一位
SCLK=1;
}
RST=0;
_nop_(); //以下为DS1302复位的稳定时间
RST=0;
SCLK=0;
_nop_();
SCLK=1;
_nop_();
IO=0;
_nop_();
IO=1;
_nop_();
return (temp);
}
/******************************************************/
/* 函数名称:DS1302_Fresh(uchar *clock_time)
/* 功 能:DS1302时间刷新
/* 入口参数:clock_time
/* 出口参数:clock_time
/******************************************************/
void DS1302_Fresh(uchar *clock_time)
{
unsigned char i,*p;
p=read_addr; //地址传递
for(i=0;i<7;i++) //分7次读取秒分时日月周年
{
clock_time[i]=DS1302_Read(*p);
p++;
}
}
/******************************************************/
/* 函数名称:DS1302_Init(uchar *clock_time)
/* 功 能:DS1302时间初始化
/* 入口参数:clock_time
/* 出口参数:clock_time
/******************************************************/
void DS1302_Init(uchar *clock_time)
{
unsigned char i,*p,tmp;
for(i=0;i<7;i++)
{
tmp=clock_time[i]/10; //BCD处理
clock_time[i]=clock_time[i]%10;
clock_time[i]=clock_time[i]+tmp*16;
}
DS1302_Write(0x8E,0X00);
p=write_addr; //传地址
for(i=0;i<7;i++) //7次写入 秒分时日月周年
{
DS1302_Write(*p,clock_time[i]);
p++;
}
DS1302_Write(0x8E,0x80);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -