DS3231, RTC 實驗

http://wp.me/ph3BR-1Zp

 

 

昨天收到貨, 實驗了 DS1302, LCD 時鐘實驗, http://wp.me/ph3BR-1Zj

 

今天收到另外的一種 DS3231, 抓緊時間測試一下, 樣品單價比較高, 等批量貨到就不用匆忙寫碼了.

繼續抄抄寫寫, 抄來的源碼, 改一改, 方便觀察實驗結果.

用 UNO, 四條連線,

SDA – A4

SCL – A5

VCC – VCC

GND – GND

 

DS3231-test-ok

 

 

 

同樣, 使用 setRTC 編譯燒錄一次, 再禁用 setRTC 並編譯燒錄一次, RTC 時鐘就設定到下面預設的時日, 直到拔掉鈕扣電池.


//The following lines can be uncommented to set the date and time
//setRTC();

 

 

要預設你的自己的時間, 修改這個位置

// preset the date to DEC,31,2098
#define _year 2098
#define _month 12
#define _day 31
#define _weekday WEDNESDAY
// preset time to 23:59:55
#define _HH 23
#define _mm 59
#define _ss 55

 

 

 

俺自己的完整源碼,




#include <DS3231.h>

// DS3231_Serial_Easy
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// A quick demo of how to use my DS3231-library to
// quickly send time and date information over a serial link
//
// To use the hardware I2C (TWI) interface of the Arduino you must connect
// the pins as follows:
//
// Arduino Uno/2009:
// ----------------------
// DS3231: SDA pin -&gt; Arduino Analog 4 or the dedicated SDA pin
// SCL pin -&gt; Arduino Analog 5 or the dedicated SCL pin
//
// Arduino Leonardo:
// ----------------------
// DS3231: SDA pin -&gt; Arduino Digital 2 or the dedicated SDA pin
// SCL pin -&gt; Arduino Digital 3 or the dedicated SCL pin
//
// Arduino Mega:
// ----------------------
// DS3231: SDA pin -&gt; Arduino Digital 20 (SDA) or the dedicated SDA pin
// SCL pin -&gt; Arduino Digital 21 (SCL) or the dedicated SCL pin
//
// Arduino Due:
// ----------------------
// DS3231: SDA pin -&gt; Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
// SCL pin -&gt; Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
//
// The internal pull-up resistors will be activated when using the
// hardware I2C interfaces.
//
// You can connect the DS3231 to any available pin but if you use any
// other than what is described above the library will fall back to
// a software-based, TWI-like protocol which will require exclusive access
// to the pins used, and you will also have to use appropriate, external
// pull-up resistors on the data and clock signals.
//



// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);

void setup()
{
// Setup Serial connection
Serial.begin(115200);
// Uncomment the next line if you are using an Arduino Leonardo
//while (!Serial) {}

// Initialize the rtc object
rtc.begin();

// The following lines can be uncommented to set the date and time
//setRTC();
}

// xiaolaba, routine change, easy for setup RTC.
//https://xiaolaba.wordpress.com/2017/06/06/ds3231-rtc-%e5%af%a6%e9%a9%97/
void setRTC()
{
// preset the date to DEC,31,2098
#define _year 2098
#define _month 12
#define _day 31
#define _weekday WEDNESDAY
// preset time to 23:59:55
#define _HH 23
#define _mm 59
#define _ss 55

rtc.setDOW(_weekday); // Set Day-of-Week
rtc.setTime(_HH,_mm,_ss); // Set the time (24hr format)
rtc.setDate(_day, _month, _year); // Set the date to month,day,year
}



void loop()
{
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");

// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");

// Send time
Serial.println(rtc.getTimeStr());

// Wait one second before repeating 🙂
delay (1000);
}