Skip to content

Commit 3a88bef

Browse files
author
Jack Christensen
committed
Change get() method to return a zero value if RTC not present, i.e. I2C I/O error.
Change read() method to return a boolean value. Returns false if RTC not present.
1 parent 9e65a3d commit 3a88bef

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

MCP79412RTC.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ MCP79412RTC::MCP79412RTC()
3434

3535
/*----------------------------------------------------------------------*
3636
* Read the current time from the RTC and return it as a time_t value. *
37+
* Returns a zero value if RTC not present (I2C I/O error). *
3738
*----------------------------------------------------------------------*/
3839
time_t MCP79412RTC::get()
3940
{
4041
tmElements_t tm;
4142

42-
read(tm);
43-
return(makeTime(tm));
43+
if ( read(tm) )
44+
return( makeTime(tm) );
45+
else
46+
return 0;
4447
}
4548

4649
/*----------------------------------------------------------------------*
@@ -56,23 +59,27 @@ void MCP79412RTC::set(time_t t)
5659

5760
/*----------------------------------------------------------------------*
5861
* Read the current time from the RTC and return it in a tmElements_t *
59-
* structure. *
62+
* structure. Returns false if RTC not present (I2C I/O error). *
6063
*----------------------------------------------------------------------*/
61-
void MCP79412RTC::read(tmElements_t &tm)
64+
boolean MCP79412RTC::read(tmElements_t &tm)
6265
{
6366
Wire.beginTransmission(RTC_ADDR);
6467
i2cWrite(TIME_REG);
65-
Wire.endTransmission();
66-
67-
//request 7 bytes (secs, min, hr, dow, date, mth, yr)
68-
Wire.requestFrom(RTC_ADDR, tmNbrFields);
69-
tm.Second = bcd2dec(i2cRead() & ~_BV(ST));
70-
tm.Minute = bcd2dec(i2cRead());
71-
tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); //assumes 24hr clock
72-
tm.Wday = i2cRead() & ~(_BV(OSCON) | _BV(VBAT) | _BV(VBATEN)); //mask off OSCON, VBAT, VBATEN bits
73-
tm.Day = bcd2dec(i2cRead());
74-
tm.Month = bcd2dec(i2cRead() & ~_BV(LP)); //mask off the leap year bit
75-
tm.Year = y2kYearToTm(bcd2dec(i2cRead()));
68+
if (Wire.endTransmission() != 0) {
69+
return false;
70+
}
71+
else {
72+
//request 7 bytes (secs, min, hr, dow, date, mth, yr)
73+
Wire.requestFrom(RTC_ADDR, tmNbrFields);
74+
tm.Second = bcd2dec(i2cRead() & ~_BV(ST));
75+
tm.Minute = bcd2dec(i2cRead());
76+
tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); //assumes 24hr clock
77+
tm.Wday = i2cRead() & ~(_BV(OSCON) | _BV(VBAT) | _BV(VBATEN)); //mask off OSCON, VBAT, VBATEN bits
78+
tm.Day = bcd2dec(i2cRead());
79+
tm.Month = bcd2dec(i2cRead() & ~_BV(LP)); //mask off the leap year bit
80+
tm.Year = y2kYearToTm(bcd2dec(i2cRead()));
81+
return true;
82+
}
7683
}
7784

7885
/*----------------------------------------------------------------------*

MCP79412RTC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class MCP79412RTC
102102
MCP79412RTC();
103103
static time_t get(void);
104104
static void set(time_t t);
105-
static void read(tmElements_t &tm);
105+
static boolean read(tmElements_t &tm);
106106
static void write(tmElements_t &tm);
107107
void sramWrite(byte addr, byte value);
108108
void sramWrite(byte addr, byte *values, byte nBytes);

0 commit comments

Comments
 (0)