参考文章:[https://blog.csdn.net/ling3ye/article/details/74942928]
程序
需要下载库
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);
}