|
发表于 2022-10-24 10:32:38
|
显示全部楼层
#include <U8g2lib.h>
#include <Wire.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
ThreeWire myWire(A1, A2, A0); //set data.clk,rst Pin
RtcDS1302<ThreeWire> Rtc(myWire);
#define countof(a) (sizeof(a) / sizeof(a[0]))
int yue , ri, nian, shi , fen , miao , zhou ;
void setup() {
Serial.begin(9600);
Rtc.Begin();
u8g2.begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); //Set Time for Rtc Object
RtcDateTime now = Rtc.GetDateTime();
}
void loop() {
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);串口打印当前时间
show_time(now);//屏幕上显示当前时间
}
void printDateTime(const RtcDateTime& dt) {
char datestring[22];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u %01u"),//输出日期到串口 输出的结果总共占两位,十进制的形式输出
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second(),
dt.DayOfWeek()
);
//Serial.println(datestring);
}
//show time
void show_time(const RtcDateTime & dt) { //时间显示页面
String z_str = "";
yue = dt.Month();
ri = dt.Day();
nian = dt.Year();
shi = dt.Hour();
fen = dt.Minute();
miao = dt.Second();
zhou = dt.DayOfWeek();
switch ( zhou)
{ //将星期从数字转换为名称
case 7: z_str = "Sunday"; break;
case 1: z_str = "Monday"; break;
case 2: z_str = "Tuesday"; break;
case 3: z_str = "Wednesday"; break;
case 4: z_str = "Thursday"; break;
case 5: z_str = "Friday"; break;
case 6: z_str = "Saturday"; break;
}
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB10_tr);//设置字体
u8g2.setCursor(12, 18); //设置光标处
u8g2.print(String(nian) + "-" + String(yue) + "-" + String(ri));//输出内容
u8g2.setCursor(24, 41); //设置光标处
u8g2.print(String(shi) + ":" + String(fen) + ":" + String(miao));//输出内容
u8g2.setCursor(12, 60);//设置光标处
u8g2.print( |
|