參考文章:[https://blog.csdn.net/ling3ye/article/details/74942928]
ds3231連線.JPG
程序
需要下載庫
http://www.rinkydinkelectronics.com/library.php?id=74
設置時間的程序
#include <DS3231.h>
// 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
rtc.setDOW(THURSDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(20, 43, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(22, 8, 2019); // Set the date to January 1st, 2014
}
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);
}
#include "TM1637.h"
#include <Wire.h>
#include "Sodaq_DS3231.h"
DateTime dt(2018, 5, 4, 14, 5, 0, 5); // 年 月 日 時 分 秒 星期。周日-周六對應0-6
//pins definitions for TM1637 and can be changed to other ports
#define CLK A0
#define DIO A1
TM1637 tm1637(CLK, DIO);
void setup()
{
tm1637.init();
// 設置LED亮度。最暗到最亮 0-7。典型值2。
tm1637.set(1);
Wire.begin();
rtc.begin();
// 第一次使用時鐘模塊,或者需要校準時放開下列注釋
// 一旦校準完畢,繼續注釋掉,并再次上傳
// 定義dt的時候建議預留一些編譯和上傳的時間
//rtc.setDateTime(dt);
}
// 時間分隔符閃爍標識
bool ShowPoint = true;
void loop()
{
DateTime now = rtc.now();
int h = now.hour();
int mn = now.minute();
int b0 = h / 10;
int b1 = h % 10;
int b2 = mn / 10;
int b3 = mn % 10;
tm1637.point(ShowPoint);
tm1637.display(0, b0);
tm1637.display(1, b1);
tm1637.display(2, b2);
tm1637.display(3, b3);
ShowPoint = !ShowPoint;
delay(1000);
}