Skip to content

Commit 6f145d8

Browse files
authored
Merge pull request #2 from jrbenito/master
Correct bug in examples and add Travis-ci and also add reading power outage timestamps
2 parents 358b346 + ab10f7f commit 6f145d8

File tree

7 files changed

+127
-8
lines changed

7 files changed

+127
-8
lines changed

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: python
2+
python:
3+
- "2.7"
4+
5+
# Cache PlatformIO packages using Travis CI container-based infrastructure
6+
sudo: false
7+
cache:
8+
directories:
9+
- "~/.platformio"
10+
11+
env:
12+
- PLATFORMIO_CI_SRC=examples/alarm.ino
13+
- PLATFORMIO_CI_SRC=examples/clock.ino
14+
- PLATFORMIO_CI_SRC=examples/tm_format.ino
15+
16+
install:
17+
- pip install -U platformio
18+
19+
script:
20+
- platformio ci --lib=. --board=megaatmega2560 --board uno

RTCC_MCP7940N.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,56 @@ bool RTCC_MCP7940N::IsOscillatorRunning()
208208
return this->GetFlag(0x03, B100000);
209209
}
210210

211-
bool RTCC_MCP7940N::HasPowerFailed()
212-
{
213-
return this->GetFlag(0x03, B10000);
211+
bool RTCC_MCP7940N::HasPowerFailed(rtcc_time *dwTime, rtcc_time *upTime)
212+
{
213+
byte year;
214+
bool pwFail;
215+
216+
year = this->GetByte(0x06);
217+
pwFail = this->GetFlag(0x03, B10000);
218+
219+
if (pwFail) {
220+
221+
// set read address to POWER DOWN Time-Stamp
222+
Wire.beginTransmission(this->i2c_addr);
223+
Wire.write(0x18);
224+
Wire.endTransmission();
225+
226+
// Request 8 bytes for both Time-stamps
227+
Wire.requestFrom(this->i2c_addr, 8, true);
228+
byte time_data[8];
229+
Wire.readBytes(time_data, 8);
230+
231+
// convert
232+
dwTime->secten = 0; //(time_data[0] & B1110000) >> 4;
233+
dwTime->secone = 0; //time_data[0] & B1111;
234+
dwTime->minten = (time_data[0] & B111000) >> 4;
235+
dwTime->minone = time_data[0] & B1111;
236+
dwTime->hrten = (time_data[1] & B110000) >> 4;
237+
dwTime->hrone = time_data[1] & B1111;
238+
dwTime->wkday = 0; //time_data[3] & B111; not provided
239+
dwTime->dateten = (time_data[2] & B110000) >> 4;
240+
dwTime->dateone = time_data[2] & B1111;
241+
dwTime->mthten = (time_data[3] & B10000) >> 4;
242+
dwTime->mthone = time_data[3] & B1111;
243+
dwTime->yrten = (year & B11110000) >> 4;
244+
dwTime->yrone = year & B1111;
245+
dwTime->secten = 0; //(time_data[0] & B1110000) >> 4;
246+
dwTime->secone = 0; //time_data[0] & B1111;
247+
dwTime->minten = (time_data[4] & B111000) >> 4;
248+
dwTime->minone = time_data[4] & B1111;
249+
dwTime->hrten = (time_data[5] & B110000) >> 4;
250+
dwTime->hrone = time_data[5] & B1111;
251+
dwTime->wkday = 0; //time_data[3] & B111; not provided
252+
dwTime->dateten = (time_data[6] & B110000) >> 4;
253+
dwTime->dateone = time_data[6] & B1111;
254+
dwTime->mthten = (time_data[7] & B10000) >> 4;
255+
dwTime->mthone = time_data[7] & B1111;
256+
dwTime->yrten = (year & B11110000) >> 4;
257+
dwTime->yrone = year & B1111;
258+
}
259+
260+
return pwFail;
214261
}
215262

216263
bool RTCC_MCP7940N::GetBatteryEnabled()

RTCC_MCP7940N.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class RTCC_MCP7940N
141141
*
142142
* @return True if a power fail event has been detected
143143
*/
144-
bool HasPowerFailed();
144+
bool HasPowerFailed(rtcc_time *dwTime, rtcc_time *upTime);
145145

146146
/**
147147
* Clear the power failure flag so it can be raised again if another failure is detected

examples/alarm.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#define PIN_INT (3)
99

10-
RTCC_MCP7940N rtc();
10+
RTCC_MCP7940N rtc;
1111

1212
volatile bool itr = false;
1313

examples/clock.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <Wire.h>
66
#include <RTCC_MCP7940N.h>
77

8-
RTCC_MCP7940N rtc();
8+
RTCC_MCP7940N rtc;
99

1010
void setup()
1111
{

examples/power_outage.ino

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Part of RTCC (MCP7940N) Arduino library
2+
* Copyright (C) 2017 Kane Wallmann
3+
* See LICENCE.txt for license (MIT)*/
4+
5+
#include <Wire.h>
6+
#include <RTCC_MCP7940N.h>
7+
8+
RTCC_MCP7940N rtc;
9+
10+
void setup()
11+
{
12+
Serial.begin(9600);
13+
14+
// Must call before using rtcc
15+
Wire.begin();
16+
17+
// Use crystal for timekeeping
18+
rtc.SetOscillatorEnabled(true);
19+
rtc.SetExternalOscillatorEnabled(false);
20+
}
21+
22+
void loop()
23+
{
24+
char buff[20];
25+
bool pwrOutage;
26+
27+
// rtcc_time struct represents how the data is stored on RTCC chip, see tm_format example for info on converting
28+
rtcc_time time, dwTime, upTime;
29+
rtc.ReadTime(&time);
30+
31+
sprintf(buff, "%u%u-%u%u-%u%u %u%u:%u%u:%u%u", time.dateten, time.dateone, time.mthten, time.mthone, time.yrten,
32+
time.yrone, time.hrten, time.hrone, time.minten, time.minone, time.secten, time.secone);
33+
Serial.println(buff);
34+
35+
pwrOutage = rtc.HasPowerFailed(&dwTime, &upTime);
36+
if (pwrOutage) {
37+
Serial.println("*****NEW OUTAGE*****");
38+
}
39+
else {
40+
Serial.println("LastOutage: ");
41+
}
42+
Serial.print(" From: ");
43+
sprintf(buff, "%u%u-%u%u-%u%u %u%u:%u%u:%u%u", dwTime.dateten, dwTime.dateone, dwTime.mthten, dwTime.mthone, dwTime.yrten,
44+
dwTime.yrone, dwTime.hrten, dwTime.hrone, dwTime.minten, dwTime.minone, dwTime.secten, dwTime.secone);
45+
Serial.println(buff);
46+
Serial.print(" To: ");
47+
sprintf(buff, "%u%u-%u%u-%u%u %u%u:%u%u:%u%u", upTime.dateten, upTime.dateone, upTime.mthten, upTime.mthone, upTime.yrten,
48+
upTime.yrone, upTime.hrten, upTime.hrone, upTime.minten, upTime.minone, upTime.secten, upTime.secone);
49+
Serial.println(buff);
50+
51+
delay(10000);
52+
}

examples/tm_format.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <RTCC_MCP7940N.h>
77
#include <time.h>
88

9-
RTCC_MCP7940N rtc();
9+
RTCC_MCP7940N rtc;
1010

1111
void setup()
1212
{
@@ -30,7 +30,7 @@ void loop()
3030

3131
// Covner to tm format
3232
tm tm_time;
33-
rtc.ConverTime( &time, &tm_time );
33+
rtc.ConvertTime( &time, &tm_time );
3434

3535
// Format time and output to string
3636
strftime( buff, 20, "%d-%m-%y %H:%M:%s", &tm_time );

0 commit comments

Comments
 (0)