-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SetDateTimeRTC_2.ino
78 lines (67 loc) · 2.65 KB
/
SetDateTimeRTC_2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*----------------------------------------------------------------------*
* A simple example of how to set the date and time, *
* read the date and time from the RX8025T RTC and display them. *
* *
* In addition, it shows how to display the date *
* with the names of the days of the week and months. *
* *
* The MIT License *
* Marcin Saj 25 OCT 2022 *
* https://github.com/marcinsaj/RTC_RX8025T *
*----------------------------------------------------------------------*/
#include <RTC_RX8025T.h> //https://github.com/marcinsaj/RTC_RX8025T
#include <TimeLib.h> //https://github.com/PaulStoffregen/Time
#include <Wire.h> //https://arduino.cc/en/Reference/Wire (included with Arduino IDE)
//Declare structure that allows convenient access to the elements of time like hours, minutes and seconds
tmElements_t tm;
const char * dayOfWeekName[] =
{
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
};
const char * monthName[] =
{
"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
};
void setup(void)
{
Serial.begin(9600);
delay(3000);
//RX8025T initialization
RTC_RX8025T.init();
//The Time library is a software RTC_RX8025T.
//"system time" it is referring to the current time according to the software RTC_RX8025T.
//Set the system time to 10h 23m 30s on 25 Oct 2022
setTime(10, 23, 30, 25, 10, 22);
//Set the RTC from the system time
RTC_RX8025T.set(now());
}
void loop(void)
{
//Read the time from the RTC and store it in the tm structure
RTC_RX8025T.read(tm);
printDateTime();
delay(1000);
}
void printDateTime(void)
{
Serial.print("Date: ");
//Time library macro for convenient Year conversion to calendar format
Serial.print(tmYearToCalendar(tm.Year));
Serial.print(" ");
Serial.print(monthName[tm.Month]);
Serial.print(" ");
if(tm.Day < 10) Serial.print("0");
Serial.print(tm.Day);
Serial.print(" ");
//tm.Wday stores values from 1 to 7, so we have to subtract 1 to correctly read the names from the array
Serial.print(dayOfWeekName[tm.Wday - 1]);
Serial.print(" Time: ");
if(tm.Hour < 10) Serial.print("0");
Serial.print(tm.Hour);
Serial.print(":");
if(tm.Minute < 10) Serial.print("0");
Serial.print(tm.Minute);
Serial.print(":");
if(tm.Second < 10) Serial.print("0");
Serial.println(tm.Second);
}