TM1637库:https://github.com/bremme/arduino-tm1637
DS3231库:https://github.com/adafruit/RTClib
#include <TimerOne.h>
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#define TM_CLK_PIN 4
#define TM_DIO_PIN 5
SevenSegmentExtended tm1637(TM_CLK_PIN, TM_DIO_PIN);
RTC_DS3231 rtc;
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2); //IIC地址为0x3F
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const unsigned long interval_time = 1000000; //1000ms
unsigned long prev_time = 0;
DateTime cur;
byte update;
byte clock_point = 0;
byte cur_hour = 0;
byte cur_minute = 0;
unsigned int cur_year = 2019;
unsigned int cur_month = 1;
unsigned int cur_day = 1;
String lcd_line1;
String lcd_line2;
void setup() {
Serial.begin(9600);
delay(3000); //wait for console opening
tm1637.begin();
tm1637.setBacklight(80); //set the brightness to 80%
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
/*
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))
+ TimeSpan(0, 0, 0, 25)); */
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))
+ TimeSpan(0, 0, 0, 25));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
Timer1.initialize(interval_time); //1s
Timer1.attachInterrupt(timing_isr);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Start!");
delay(2000);
}
void loop() {
if (update) {
time_update();
tm1637.printTime(cur_hour, cur_minute, true);
lcd_display();
}
}
void timing_isr() {
update = 1 - update;
clock_point = (~clock_point) & 0x01;
}
void time_update() {
// if (clock_point) tm1637.point(POINT_ON);
// else tm1637.point(POINT_OFF);
cur = rtc.now();
// Serial.println(cur);
cur_minute = cur.minute();
cur_hour = cur.hour();
cur_year = cur.year();
cur_month = cur.month();
cur_day = cur.day();
lcd_line2 = daysOfTheWeek[cur.dayOfTheWeek()];
lcd_line1 = String("") + cur.year() + "-"
+ (cur_month / 10) + (cur_month % 10) + "-"
+ (cur_day / 10) + (cur_day % 10);
update = 0;
}
void lcd_display() {
lcd.setCursor(0, 0);
lcd.print(lcd_line1);
lcd.setCursor(4, 1);
lcd.print(lcd_line2);
}