DS3231, getDateStr query

Using the DS3231 spec sheet I can use following to get the date in the format '2016 12 04'.

(rtc.getDateStr(FORMAT_LONG, FORMAT_BIGENDIAN, ' '));

According to the spec there is no way of calling the individual 'year' 'Month' or 'Day'.

This I understand can only be done by directly querying the appropriate Registers.

So my question is,
in peoples opinion is it more efficient to query the Registers or employ another Library (Wire.h).

I hope this isn't a naive question but I am trying to understand the merits of both approaches.
Many thanks

in peoples opinion is it more efficient to query the Registers or employ another Library

It isn't an either/or question. The mechanism for talking to the RTC IS the Wire library. The question is what data do you get from the RTC. Obviously, getting numeric data is more efficient than getting ASCII data, in terms of the number of bytes that have to be transferred.

Thanks Paul,
Just discovered CPP files and now everything makes a lot more sense.

MarkS1964:
So my question is,
in peoples opinion is it more efficient to query the Registers or employ another Library (Wire.h).

Here's how I do it:

#include <Wire.h>

byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;
 
void setup()
{
  Wire.begin();
  Serial.begin(9600);
 
  // clear /EOSC bit
  // Sometimes necessary to ensure that the clock
  // keeps running on just battery power. Once set,
  // it shouldn't need to be reset but it's a good
  // idea to make sure.
//  Wire.beginTransmission(0x68); // address DS3231
//  Wire.write(0x0E); // select register
//  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
//  Wire.endTransmission();
}
 
void loop()
{
  // ask RTC for the time
  // 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, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
  // check for a reply from the RTC, and use it if we can
  if (Wire.available() >= 7) { 
    // if we're here, we got a reply and it is long enough
    // so now we read the time
    ss = bcd2bin(Wire.read()); // get seconds
    mi = bcd2bin(Wire.read()); // get minutes
    hh = bcd2bin(Wire.read()); // get hours
    wd = bcd2bin(Wire.read());
    dd = bcd2bin(Wire.read());
    mo = bcd2bin(Wire.read());
    yy = bcd2bin(Wire.read());
    // show that we successfully got the time
    Serial.print("Got the time: ");
    printTime();
  }
  else {
    // if we're here, that means we were unable to read the time
    Serial.println("Unable to read time from RTC"); 
  }
  delay(500);
}

byte bcd2bin(byte x) {
  // converts from binary-coded decimal to a "regular" binary number
  return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}

void printTime() {
  // just like it says on the tin
  Serial.print ("\'");
  if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
  if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
  if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print("(");
  switch (wd) {
    case 1: Serial.print("Mon"); break;
    case 2: Serial.print("Tue"); break; 
    case 3: Serial.print("Wed"); break; 
    case 4: Serial.print("Thu"); break; 
    case 5: Serial.print("Fri"); break; 
    case 6: Serial.print("Sat"); break; 
    case 7: Serial.print("Sun"); break;
    default: Serial.print("Bad");  
  }
  Serial.print(") ");
  if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
  if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
  if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");
}

This should print the date and time to the serial monitor. Remember to select the correct baud rate.

Note that I number the weekdays starting from 1 for Monday. Some people prefer to use 1 for Sunday.

Thanks for posting that odometer.

4098 bytes, it would be interesting to do a comparison with an RTC library.

As a beginner I much prefer your method, it's much clearer how things are working and easier to learn from, a direct A to B approach.

Without looking too deeply will it adapt to outputting the 2016 12 05 output?

Thanks again

MarkS1964:
Without looking too deeply will it adapt to outputting the 2016 12 05 output?

You don't have to look particularly deeply to figure that out.

Just be aware that 2016, etc., will not fit in a byte. You need at least an int for that. This only matters, though, if you intend to store the "full" year number and not just display it.