Hi!
I'm trying to expand Stephanie Maks' Chronodot library for Arduino with a few functions (ie get only time and only date, not both) and I have encountered an odd thing that I can't explain.
In now (get dateTime) function she has 20 received bytes, although only 19 are requested. What is that 20th byte?
// Get current time and date
DateTime Chronodot::now() {
// Set register address to 0x00 - second register
Wire.beginTransmission(CHRONODOT_ADDRESS);
#if ARDUINO >= 100
Wire.write((byte)0);
#else
Wire.send(0);
#endif
Wire.endTransmission();
// Request 19 (all) registers, beginning from register 0x00 - second register
Wire.requestFrom(CHRONODOT_ADDRESS, 19);
byte blah[20];
int i;
for (i = 0; i < 20; i++) {
#if ARDUINO >= 100
blah[i] = Wire.read();
#else
blah[i] = Wire.receive();
#endif
}
uint8_t ss = bcd2bin(blah[0] & 0x7F);
uint8_t mm = bcd2bin(blah[1]);
uint8_t hh = bcd2bin(blah[2]);
uint8_t d = bcd2bin(blah[4]);
uint8_t m = bcd2bin(blah[5]);
uint16_t y = bcd2bin(blah[6]) + 2000;
float ttc = (float)(int)blah[17];
byte portion = blah[18];
if (portion == 0b01000000) ttc += 0.25;
if (portion == 0b10000000) ttc += 0.5;
if (portion == 0b11000000) ttc += 0.75;
float degF = (((ttc * 9.0) / 5.0) + 32.5);
int ttf = (int)degF;
return DateTime(y, m, d, hh, mm, ss, ttf, ttc);
}
Some other people, for example, only mention it, but never actually read it. Wouldn't that break the communication?
void get_date()
{
Wire.beginTransmission(104);
Wire.send(3);//set register to 3 (day)
Wire.endTransmission();
Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
day = bcdToDec(Wire.receive());
date = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
}
But that same person doesn't even mention it when getting time, only date.
Official example also doesn't have this.
void loop()
{
// send request to receive data starting at register 0
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write((byte)0); // start at register 0
Wire.endTransmission();
Wire.requestFrom(0x68, 3); // request three bytes (seconds, minutes, hours)
while(Wire.available())
{
int seconds = Wire.read(); // get seconds
int minutes = Wire.read(); // get minutes
int hours = Wire.read(); // get hours
seconds = (((seconds & 0b11110000)>>4)*10 + (seconds & 0b00001111)); // convert BCD to decimal
minutes = (((minutes & 0b11110000)>>4)*10 + (minutes & 0b00001111)); // convert BCD to decimal
hours = (((hours & 0b00100000)>>5)*20 + ((hours & 0b00010000)>>4)*10 + (hours & 0b00001111)); // convert BCD to decimal (assume 24 hour mode)
Serial.print(hours); Serial.print(":"); Serial.print(minutes); Serial.print(":"); Serial.println(seconds);
}
delay(1000);
}
Could someone who has more experience please shed some light on this? Thanks very much!
P.S.: Yes, I have gone through datasheet and I didn't find any explanation.