DS3231 Code Question

Im using a Maxim DS3231 Real Time Clock IC.

I found a working code on google:

Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());

in the line
*second = bcdToDec(Wire.read() & 0x7f);
is a 0x7f added,
and in the line

*hour = bcdToDec(Wire.read() & 0x3f);

a 0x3f.

I think the 0x3f for hour, is because of the am/pm 12/24 thing, if you look at the datasheet.

But im wondering what this 0x7f is doing there? Why is is it needed? If you look at the datasheet, minutes and seconds in the register map are looking complete the same.

Any idea what 0x7f is for?

P.S. The code works fine, i just wanna understand it...

There is no obvious reason that I can see (and will no doubt be corrected if I'm wrong!), but I suspect that the code was copied from the DS1302/7. On those RTCs the MSB of the seconds register is the CH (clock halt) flag which you don't want to include in the time.

dannable:
There is no obvious reason that I can see (and will no doubt be corrected if I'm wrong!), but I suspect that the code was copied from the DS1302/7. On those RTCs the MSB of the seconds register is the CH (clock halt) flag which you don't want to include in the time.

should i try without it? :slight_smile:

Nothing will explode. Well, I hope not... :wink:

is a 0x7f added,

sp. "anded"

It's a goofy mistake (not checking to see whether the RTC chips are different). If it's supposed to work with multiple RTC types, they should have documented that. You can't learn much from an idiot except how to be one. Download a decent DS3231 library and see how it's supposed to be done.

The code you use looks rather like what I use for the DS1307 and you might find it better explained here.

I now use it with the 3231, no alteration required.

Nick_Pyner:
I now use it with the 3231, no alteration required.

There would be if you were using any of the DS3231 specific features. One being setting the time, because I think the example software given there to perform that function, would not work with the DS3231. This segment writes a zero first, which I believe is not correct for it.

 Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
//...

If all you need is an agnostic read of the time, well, okay.

How are you planning to set the time?

I just used the timesetting programme from the same source. It was OK but, now that you raise the matter, I will check it out. That Arduino has not gone into service.