1302驱动代码
时间:10-02
整理:3721RD
点击:
#ifndef _1302_H__ //防止重载
#define _1302_H__
#include <reg51.h> //包含头文件
#define uchar unsigned char
#define uint unsigned int
#define ds1302_second 0x80
#define ds1302_minute 0x82
#define ds1302_hour 0x84
#define ds1302_day 0x86
#define ds1302_week 0x8a
#define ds1302_month 0x88
#define ds1302_year 0x8c
/**端口定义**/
sbit IO=P1^6;
sbit CLK=P1^7;
sbit RST=P1^5;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
uchar datastring[13]; ////存放时间的数组
typedef unsigned char U8; // defined for unsigned 8-bits integer variable 无符号8位整型变量
typedef unsigned int U16; // defined for unsigned 16-bits integer variable 无符号16位整型变量 */
U8 U8FLAG;
U8 U8temp;
U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8 U8comdata;
typedef struct
{
uchar second;
uchar minute;
uchar hour;
uchar day;
uchar week;
uchar month;
uchar year;
}systemtime;//定义的时间的类型
void ds1302_write(uchar dat)
{
uchar i,j;
j=dat;
for(i=0;i<8;i++)
{
IO=j&0x01;
CLK=1;
CLK=0;
j>>=1;
}
}
/**实时时钟读出一个字节**/
uchar ds1302_read()
{
uchar i;
for(i=0;i<8;i++)
{
ACC=ACC>>1;
ACC7=IO;
CLK=1;
CLK=0;
}
return(ACC);
}
/**写入数据**/
void dat_write(uchar addr,uchar dat) //addr为要写数据的地址,dat为要写的数据
{
RST=0;
CLK=0;
RST=1;
ds1302_write(addr);
ds1302_write(dat);
CLK=1;
RST=0;
}
/**读出数据**/
uchar dat_read(uchar addr)
{
uchar dat;
RST=0;
CLK=0;
RST=1;
ds1302_write(addr|0x01);
dat=ds1302_read();
CLK=1;
RST=0;
return(dat);
}
/**是否写保护**/
void ds1302_protect(bit flag)
{
if(flag)
dat_write(0x8e,0x10); //禁止写
else
dat_write(0x8e,0x00); //允许写
}
/**时间设置**/
void settime(uchar addr,uchar value)
{
ds1302_protect(0);
dat_write(addr,value);
ds1302_protect(1);
}
/**时间增减设置**/
void set(uchar adr,bit flag)
{
uchar dat;
dat=dat_read(adr);
if(flag)
dat_write(adr,dat+1);
else
dat_write(adr,dat-1);
}
/**12/24小时时间设置**/
void day_set(bit flag) //flag为1为12时制,为0则为24小时制
{
uchar hour;
hour=(dat_read(0x85)&0x7f); //保留小时寄存器中原有的时间值
ds1302_protect(0) ;
if(flag)
{
dat_write(0x84,0x80|hour);
}
else
{
dat_write(0x84,0x00|hour);
}
ds1302_protect(1);
}
/**取时间**/
void gettime(systemtime *time)
{
uchar readvalue;
readvalue=dat_read(ds1302_second);
time->second=((readvalue&0x70)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_minute);
time->minute=((readvalue&0x70)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_hour);
time->hour=((readvalue&0x30)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_day);
time->day=((readvalue&0x30)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_week);
time->week=(readvalue&0x07);
readvalue=dat_read(ds1302_month);
time->month=((readvalue&0x01)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_year);
time->year=((readvalue&0xf0)>>4)*10+(readvalue&0x0f);
datastring[0]=time->year/10;
datastring[1]=time->year%10;
datastring[2]=time->month/10;
datastring[3]=time->month%10;
datastring[4]=time->day/10;
datastring[5]=time->day%10;
datastring[6]=time->hour/10;
datastring[7]=time->hour%10;
datastring[8]=time->minute/10;
datastring[9]=time->minute%10;
datastring[10]=time->second/10;
datastring[11]=time->second%10;
datastring[12]=time->week;
}
/**初始时间设置为2013-04-08 08时08分08秒**/
void init_time(void)
{
day_set(0);
settime(ds1302_second,0x08);
settime(ds1302_minute,0x08);
settime(ds1302_hour,0x08);
settime(ds1302_day,0x08);
settime(ds1302_month,0x04);
settime(ds1302_year,0x13);
settime(ds1302_week,0x01);
}
/**DS1302初始化**/
void init_ds1302(void)
{
uchar second=dat_read(ds1302_second);
if(second&0x80)
settime(ds1302_second,0);
}
#endif
#define _1302_H__
#include <reg51.h> //包含头文件
#define uchar unsigned char
#define uint unsigned int
#define ds1302_second 0x80
#define ds1302_minute 0x82
#define ds1302_hour 0x84
#define ds1302_day 0x86
#define ds1302_week 0x8a
#define ds1302_month 0x88
#define ds1302_year 0x8c
/**端口定义**/
sbit IO=P1^6;
sbit CLK=P1^7;
sbit RST=P1^5;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
uchar datastring[13]; ////存放时间的数组
typedef unsigned char U8; // defined for unsigned 8-bits integer variable 无符号8位整型变量
typedef unsigned int U16; // defined for unsigned 16-bits integer variable 无符号16位整型变量 */
U8 U8FLAG;
U8 U8temp;
U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8 U8comdata;
typedef struct
{
uchar second;
uchar minute;
uchar hour;
uchar day;
uchar week;
uchar month;
uchar year;
}systemtime;//定义的时间的类型
void ds1302_write(uchar dat)
{
uchar i,j;
j=dat;
for(i=0;i<8;i++)
{
IO=j&0x01;
CLK=1;
CLK=0;
j>>=1;
}
}
/**实时时钟读出一个字节**/
uchar ds1302_read()
{
uchar i;
for(i=0;i<8;i++)
{
ACC=ACC>>1;
ACC7=IO;
CLK=1;
CLK=0;
}
return(ACC);
}
/**写入数据**/
void dat_write(uchar addr,uchar dat) //addr为要写数据的地址,dat为要写的数据
{
RST=0;
CLK=0;
RST=1;
ds1302_write(addr);
ds1302_write(dat);
CLK=1;
RST=0;
}
/**读出数据**/
uchar dat_read(uchar addr)
{
uchar dat;
RST=0;
CLK=0;
RST=1;
ds1302_write(addr|0x01);
dat=ds1302_read();
CLK=1;
RST=0;
return(dat);
}
/**是否写保护**/
void ds1302_protect(bit flag)
{
if(flag)
dat_write(0x8e,0x10); //禁止写
else
dat_write(0x8e,0x00); //允许写
}
/**时间设置**/
void settime(uchar addr,uchar value)
{
ds1302_protect(0);
dat_write(addr,value);
ds1302_protect(1);
}
/**时间增减设置**/
void set(uchar adr,bit flag)
{
uchar dat;
dat=dat_read(adr);
if(flag)
dat_write(adr,dat+1);
else
dat_write(adr,dat-1);
}
/**12/24小时时间设置**/
void day_set(bit flag) //flag为1为12时制,为0则为24小时制
{
uchar hour;
hour=(dat_read(0x85)&0x7f); //保留小时寄存器中原有的时间值
ds1302_protect(0) ;
if(flag)
{
dat_write(0x84,0x80|hour);
}
else
{
dat_write(0x84,0x00|hour);
}
ds1302_protect(1);
}
/**取时间**/
void gettime(systemtime *time)
{
uchar readvalue;
readvalue=dat_read(ds1302_second);
time->second=((readvalue&0x70)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_minute);
time->minute=((readvalue&0x70)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_hour);
time->hour=((readvalue&0x30)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_day);
time->day=((readvalue&0x30)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_week);
time->week=(readvalue&0x07);
readvalue=dat_read(ds1302_month);
time->month=((readvalue&0x01)>>4)*10+(readvalue&0x0f);
readvalue=dat_read(ds1302_year);
time->year=((readvalue&0xf0)>>4)*10+(readvalue&0x0f);
datastring[0]=time->year/10;
datastring[1]=time->year%10;
datastring[2]=time->month/10;
datastring[3]=time->month%10;
datastring[4]=time->day/10;
datastring[5]=time->day%10;
datastring[6]=time->hour/10;
datastring[7]=time->hour%10;
datastring[8]=time->minute/10;
datastring[9]=time->minute%10;
datastring[10]=time->second/10;
datastring[11]=time->second%10;
datastring[12]=time->week;
}
/**初始时间设置为2013-04-08 08时08分08秒**/
void init_time(void)
{
day_set(0);
settime(ds1302_second,0x08);
settime(ds1302_minute,0x08);
settime(ds1302_hour,0x08);
settime(ds1302_day,0x08);
settime(ds1302_month,0x04);
settime(ds1302_year,0x13);
settime(ds1302_week,0x01);
}
/**DS1302初始化**/
void init_ds1302(void)
{
uchar second=dat_read(ds1302_second);
if(second&0x80)
settime(ds1302_second,0);
}
#endif