-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSetSerial.ino
More file actions
165 lines (150 loc) · 5.59 KB
/
Copy pathSetSerial.ino
File metadata and controls
165 lines (150 loc) · 5.59 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Arduino MCP79412RTC Library
// https://github.com/JChristensen/MCP79412RTC
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// Example sketch. Displays the date and time from an MCP79412 RTC
// every second.
//
// Set the date and time by entering the following on the Arduino
// serial monitor:
// Syear,month,day,hour,minute,second,
//
// Where
// year can be two or four digits,
// month is 1-12,
// day is 1-31,
// hour is 0-23, and
// minute and second are 0-59.
//
// Set the calibration register by entering the following:
// Cnnn,
//
// Where nnn can be a positive or negative value, e.g. "c4" or "c-42".
//
// Entering the final comma delimiter (after "second") will avoid a
// one-second timeout and will allow the RTC to be set more accurately.
//
// No validity checking is done, invalid values or incomplete syntax
// in the input will result in an incorrect RTC setting.
//
// Jack Christensen 28Aug2013
#include <MCP79412RTC.h> // https://github.com/JChristensen/MCP79412RTC
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
MCP79412RTC myRTC;
void setup()
{
Serial.begin(115200);
Serial << F( "\n" __FILE__ "\n" __DATE__ " " __TIME__ "\n" );
myRTC.begin();
// setSyncProvider() causes the Time library to synchronize with the
// external RTC by calling myRTC.get() every five minutes by default.
setSyncProvider([](){return myRTC.get();});
Serial << endl << F("RTC Sync");
if (timeStatus() != timeSet) Serial << F(" FAIL!");
Serial << endl;
uint8_t rtcID[8];
myRTC.idRead(rtcID);
Serial << F("RTC ID = ");
for (int i=0; i<8; ++i) {
if (rtcID[i] < 16) Serial << '0';
Serial << _HEX(rtcID[i]);
}
Serial << endl;
myRTC.getEUI64(rtcID);
Serial << F("EUI-64 = ");
for (int i=0; i<8; ++i) {
if (rtcID[i] < 16) Serial << '0';
Serial << _HEX(rtcID[i]);
}
Serial << endl;
Serial << F("Calibration Register = ") << myRTC.calibRead() << endl;
}
void loop()
{
// check for input, to set rtc time or calibration
if (Serial.available()) setRTC();
// print time and date every second
static time_t tLast;
time_t t { now() };
if (t != tLast) {
tLast = t;
printTime(t);
}
}
void setRTC()
{
// first character is a command, "S" to set date/time, or "C" to set the calibration register
int cmdChar = Serial.read();
switch (cmdChar) {
case 'S':
case 's':
delay(25); // wait for all the input to arrive
// check for input to set the RTC, minimum length is 13, i.e. yy,m,d,h,m,s<nl>
if (Serial.available() < 13) {
while (Serial.available()) Serial.read(); // dump extraneous input
Serial << F("Input error or timeout, try again.\n");
}
else {
// note that the tmElements_t Year member is an offset from 1970,
// but the RTC wants the last two digits of the calendar year.
// use the convenience macros from TimeLib.h to do the conversions.
int y = Serial.parseInt();
if (y >= 100 && y < 1000)
Serial << F("Error: Year must be two digits or four digits!\n");
else {
tmElements_t tm;
if (y >= 1000)
tm.Year = CalendarYrToTm(y);
else //(y < 100)
tm.Year = y2kYearToTm(y);
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
if (tm.Month == 0 || tm.Day == 0) {
while (Serial.available()) Serial.read(); // dump extraneous input
Serial << F("Input error or timeout, try again.\n");
}
else {
time_t t = makeTime(tm);
myRTC.set(t); // use the time_t value to ensure correct weekday is set
setTime(t);
Serial << F("RTC set to: ");
printTime(t);
}
}
}
break;
case 'C':
case 'c':
delay(25); // wait for all the input to arrive
if (Serial.available() < 2) { // minimum valid input at this point is 2 chars
while (Serial.available()) Serial.read(); // dump extraneous input
Serial << F("Input error or timeout, try again.\n");
}
else {
int newCal = Serial.parseInt();
int oldCal = myRTC.calibRead();
myRTC.calibWrite(newCal);
Serial << F("Calibration changed from ") << oldCal << F(" to ") << myRTC.calibRead() << endl;
}
break;
default:
Serial << endl << F("Unrecognized command: ") << (char)cmdChar << endl;
break;
}
// dump any extraneous input
while (Serial.available()) Serial.read();
}
// format and print a time_t value
void printTime(const time_t t)
{
char buf[25];
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t));
Serial.println(buf);
}