Skip to content

Commit

Permalink
Fix weekly alarms on the DS3231
Browse files Browse the repository at this point in the history
For the `DS3231_A1_Day' and `DS3231_A2_Day' alarm modes to work, the RTC
has to know the day of the week, but `adjust()' would write 0 to the
relevant register.

Fix these alarm modes by telling the RTC the day of the week in a format
suitable to it: from 1 (Monday) to 7 (Sunday).

Fixes #193.
  • Loading branch information
edgar-bonet committed Sep 21, 2020
1 parent 4f13fa5 commit 11ee4b2
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions RTClib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,16 @@ void RTC_PCF8523::calibrate(Pcf8523OffsetMode mode, int8_t offset) {
Wire.endTransmission();
}

/**************************************************************************/
/*!
@brief Convert the day of the week to a representation suitable for
storing in the DS3231: from 1 (Monday) to 7 (Sunday).
@param d Day of the week as represented by the library:
from 0 (Sunday) to 6 (Saturday).
*/
/**************************************************************************/
static uint8_t dowToDS3231(uint8_t d) { return d == 0 ? 7 : d; }

/**************************************************************************/
/*!
@brief Start I2C for the DS3231 and test succesful connection
Expand Down Expand Up @@ -1394,7 +1404,8 @@ void RTC_DS3231::adjust(const DateTime &dt) {
Wire._I2C_WRITE(bin2bcd(dt.second()));
Wire._I2C_WRITE(bin2bcd(dt.minute()));
Wire._I2C_WRITE(bin2bcd(dt.hour()));
Wire._I2C_WRITE(bin2bcd(0));
// The RTC must know the day of the week for the weekly alarms to work.
Wire._I2C_WRITE(bin2bcd(dowToDS3231(dt.dayOfTheWeek())));
Wire._I2C_WRITE(bin2bcd(dt.day()));
Wire._I2C_WRITE(bin2bcd(dt.month()));
Wire._I2C_WRITE(bin2bcd(dt.year() - 2000));
Expand Down Expand Up @@ -1523,7 +1534,7 @@ bool RTC_DS3231::setAlarm1(const DateTime &dt, Ds3231Alarm1Mode alarm_mode) {
Wire._I2C_WRITE(bin2bcd(dt.minute()) | A1M2);
Wire._I2C_WRITE(bin2bcd(dt.hour()) | A1M3);
if (DY_DT) {
Wire._I2C_WRITE(bin2bcd(dt.dayOfTheWeek()) | A1M4 | DY_DT);
Wire._I2C_WRITE(bin2bcd(dowToDS3231(dt.dayOfTheWeek())) | A1M4 | DY_DT);
} else {
Wire._I2C_WRITE(bin2bcd(dt.day()) | A1M4 | DY_DT);
}
Expand Down Expand Up @@ -1559,7 +1570,7 @@ bool RTC_DS3231::setAlarm2(const DateTime &dt, Ds3231Alarm2Mode alarm_mode) {
Wire._I2C_WRITE(bin2bcd(dt.minute()) | A2M2);
Wire._I2C_WRITE(bin2bcd(dt.hour()) | A2M3);
if (DY_DT) {
Wire._I2C_WRITE(bin2bcd(dt.dayOfTheWeek()) | A2M4 | DY_DT);
Wire._I2C_WRITE(bin2bcd(dowToDS3231(dt.dayOfTheWeek())) | A2M4 | DY_DT);
} else {
Wire._I2C_WRITE(bin2bcd(dt.day()) | A2M4 | DY_DT);
}
Expand Down

0 comments on commit 11ee4b2

Please sign in to comment.