Skip to content

Commit 68ea5a2

Browse files
author
Jack Christensen
committed
v1.0
Complete documentation. Additional example sketches. Otherwise minor tweaks.
1 parent 0c73181 commit 68ea5a2

File tree

8 files changed

+660
-141
lines changed

8 files changed

+660
-141
lines changed

Examples/PowerOutageLogger/PowerOutageLogger.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void setup() {
4343

4444
setSyncProvider(RTC.get); //the function to get the time from the RTC
4545
Serial << "RTC SYNC";
46-
if (timeStatus()!= timeSet) Serial << "FAIL";
46+
if (timeStatus()!= timeSet) Serial << " FAIL";
4747
Serial << endl;
4848

4949
//logClear();

Examples/rtcSet1/rtcSet1.ino

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*----------------------------------------------------------------------*
2+
* Example sketch for Arduino MCP79412 Library by Jack Christensen *
3+
* *
4+
* Sets the RTC date and time to a hard-coded value. *
5+
* This is just a simple demonstration of setting the RTC time. *
6+
* Note that each time the sketch is run (or the microcontroller is *
7+
* reset), the RTC will be set to the same hard-coded time, so this *
8+
* example may be of limited usefulness as an actual clock. *
9+
* *
10+
* This work is licensed under the Creative Commons Attribution- *
11+
* ShareAlike 3.0 Unported License. To view a copy of this license, *
12+
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
13+
* letter to Creative Commons, 171 Second Street, Suite 300, *
14+
* San Francisco, California, 94105, USA. *
15+
*----------------------------------------------------------------------*/
16+
17+
#include <MCP79412RTC.h> //http://github.com/JChristensen/MCP79412RTC
18+
#include <Time.h> //http://www.arduino.cc/playground/Code/Time
19+
#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
20+
21+
void setup(void)
22+
{
23+
delay(2000);
24+
Serial.begin(9600);
25+
26+
setTime(23, 31, 30, 13, 2, 2009); //set the system time to 23h31m30s on 13Feb2009
27+
//the setTime() function is from the Time.h library.
28+
//setTime(hour, minute, second, day, month, year);
29+
RTC.set(now()); //set the RTC from the system time
30+
}
31+
32+
void loop(void)
33+
{
34+
printTime(now());
35+
delay(1000);
36+
}
37+
38+
//Print time (and date) given a time_t value
39+
void printTime(time_t t)
40+
{
41+
printI00(hour(t), ':');
42+
printI00(minute(t), ':');
43+
printI00(second(t), ' ');
44+
Serial.print(dayShortStr(weekday(t)));
45+
Serial.print(' ');
46+
printI00(day(t), ' ');
47+
Serial.print(monthShortStr(month(t)));
48+
Serial.print(' ');
49+
Serial.println(year(t));
50+
}
51+
52+
//Print an integer in "00" format (with leading zero),
53+
//followed by a delimiter.
54+
//Input value assumed to be between 0 and 99.
55+
void printI00(int val, char delim)
56+
{
57+
if (val < 10) Serial.print('0');
58+
Serial.print(val);
59+
Serial.print(delim);
60+
return;
61+
}

Examples/rtcSet2/rtcSet2.ino

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*----------------------------------------------------------------------*
2+
* Example sketch for Arduino MCP79412 Library by Jack Christensen *
3+
* *
4+
* Sets the RTC date and time to a hard-coded value, using a *
5+
* tmElements_t structure. The same structure is then used to *
6+
* read the RTC once per second. This sketch only uses the Time *
7+
* library for the tmElements_t definition. *
8+
* *
9+
* This is just a simple demonstration of setting the RTC time. *
10+
* Note that each time the sketch is run (or the microcontroller is *
11+
* reset), the RTC will be set to the same hard-coded time, so this *
12+
* example may be of limited usefulness as an actual clock. *
13+
* *
14+
* This work is licensed under the Creative Commons Attribution- *
15+
* ShareAlike 3.0 Unported License. To view a copy of this license, *
16+
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
17+
* letter to Creative Commons, 171 Second Street, Suite 300, *
18+
* San Francisco, California, 94105, USA. *
19+
*----------------------------------------------------------------------*/
20+
21+
#include <MCP79412RTC.h> //http://github.com/JChristensen/MCP79412RTC
22+
#include <Time.h> //http://www.arduino.cc/playground/Code/Time
23+
#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
24+
25+
tmElements_t tm;
26+
27+
void setup(void)
28+
{
29+
delay(2000);
30+
Serial.begin(9600);
31+
32+
tm.Hour = 23; //set the tm structure to 23h31m30s on 13Feb2009
33+
tm.Minute = 31;
34+
tm.Second = 30;
35+
tm.Year = 2009 - 1970; //tmElements_t.Year is the offset from 1970.
36+
tm.Month = 2;
37+
tm.Day = 13;
38+
RTC.write(tm); //set the RTC from the tm structure
39+
}
40+
41+
void loop(void)
42+
{
43+
RTC.read(tm);
44+
Serial.print(tm.Hour, DEC);
45+
Serial.print(':');
46+
Serial.print(tm.Minute,DEC);
47+
Serial.print(':');
48+
Serial.print(tm.Second,DEC);
49+
Serial.print(' ');
50+
Serial.print(tm.Year + 1970, DEC);
51+
Serial.print('-');
52+
Serial.print(tm.Month, DEC);
53+
Serial.print('-');
54+
Serial.print(tm.Day, DEC);
55+
Serial.println();
56+
delay(1000);
57+
}
58+

Examples/rtcSet3/rtcSet3.ino

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*----------------------------------------------------------------------*
2+
* Example sketch for Arduino MCP79412 Library by Jack Christensen *
3+
* *
4+
* Sets the RTC date and time to the sketch compile time. *
5+
* Re-running this sketch by pressing the reset button, etc., will *
6+
* cause the RTC time to be reset to the *same* compile time. *
7+
* Upload the sketch again to recompile it with the current time. *
8+
* *
9+
* This work is licensed under the Creative Commons Attribution- *
10+
* ShareAlike 3.0 Unported License. To view a copy of this license, *
11+
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
12+
* letter to Creative Commons, 171 Second Street, Suite 300, *
13+
* San Francisco, California, 94105, USA. *
14+
*----------------------------------------------------------------------*/
15+
16+
#include <MCP79412RTC.h> //http://github.com/JChristensen/MCP79412RTC
17+
#include <Time.h> //http://www.arduino.cc/playground/Code/Time
18+
#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
19+
20+
void setup(void)
21+
{
22+
delay(2000);
23+
Serial.begin(9600);
24+
25+
setTime(compileTime()); //set the system time to the sketch compile time
26+
RTC.set(now()); //set the RTC from the system time
27+
}
28+
29+
void loop(void)
30+
{
31+
printTime(now());
32+
delay(1000);
33+
}
34+
35+
//Function to return the compile date and time as a time_t value
36+
time_t compileTime(void)
37+
{
38+
#define FUDGE 15 //fudge factor to allow for compile time (seconds, YMMV)
39+
40+
char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
41+
char chMon[3], *m;
42+
int d, y;
43+
tmElements_t tm;
44+
time_t t;
45+
46+
strncpy(chMon, compDate, 3);
47+
chMon[3] = '\0';
48+
m = strstr(months, chMon);
49+
tm.Month = ((m - months) / 3 + 1);
50+
51+
tm.Day = atoi(compDate + 4);
52+
tm.Year = atoi(compDate + 7) - 1970;
53+
tm.Hour = atoi(compTime);
54+
tm.Minute = atoi(compTime + 3);
55+
tm.Second = atoi(compTime + 6);
56+
t = makeTime(tm);
57+
return t + FUDGE; //add fudge factor to allow for compile time
58+
}
59+
60+
//Print time (and date) given a time_t value
61+
void printTime(time_t t)
62+
{
63+
printI00(hour(t), ':');
64+
printI00(minute(t), ':');
65+
printI00(second(t), ' ');
66+
Serial.print(dayShortStr(weekday(t)));
67+
Serial.print(' ');
68+
printI00(day(t), ' ');
69+
Serial.print(monthShortStr(month(t)));
70+
Serial.print(' ');
71+
Serial.println(year(t));
72+
}
73+
74+
//Print an integer in "00" format (with leading zero),
75+
//followed by a delimiter.
76+
//Input value assumed to be between 0 and 99.
77+
void printI00(int val, char delim)
78+
{
79+
if (val < 10) Serial.print('0');
80+
Serial.print(val);
81+
Serial.print(delim);
82+
return;
83+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*----------------------------------------------------------------------*
2+
* Example sketch for Arduino MCP79412 Library by Jack Christensen *
3+
* *
4+
* Set the RTC by entering a "Set" command on the serial monitor. *
5+
* Use a 24-hour clock and enter the command (case sensitive) exactly *
6+
* as follows: Set yyyy-mm-dd hh:mm:ss *
7+
* *
8+
* This work is licensed under the Creative Commons Attribution- *
9+
* ShareAlike 3.0 Unported License. To view a copy of this license, *
10+
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
11+
* letter to Creative Commons, 171 Second Street, Suite 300, *
12+
* San Francisco, California, 94105, USA. *
13+
*----------------------------------------------------------------------*/
14+
15+
#include <MCP79412RTC.h> //http://github.com/JChristensen/MCP79412RTC
16+
#include <Time.h> //http://www.arduino.cc/playground/Code/Time
17+
#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
18+
19+
#define PRINT_INTERVAL 1000 //ms between printing the time
20+
21+
unsigned long ms, msLast;
22+
23+
void setup()
24+
{
25+
delay(2000);
26+
Serial.begin(9600);
27+
28+
setSyncProvider(RTC.get); //the function to get the time from the RTC
29+
Serial.print("RTC SYNC");
30+
if (timeStatus() != timeSet) Serial.print(" FAIL");
31+
Serial.println();
32+
}
33+
34+
void loop(void)
35+
{
36+
ms = millis();
37+
readCommand();
38+
if (ms - msLast >= PRINT_INTERVAL) {
39+
msLast = ms;
40+
printTime(now());
41+
}
42+
}
43+
44+
//Read command from the Arduino serial monitor to set the RTC.
45+
//Case-sensitive and must be entered exactly as (24-hour clock):
46+
// Set yyyy-mm-dd hh:mm:ss
47+
void readCommand()
48+
{
49+
char cmd[24] = "Set yyyy-mm-dd hh:mm:ss";
50+
static int i;
51+
tmElements_t tmSet;
52+
time_t tSet;
53+
54+
if (Serial.available() >= 23) { //enough characters for the whole command?
55+
i = 0; //yes, read the available characters
56+
while (Serial.available() > 0) {
57+
if (i >= sizeof(cmd) - 1) { //more than we can enjoy
58+
flushInput(); //clear out the input buffer
59+
Serial.print("Too long: ");
60+
Serial.println(cmd);
61+
return;
62+
}
63+
delay(2); //let the next character trickle in
64+
cmd[i++] = char(Serial.read());
65+
}
66+
cmd[i] = 0; //put in string terminator
67+
68+
if (strncmp(cmd, "Set ", 4) == 0) {
69+
tmSet.Year = 1000 * (cmd[4] - '0') + 100 * (cmd[5] - '0') + 10 * (cmd[6] - '0') + cmd[7] - '0' - 1970;
70+
tmSet.Month = 10 * (cmd[9] - '0') + cmd[10] - '0';
71+
tmSet.Day = 10 * (cmd[12] - '0') + cmd[13] - '0';
72+
tmSet.Hour = 10 * (cmd[15] - '0') + cmd[16] - '0';
73+
tmSet.Minute = 10 * (cmd[18] - '0') + cmd[19] - '0';
74+
tmSet.Second = 10 * (cmd[21] - '0') + cmd[22] - '0';
75+
tSet = makeTime(tmSet); //convert to time_t
76+
setTime(tSet); //set the system time
77+
RTC.set(now()); //set the rtc
78+
Serial.println("RTC set!");
79+
flushInput(); //discard any extraneous trailing characters
80+
}
81+
else {
82+
Serial.print("Unknown: ");
83+
Serial.println(cmd);
84+
}
85+
}
86+
}
87+
88+
void flushInput(void)
89+
{
90+
do {
91+
delay(2);
92+
Serial.read();
93+
} while (Serial.available() > 0);
94+
}
95+
96+
//Print time (and date) given a time_t value
97+
void printTime(time_t t)
98+
{
99+
printI00(hour(t), ':');
100+
printI00(minute(t), ':');
101+
printI00(second(t), ' ');
102+
Serial.print(dayShortStr(weekday(t)));
103+
Serial.print(' ');
104+
printI00(day(t), ' ');
105+
Serial.print(monthShortStr(month(t)));
106+
Serial.print(' ');
107+
Serial.println(year(t));
108+
}
109+
110+
//Print an integer in "00" format (with leading zero),
111+
//followed by a delimiter.
112+
//Input value assumed to be between 0 and 99.
113+
void printI00(int val, char delim)
114+
{
115+
if (val < 10) Serial.print('0');
116+
Serial.print(val);
117+
Serial.print(delim);
118+
return;
119+
}
120+

0 commit comments

Comments
 (0)