⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds12887_1.h

📁 DS12887驱动程序,用串口助手调试。刚调试过的
💻 H
字号:
//file:	DS12887驱动程序	,用串口助手调试
//time:	2008-2-16
//author:borlittle
//qq:634172943

#ifndef __DS12887_1_H__ 
#define __DS12887_1_H__ 
#define uchar unsigned char		//定义无符号字符为 uchar


//接口定义
sbit DS_AS	=	P2^3;	//地址选择	
sbit DS_RW	=	P2^4;	//读写选择
sbit DS_DS	=	P2^5;	//数据选择 
sfr  DS_DAT =	0x90;	//8位数据线P1口!!
//DS12887 的CS引脚接地



//串口上位机部分
uchar temp;
uchar y1,y2,y3,y4,y5,y6;

//数据地址定义
#define	DS12887_Second		0x00	//秒
#define	DS12887_Minute		0x02	//分
#define	DS12887_Hour		0x04	//时
#define	DS12887_Week		0x06	//星期
#define	DS12887_Day			0x07	//日
#define	DS12887_Month		0x08	//月
#define	DS12887_Year		0x09	//年


//定义时间类型,结构体数据类型
typedef struct _SYSTEMTIME_
{
	unsigned  char Second;
	unsigned  char Minute;
	unsigned  char Hour;
	unsigned  char Week;
	unsigned  char Day;
	unsigned  char Month;
	unsigned  char Year;
	unsigned  char DateString[9];
	unsigned  char TimeString[9];
}SYSTEMTIME;


//函数声明
void DS12887_Init(void);											//DS12887初始化
void DS12887_Write(unsigned char Address, unsigned char Value);		//DS12887指定地址写入数据
unsigned char DS12887_Read(unsigned char Address);					//DS12887指定地址中读出数据	
void DS12887_SetTime(unsigned  char Address, unsigned  char Value);	//设置时间函数
void DS12887_GetTime(SYSTEMTIME *Time);								//读出时间
void DateToStr(SYSTEMTIME *Time);									//日期转化成字符
void TimeToStr(SYSTEMTIME *Time);									//时间转化成字符	


//DS12887指定地址写入数据 ,严格按照INTERL的时序,程序de关键!!!
void DS12887_Write(unsigned char Address, unsigned char Value)
{	
	DS_AS = 0;
	DS_DS = 1;
	DS_RW = 1;
	DS_AS = 1;
	DS_DAT= Address; 
	DS_AS = 0;
	DS_RW = 0;
	DS_DAT= Value;
	DS_RW = 1;
	DS_AS = 1;
}


//DS12887指定地址中读出数据,严格按照INTERL的时序!!!!!	
unsigned char DS12887_Read(unsigned char Address)
{
	unsigned char DS_RDAT;
	DS_AS = 0;
	DS_DS = 1;
	DS_RW = 1;
	DS_AS = 1;
	DS_DAT= Address;
	DS_AS = 0;
	DS_DS = 0;
	DS_DAT=0xff;
	DS_RDAT = DS_DAT;
	DS_DS = 1;
	DS_AS = 1;
	return (DS_RDAT);
}


//DS12887初始化
void DS12887_Init(void)
{
	DS12887_Write(0x0a, 0x20);	//启动DS12887
	DS12887_Write(0x0b, 0x86);	//禁止更新,接下来初始化数据,即写入时间,日期等
	DS12887_Write(0x0b, 0x06);	//正常更新,二进制格式,24进制小时
}


//设置时间函数
void DS12887_SetTime(unsigned  char Address, unsigned  char Value)
{
	DS12887_Write(0x0b, 0x86);	//禁止更新,接下来初始化数据,即写入时间,日期等
	DS12887_Write(Address,Value);
	DS12887_Write(0x0b, 0x06);	//正常更新,二进制格式,24进制小时
}


//读出时间
void DS12887_GetTime(SYSTEMTIME *Time)
{
	Time->Second = DS12887_Read(DS12887_Second);
	Time->Minute = DS12887_Read(DS12887_Minute);
	Time->Hour   = DS12887_Read(DS12887_Hour  );
	Time->Day    = DS12887_Read(DS12887_Day   );
	Time->Week   = DS12887_Read(DS12887_Week  );
	Time->Month  = DS12887_Read(DS12887_Month );
	Time->Year   = DS12887_Read(DS12887_Year  );
	y1=Time->Second;
  y2=Time->Minute;
  y3=Time->Hour;
  y4=Time->Day;
  y5=Time->Month;
  y6=Time->Year;
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -