📄 ds1302new.h
字号:
//本头文件由-独步雨林-修改
//日期:2007-4-11
#ifndef _ds1302_H
#define _ds1302_H
#include<at89x52.h>
#include<intrins.h>
#include <String.h>
#define uchar unsigned char
#define uint unsigned int
sbit reset = P3^7;
sbit sclk = P3^5;
sbit io = P3^6;
#define uchar unsigned char
typedef struct
{
unsigned char Sec;
unsigned char Min;
unsigned char Hour;
unsigned char Month;
unsigned char MonthDay; //月中的日
unsigned char WeekDay; //星期中的日
unsigned char Year;
unsigned char DateStr[11]; //日期字符串
unsigned char TimeStr[8]; //时间字符串
}TDateTime; //定义日期时间结构体
uchar init [] ={0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //
/**********************************************************/
void write_byte(uchar inbyte)
{
uchar i;
for(i=0;i<8;i++)
{
sclk=0; //写的时候低电平改变数据
if(inbyte&0x01)
io=1;
else
io=0;
sclk=1; //写的时候高电平,把数据写入ds1302
_nop_();
inbyte=inbyte>>1;
}
}
/**********************************************************/
uchar read_byte() //sclk的下跳沿读数据
{
uchar i,temp=0,tmp=0;
io=1; //设置为输入口
for(i=0;i<7;i++)
{
sclk=0;
if(io==1)
temp=temp|0x80;
else
temp=temp&0x7f;
sclk=1; //产生下跳沿
temp=temp>>1;
}
tmp=temp;
tmp=(tmp>>4)*10+(tmp &0x0f);
return(tmp);
}
/**********************************************************/
// 往ds1302的某个地址写入数据
/**********************************************************/
void write_ds1302(uchar cmd,uchar indata)
{
sclk=0;
reset=1;
write_byte(cmd);
write_byte(indata);
sclk=0;
reset=0;
}
/**********************************************************/
// 读ds1302某地址的的数据
/**********************************************************/
uchar read_ds1302(uchar addr)
{
uchar backdata;
sclk=0;
reset=1;
write_byte(addr); //先写地址
backdata=read_byte(); //然后读数据
sclk=0;
reset=0;
return (backdata);
}
/**********************************************************/
// 设置初始时间
/**********************************************************/
void set_ds1302(uchar addr,uchar *p,uchar n) //写入n个数据
{
write_ds1302(0x8e,0x00); //写控制字,允许写操作
for(;n>0;n--)
{
write_ds1302(addr,*p);
p++;
addr=addr+2;
}
write_ds1302(0x8e,0x80); //写保护,不允许写
}
/**********************************************************/
// 读取当前时间
/**********************************************************/
void read_nowtime(uchar addr,uchar *p,uchar n)
{
for(;n>0;n--)
{
*p=read_ds1302(addr);
p++;
addr=addr+2;
}
}
/**********************************************************/
// 初始化DS1302
/**********************************************************/
void init_ds1302()
{
reset=0;
sclk=0;
write_ds1302(0x80,0x00);
write_ds1302(0x90,0xa6); //一个二极管+4K电阻充电
write_ds1302(0x8e,0x80); //写保护控制字,禁止写
}
//*****************************************
//函数功能:返回当前时间
//**********************************
void GetCurrentDateTime(TDateTime *DT)
{
unsigned char CurDateTime[7];
read_nowtime(0x81,CurDateTime,7);
DT->Sec=CurDateTime[0];
DT->Min=CurDateTime[1];
DT->Hour=CurDateTime[2];
DT->MonthDay=CurDateTime[3];
DT->Month=CurDateTime[4];
DT->WeekDay=CurDateTime[5];
DT->Year=CurDateTime[6];
strcpy(DT->DateStr,"2007-04-11");
strcpy(DT->TimeStr,"15:00:00");
DT->DateStr[2]=(DT->Year/10)+0x30;
DT->DateStr[3]=(DT->Year%10)+0x30;
DT->DateStr[5]=(DT->Month/10)+0x30;
DT->DateStr[6]=(DT->Month%10)+0x30;
DT->DateStr[8]=(DT->MonthDay/10)+0x30;
DT->DateStr[9]=(DT->MonthDay%10)+0x30;
DT->TimeStr[0]=DT->Hour/10+0x30;
DT->TimeStr[1]=DT->Hour%10+0x30;
DT->TimeStr[3]=DT->Min/10+0x30;
DT->TimeStr[4]=DT->Min%10+0x30;
DT->TimeStr[6]=DT->Sec/10+0x30;
DT->TimeStr[7]=DT->Sec%10+0x30;
}
//设置时间
void SetDateTime(TDateTime *DT)
{
unsigned char Str[7];
Str[0]=DT->Sec/10*16+DT->Sec%10;
Str[1]=DT->Min/10*16+DT->Min%10;
Str[2]=DT->Hour/10*16+DT->Hour%10;
Str[3]=DT->MonthDay/10*16+DT->MonthDay%10;
Str[4]=DT->Month/10*16+DT->Month%10;
Str[5]=DT->WeekDay/10*16+DT->WeekDay%10;
Str[6]=DT->Year/10*16+DT->Year%10;
set_ds1302(0x80,Str,7);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -