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

📄 ds1307.c

📁 ds1307 driver by c clock chip
💻 C
字号:
#include "ds1307.h"
#include "viic.h"
#include <string.h>



extern uchar g_valuearr[8];

//读写任意位置ram
uchar Ds1307ReadRam(uchar addr,uchar *Buffer,uchar Length);
uchar Ds1307WriteRam(uchar addr,uchar *Buffer,uchar Length);

//初始化时钟芯片
uchar Ds1307Init(void);

//声音及模式
uchar ReadSpeakMode(uchar *mode);
uchar SetSpeakMode(uchar mode);
uchar open_speak();
uchar close_speak();

//读写时间信息
//time[]={century year month day hour minute seconds week}
uchar read_alltime(uchar *time);
//time[]={century year month day hour minute seconds week}
uchar  write_alltime(uchar *time);



uchar Ds1307ReadRam(uchar addr,uchar *Buffer,uchar Length)
{
	if(IRcvStr(DS1307_DEVICEADDR,addr,Buffer,Length))
	{
		return ID_OK;
	}
	else
	{
		return ID_ERR;
	}
	
}
uchar Ds1307WriteRam(uchar addr,uchar *Buffer,uchar Length)
{

	if(ISendStr(DS1307_DEVICEADDR,addr,Buffer,Length))
	{
		return ID_OK;
	}
	else
	{
		return ID_ERR;
	}
	
}


uchar Ds1307Init(void)
{
	idata uchar strTemp[8];

	if(Ds1307ReadRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;
	
	//初始化控制字	
	strTemp[0]=(strTemp[0]&0x03);//SQW,OUT
	if(Ds1307WriteRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;

	return ID_OK;

}


//time[]={century year month day hour minute seconds week}
uchar read_alltime(uchar *time)
{
    
	
	idata uchar strTemp[8];

	if(Ds1307ReadRam(0x00,strTemp,7)!=ID_OK)return ID_ERR;

	memcpy(g_valuearr,strTemp,7);
	   
    time[0]=0X20;				//CENTURY ;
    time[1]=strTemp[6];			//year
    time[2]=strTemp[5];			//mon
    time[3]=strTemp[4];			//day
    time[4]=(strTemp[2]&0x3f);	//hour
    time[5]=(strTemp[1]&0x7f);	//min
    time[6]=(strTemp[0]&0x7f);	//sec
    time[7]=strTemp[3];			//week



    return ID_OK ;    
    
}

//time[]={century year month day hour minute seconds week}
uchar  write_alltime(uchar *time)
{
    
    
	idata uchar strTemp[8];
 
	//关掉crystal	
	//if(Ds1307ReadRam(0x00,strTemp,1)!=ID_OK)return ID_ERR;
	//strTemp[0]=(strTemp[0]|0x80);	//CH关掉
	//if(Ds1307WriteRam(0x00,strTemp,1)!=ID_OK)return ID_ERR;
	

	//构造数据    
	strTemp[6]=time[1];		//year
    strTemp[5]=time[2];		//mon
    strTemp[4]=time[3];		//day
	strTemp[2]=time[4];		//hour
	strTemp[1]=time[5];		//min
	strTemp[0]=time[6];		//sec
	strTemp[3]=time[7];		//week

	//写时钟
	strTemp[0]=(strTemp[0]&0x7f);//写同时开时钟
	if(Ds1307WriteRam(0x00,strTemp,7)!=ID_OK)return ID_ERR;	


	return ID_OK ;    
 
}

uchar ReadSpeakMode(uchar *mode)
{

	xdata uchar strTemp[8];  
    
	if(Ds1307ReadRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;
	*mode=(strTemp[0]&0x03);   
    
    return ID_OK ;
    
}

uchar SetSpeakMode(uchar mode)
{
    
    xdata uchar strTemp[8];  
    
    if(Ds1307ReadRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;   
	strTemp[0]=(strTemp[0]&0xfc)+mode;
	if(Ds1307WriteRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;
	return ID_OK ;   
    
}

uchar open_speak()
{
	xdata uchar strTemp[8];  
    
    if(Ds1307ReadRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;   
	strTemp[0] |= 0x10;
	if(Ds1307WriteRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;
	return ID_OK ;      
    
}
uchar close_speak()
{
    
 	xdata uchar strTemp[8];  
    
    if(Ds1307ReadRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;   
	strTemp[0] &= 0xef;
	if(Ds1307WriteRam(0x07,strTemp,1)!=ID_OK)return ID_ERR;
	return ID_OK ;         

}

⌨️ 快捷键说明

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