?? ds1307.c
字號(hào):
#include "ds1307.h"
#include "viic.h"
#include <string.h>
extern uchar g_valuearr[8];
//讀寫(xiě)任意位置ram
uchar Ds1307ReadRam(uchar addr,uchar *Buffer,uchar Length);
uchar Ds1307WriteRam(uchar addr,uchar *Buffer,uchar Length);
//初始化時(shí)鐘芯片
uchar Ds1307Init(void);
//聲音及模式
uchar ReadSpeakMode(uchar *mode);
uchar SetSpeakMode(uchar mode);
uchar open_speak();
uchar close_speak();
//讀寫(xiě)時(shí)間信息
//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];
//關(guān)掉crystal
//if(Ds1307ReadRam(0x00,strTemp,1)!=ID_OK)return ID_ERR;
//strTemp[0]=(strTemp[0]|0x80); //CH關(guān)掉
//if(Ds1307WriteRam(0x00,strTemp,1)!=ID_OK)return ID_ERR;
//構(gòu)造數(shù)據(jù)
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
//寫(xiě)時(shí)鐘
strTemp[0]=(strTemp[0]&0x7f);//寫(xiě)同時(shí)開(kāi)時(shí)鐘
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 ;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -