#define uint unsigned int
#define uchar unsigned char
sbit SCLK = P2^3 ; //DS1302时钟
sbit DIO = P2^4 ; //DS1302数据
sbit CE = P2^5 ; //DS1302片选
void Ds1302_Write_Byte (uchar addr, uchar dat);
uchar Ds1302_Read_Byte ( uchar addr );
void Ds1302_Write_Time(void);
void Ds1302_Read_Time(void);
void Ds1302_Init(void)
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define WRITE_DAY 0x86
#define WRITE_MONTH 0x88
#define WRITE_WEEK 0x8A
#define WRITE_YEAR 0x8C
#define WRITE_TIMER_FLAG 0xC0
#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define READ_DAY 0x87
#define READ_MONTH 0x89
#define READ_WEEK 0x8B
#define READ_YEAR 0x8D
#define READ_TIMER_FLAG 0xC1
#define WRITE_PROTECT 0x8E
uchar time_buf1[8] = {20,11,8,31,13,35,10,3}; //空年月日时分秒周
uchar time_buf[8]; //空年月日时分秒周
//============================================
//函数名称:void Ds1302_Write_Byte (byte addr, byte dat)
//功能: 串行发送地址、数据,先发低位,且在上升沿发送
//参数传递:有,地址和数据
//返回值: 无
//===========================================
void Ds1302_Write_Byte (uchar addr, uchar dat)
{
uchar i;
//DIOOUT; //数据端口定义为输出
CE = 1;
//写入目标地址:addr
addr = addr & 0xFE; //最低位置零
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
DIO = 1;
}
else
{
DIO = 0;
}
SCLK = 1;
SCLK = 0;
addr = addr >> 1;
}
//写入数据:d
for (i = 0; i < 8; i ++)
{
if (dat & 0x01)
{
DIO = 1;
}
else
{
DIO = 0;
}
SCLK = 1;
SCLK = 0;
dat = dat >> 1;
}
CE = 0;; //停止DS1302总线
}[page]
//===============================================
//函数名称:byte Ds1302_Read_Byte ( byte addr )
//功能: 串行读取数据,先发低位,且在下降沿发送
//参数传递:有,地址
//返回值: 有,读取的数据
//===============================================
uchar Ds1302_Read_Byte ( uchar addr )
{
uchar i;
uchar temp;
CE = 1;
//写入目标地址:addr
addr = addr | 0x01; //最低位置高
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
DIO = 1;
}
else
{
DIO = 0;
}
SCLK = 1;
SCLK = 0;
addr = addr >> 1;
}
//输出数据:temp
//DIOIN; //数据端口定义为输入
for (i = 0; i < 8; i ++)
{
temp = temp >> 1;
if (DIO)
{
temp |= 0x80;
}
else
{
temp &= 0x7F;
}
SCLK = 1;
SCLK = 0;
}
CE = 0; //停止DS1302总线
//DIOOUT; //数据端口定义为输出
return temp;
}
//===============================================
// 向DS1302写入时钟数据
//===============================================
void Ds1302_Write_Time(void)
{
uchar i,tmp;
for(i=0;i<8;i++)
{ //BCD处理
tmp=time_buf1[i]/10;
time_buf[i]=time_buf1[i]%10;
time_buf[i]=time_buf[i]+tmp*16;
}
Ds1302_Write_Byte(WRITE_PROTECT,0x00); //关闭写保护
Ds1302_Write_Byte(WRITE_SECOND,0x80); //暂停
//Ds1302_Write_Byte(ds1302_charger_add,0xa9); //涓流充电
Ds1302_Write_Byte(WRITE_YEAR,time_buf[1]); //年
Ds1302_Write_Byte(WRITE_MONTH,time_buf[2]); //月
Ds1302_Write_Byte(WRITE_DAY,time_buf[3]); //日
Ds1302_Write_Byte(WRITE_HOUR,time_buf[4]); //时
Ds1302_Write_Byte(WRITE_MINUTE,time_buf[5]); //分
Ds1302_Write_Byte(WRITE_SECOND,time_buf[6]); //秒
Ds1302_Write_Byte(WRITE_WEEK,time_buf[7]); //周
Ds1302_Write_Byte(WRITE_PROTECT,0x80); //打开写保护
}
//========================================
// 从DS1302读出时钟数据
//========================================
void Ds1302_Read_Time(void)
{
uchar i,tmp;
time_buf[1]=Ds1302_Read_Byte(READ_YEAR); //年
time_buf[2]=Ds1302_Read_Byte(READ_MONTH); //月
time_buf[3]=Ds1302_Read_Byte(READ_DAY); //日
time_buf[4]=Ds1302_Read_Byte(READ_HOUR); //时
time_buf[5]=Ds1302_Read_Byte(READ_MINUTE); //分
time_buf[6]=(Ds1302_Read_Byte(READ_SECOND))&0x7F; //秒
time_buf[7]=Ds1302_Read_Byte(READ_WEEK); //周
for(i=0;i<8;i++)
{ //BCD处理
tmp=time_buf[i]/16;
time_buf1[i]=time_buf[i]%16;
time_buf1[i]=time_buf1[i]+tmp*10;
}
}
//==========================================
// DS1302初始化
//==========================================
void Ds1302_Init(void)
{
CE = 0; //RST脚置低
SCLK = 0; //SCK脚置低
Ds1302_Write_Byte(WRITE_SECOND,0x00); //开始
}
上一篇:DS18B20和ADC0832共同使用
下一篇:基于C8051F的金鹏LCD19264(带字库)程序
推荐阅读最新更新时间:2024-03-16 14:02