|
本帖最后由 xizhe2005 于 2017-3-5 21:44 編輯
做過幾個時鐘,用過1302,DS12C887,都不太理想,既然是時鐘就要精準,結(jié)果過了兩天就差了幾分了,讓人比較苦惱,其中DS12C887用STC的
1T單片機怎么也讀不出來時間,只能用12T的單片機,用過這些后發(fā)現(xiàn)了DS3231這個時鐘芯片,價格差不多,比12C887還便宜,是內(nèi)置晶振的,
到底有多精準,直觀的體會就是:過幾天和手機的時間一對分毫不差。一下是很實用簡單的程序:單片機是STC12C5A60S2,晶振11.0592M
#define DS3231_WriteAddress 0xD0 //器件寫地址
#define DS3231_ReadAddress 0xD1 //器件讀地址
#define DS3231_SECOND 0x00 //秒
#define DS3231_MINUTE 0x01 //分
#define DS3231_HOUR 0x02 //時
#define DS3231_WEEK 0x03 //星期
#define DS3231_DAY 0x04 //日
#define DS3231_MONTH 0x05 //月
#define DS3231_YEAR 0x06 //年
//啟動信號函數(shù)
void IICstart_ds3231(void)
{
SDA=1;
SCL=1;
_nop_();
_nop_();
SDA=0;
_nop_();
_nop_();
SCL=0;
}
//停止信號函數(shù)
void IICstop_ds3231(void)
{
SDA=0;
SCL=1;
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
SCL=0;
}
//向IIC總線寫入一個字節(jié)函數(shù)
void Write1Byte_ds3231(unsigned char Buf1)
{
unsigned char k;
for(k=0;k<8;k++)
{
if(Buf1&0x80)
SDA=1;
else
SDA=0;
_nop_();
_nop_();
SCL=1;
Buf1=Buf1<<1;
_nop_();
SCL=0;
_nop_();
}
SDA=1;
_nop_();
SCL=1;
_nop_();
_nop_();
SCL=0;
}
//從IIC讀出一個字節(jié)函數(shù)
unsigned char Read1Byte_ds3231()
{
unsigned char k,t=0;
for(k=0;k<8;k++)
{
t=t<<1;
SDA=1;
SCL=1;
_nop_();
_nop_();
if(SDA==1)
t=t|0x01;
else
t=t&0xfe;
SCL=0;
_nop_();
_nop_();
}
return t;
}
//向指定地址寫一個字節(jié)函數(shù)
void Write_ds3231(unsigned char Address,unsigned char Databuf)
{
IICstart_ds3231();
Write1Byte_ds3231(0xd0);//0xA0是IIC器件的寫地址1010 0000最低位為0為寫操作
Write1Byte_ds3231(Address);
Write1Byte_ds3231(Databuf);
IICstop();
}
//從指定地址讀一個字節(jié)的函數(shù)
unsigned char ReadAT_ds3231(unsigned char Address)
{
unsigned char buf;
IICstart_ds3231();
Write1Byte_ds3231(0xd0);
Write1Byte_ds3231(Address);
IICstart_ds3231();
Write1Byte_ds3231(0xd1);
buf=Read1Byte_ds3231();
IICstop_ds3231();
return(buf);
}
|
評分
-
查看全部評分
|