很想要個(gè)時(shí)鐘模塊,自己焊又太麻煩,干脆在TB上買(mǎi)下來(lái)了,省時(shí)。
模塊參數(shù):
1.尺寸:38mm(長(zhǎng))*22mm(寬)*14mm(高)
2.重量:8g
3.工作電壓:3.3--5.5V
4.時(shí)鐘芯片:高精度時(shí)鐘芯片DS3231
5.時(shí)鐘精度:0-40℃范圍內(nèi),精度2ppm,年誤差約1分鐘
6.帶2個(gè)日歷鬧鐘
7.可編程方波輸出
8.實(shí)時(shí)時(shí)鐘產(chǎn)生秒、分、時(shí)、星期、日期、月和年計(jì)時(shí),并提供有效期到2100年的閏年補(bǔ)償
9.芯片內(nèi)部自帶溫度傳感器,精度為±3℃
10.存儲(chǔ)芯片:AT24C32(存儲(chǔ)容量32K)
11.IIC總線接口,最高傳輸速度400KHz(工作電壓為5V時(shí))
12.可級(jí)聯(lián)其它IIC設(shè)備,24C32地址可通過(guò)短路A0/A1/A2修改,默認(rèn)地址為0x57
13.帶可充電電池LIR2032,保證系統(tǒng)斷電后,時(shí)鐘任然正常走動(dòng)
接線說(shuō)明,以Arduino uno r3為例:
SCL→A5
SDA→A4
VCC→5V
GND→GND
代碼部分:
#include <DS3231.h>
#include <Wire.h>
DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
void setup() {
// 啟動(dòng)I2C(IIC)接口
Wire.begin();
//以下部分是初始化時(shí)間,每次板子通電之后都會(huì)初始化成這個(gè)時(shí)間,只是測(cè)試用,以后可以刪除。
Clock.setSecond(50);//Set the second
Clock.setMinute(59);//Set the minute 設(shè)置分鐘
Clock.setHour(11); //Set the hour 設(shè)置小時(shí)
Clock.setDoW(5); //Set the day of the week 設(shè)置星期幾
Clock.setDate(31); //Set the date of the month 設(shè)置月份
Clock.setMonth(5); //Set the month of the year 設(shè)置一年中的月份
Clock.setYear(13); //Set the year (Last two digits of the year) 設(shè)置年份(在今年的最后兩位數(shù)——比如2013年最后的13)
// Start the serial interface
Serial.begin(115200);
}
void ReadDS3231()
{
int second,minute,hour,date,month,year,temperature;
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();
temperature=Clock.getTemperature();
Serial.print("20");
Serial.print(year,DEC);
Serial.print('-');
Serial.print(month,DEC);
Serial.print('-');
Serial.print(date,DEC);
Serial.print(' ');
Serial.print(hour,DEC);
Serial.print(':');
Serial.print(minute,DEC);
Serial.print(':');
Serial.print(second,DEC);
Serial.print('\n');
Serial.print("Temperature="); //這里是顯示溫度
Serial.print(temperature);
Serial.print('\n');
}
void loop()
{
ReadDS3231();
delay(1000); //間隔1000ms(1000ms=1秒)循環(huán)一次。
}