Skip to content

Commit ca91975

Browse files
author
JChristensen
committed
Add SetSerial example sketch, set the RTC date, time, and calibration register from the Arduino serial monitor.
1 parent 10194da commit ca91975

File tree

2 files changed

+165
-1
lines changed

2 files changed

+165
-1
lines changed

ReadMe.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ rtcSet2: Similar to rtcSet1, a different way to hard-code the date and time.
3939

4040
rtcSet3: Set the RTC to the sketch compile date and time.
4141

42-
rtcSetSerial: Set the RTC via serial input from the Arduino monitor.
42+
SetSerial: Set the RTC's date, time, and calibration register from
43+
Arduino serial monitor.
44+
45+
rtcSetSerial: Set the RTC via serial input from the Arduino serial monitor.
4346

4447
TimeRTC: Same as the example of the same name provided with the Time library,
4548
demonstrating the interchangeability of the MCP79412RTC library with the

examples/SetSerial/SetSerial.ino

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*----------------------------------------------------------------------*
2+
* Displays the date and time from an MCP79412 RTC every second. *
3+
* *
4+
* Set the date and time by entering the following on the Arduino *
5+
* serial monitor: *
6+
* Syear,month,day,hour,minute,second, *
7+
* *
8+
* Where *
9+
* year can be two or four digits, *
10+
* month is 1-12, *
11+
* day is 1-31, *
12+
* hour is 0-23, and *
13+
* minute and second are 0-59. *
14+
* *
15+
* Set the calibration register by entering the following: *
16+
* Cnnn, *
17+
* *
18+
* Where nnn can be a positive or negative value, e.g. "c4" or "c-42". *
19+
* *
20+
* Entering the final comma delimiter (after "second") will avoid a *
21+
* one-second timeout and will allow the RTC to be set more accurately. *
22+
* *
23+
* No validity checking is done, invalid values or incomplete syntax *
24+
* in the input will result in an incorrect RTC setting. *
25+
* *
26+
* Jack Christensen 28Aug2013 *
27+
* *
28+
* Tested with Arduino 1.0.5, Arduino Uno. *
29+
* *
30+
* This work is licensed under the Creative Commons Attribution- *
31+
* ShareAlike 3.0 Unported License. To view a copy of this license, *
32+
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
33+
* letter to Creative Commons, 171 Second Street, Suite 300, *
34+
* San Francisco, California, 94105, USA. *
35+
*----------------------------------------------------------------------*/
36+
37+
#include <MCP79412RTC.h> //http://github.com/JChristensen/MPC79412RTC
38+
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
39+
#include <Time.h> //http://playground.arduino.cc/Code/Time
40+
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
41+
42+
void setup(void)
43+
{
44+
byte rtcID[8];
45+
46+
Serial.begin(115200);
47+
48+
//setSyncProvider() causes the Time library to synchronize with the
49+
//external RTC by calling RTC.get() every five minutes by default.
50+
setSyncProvider(RTC.get);
51+
Serial << endl << F("RTC Sync");
52+
if (timeStatus() != timeSet) Serial << F(" FAIL!");
53+
Serial << endl;
54+
55+
RTC.idRead(rtcID);
56+
Serial << F("RTC ID = ");
57+
for (int i=0; i<8; ++i) {
58+
if (rtcID[i] < 16) Serial << '0';
59+
Serial << _HEX(rtcID[i]);
60+
}
61+
Serial << endl;
62+
Serial << F("Calibration Register = ") << RTC.calibRead() << endl;
63+
}
64+
65+
void loop(void)
66+
{
67+
static time_t tLast;
68+
time_t t;
69+
tmElements_t tm;
70+
int cmdChar, y, oldCal, newCal;
71+
72+
//check for input, first character is a command, "S" to set date/time, or "C" to set the calibration register
73+
if (Serial.available()) {
74+
cmdChar = Serial.read();
75+
76+
switch (cmdChar) {
77+
case 'S':
78+
case 's':
79+
//note that the tmElements_t Year member is an offset from 1970,
80+
//but the RTC wants the last two digits of the calendar year.
81+
//use the convenience macros from Time.h to do the conversions.
82+
y = Serial.parseInt();
83+
if (y >= 100 && y < 1000)
84+
Serial << F("Error: Year must be two digits or four digits!") << endl;
85+
else {
86+
if (y >= 1000)
87+
tm.Year = CalendarYrToTm(y);
88+
else //(y < 100)
89+
tm.Year = y2kYearToTm(y);
90+
tm.Month = Serial.parseInt();
91+
tm.Day = Serial.parseInt();
92+
tm.Hour = Serial.parseInt();
93+
tm.Minute = Serial.parseInt();
94+
tm.Second = Serial.parseInt();
95+
t = makeTime(tm);
96+
RTC.write(tm);
97+
setTime(t);
98+
Serial << F("RTC set to: ");
99+
printDateTime(t);
100+
Serial << endl;
101+
}
102+
break;
103+
104+
case 'C':
105+
case 'c':
106+
newCal = Serial.parseInt();
107+
oldCal = RTC.calibRead();
108+
RTC.calibWrite(newCal);
109+
Serial << F("Calibration changed from ") << oldCal << F(" to ") << RTC.calibRead() << endl;
110+
break;
111+
112+
default:
113+
Serial << endl << F("Unrecognized command: ") << (char)cmdChar << endl;
114+
break;
115+
}
116+
117+
//dump any extraneous input
118+
while (Serial.available() > 0) Serial.read();
119+
}
120+
121+
t = now();
122+
if (t != tLast) {
123+
tLast = t;
124+
printDateTime(t);
125+
Serial << endl;
126+
}
127+
}
128+
129+
//print date and time to Serial
130+
void printDateTime(time_t t)
131+
{
132+
printDate(t);
133+
Serial << ' ';
134+
printTime(t);
135+
}
136+
137+
//print time to Serial
138+
void printTime(time_t t)
139+
{
140+
printI00(hour(t), ':');
141+
printI00(minute(t), ':');
142+
printI00(second(t), ' ');
143+
}
144+
145+
//print date to Serial
146+
void printDate(time_t t)
147+
{
148+
printI00(day(t), 0);
149+
Serial << monthShortStr(month(t)) << _DEC(year(t));
150+
}
151+
152+
//Print an integer in "00" format (with leading zero),
153+
//followed by a delimiter character to Serial.
154+
//Input value assumed to be between 0 and 99.
155+
void printI00(int val, char delim)
156+
{
157+
if (val < 10) Serial << '0';
158+
Serial << _DEC(val);
159+
if (delim > 0) Serial << delim;
160+
return;
161+
}

0 commit comments

Comments
 (0)