Finally got to the bottom of the issue. Below is the standard function to read the RTC registers and pack in a DateTime struct/object:
DateTime DS3231::getTime()
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.write((byte)0);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 7);
uint8_t ss = bcd2bin(Wire.read() & 0x7F);
uint8_t mm = bcd2bin(Wire.read());
uint8_t hh = bcd2bin(Wire.read());
uint8_t dow= Wire.read();
uint8_t d = bcd2bin(Wire.read());
uint8_t m = bcd2bin(Wire.read());
uint16_t y = bcd2bin(Wire.read()) + 2000;
return DateTime (y, m, d, dow, hh, mm, ss);
}
This does not work - we’ll come to why later.
I had added a function to read the temperature on the DS3231 which worked fine.
float DS3231::getTemp()
{
uint8_t _msb = read_i2c_register(DS3231_ADDRESS, DS3231_REG_TEMPM);
uint8_t _lsb = read_i2c_register(DS3231_ADDRESS, DS3231_REG_TEMPL);
return (float)_msb + ((_lsb >> 6) * 0.25f);
}
The obvious difference was the use of the read_i2c_register() function and the reading of one register at a time. I modified the getTime() function to use this function and read register by register - and now it works. Thus, setting the pointer to the first RTC register and requesting 7 bytes (as had been coded) does not work. I would be interested to understand why but for now I need to move on as I have wasted enough time on trying to solve this.
DateTime DS3231::getTime()
{
uint8_t temp;
temp = read_i2c_register(DS3231_ADDRESS, DS3231_REG_SEC);
uint8_t ss = bcd2bin(temp & 0x7F);
temp = read_i2c_register(DS3231_ADDRESS, DS3231_REG_MIN);
uint8_t mm = bcd2bin(temp);
temp = read_i2c_register(DS3231_ADDRESS, DS3231_REG_HOUR);
uint8_t hh = bcd2bin(temp);
temp = read_i2c_register(DS3231_ADDRESS, DS3231_REG_DOW);
uint8_t dow = temp & 0x07;
temp = read_i2c_register(DS3231_ADDRESS, DS3231_REG_DATE);
uint8_t d = bcd2bin(temp);
temp = read_i2c_register(DS3231_ADDRESS, DS3231_REG_MON);
uint8_t m = bcd2bin(temp);
temp = read_i2c_register(DS3231_ADDRESS, DS3231_REG_YEAR);
uint16_t y = bcd2bin(temp) + 2000;
return DateTime (y, m, d, dow, hh, mm, ss);
}