The following methods of DS3231 object use I2C hardware connection and Wire library to read data from certain registers in DS3231. The Library assumes that DS3231 has an I2C address of 0x68.
The DS3231 register addresses mentioned below are documented on page 11 of manufacturer's datasheet .
The examples provided below assume a variable has been declared as follows:
DS3231 myRTC;
/* * returns: byte = 0 to 59 * parameters: none * asserts: none * side effects: none * DS3231 register addressed: 0x00 */ byte theSecond = myRTC.getSecond();
/* * returns: byte = 0 to 59 * parameters: none * asserts: none * side effects: none * DS3231 register addressed: 0x01 */ byte theMinute = myRTC.getMinute();
/*H******************************************************* * returns: byte, value depending on mode settings in DS3231 either 1 to * 12 (12-hour mode) or 0 to 23 (24-hour mode) * parameters: passed by reference * parameter #1: 12/24-hour flag * parameter #2: AM/PM flag * Note: must provide variable names, not constants asserts: none * side effects: * two boolean parameters are set or cleared according to state of flags * in DS3231 hardware register 12/24 hour flag = true if DS3231 is in * 12-hour mode AM/PM flag = false if AM, true if PM, in 12-hour mode * DS3231 register addressed: 0x02 ********************************************************/ //************************* DEFINES ************************************ //************************* PROTOTYPES ************************************ //************************* VARIABLES ************************************ // declare global variables to be passed into function bool h12, hPM, theHour = myRTC.getHour( h12, hPM ); // example of printing hour to Serial monitor Serial.print("The hour is "); Serial.print( theHour ); // VALUE RETURNED // TEST VALUES ALTERED BY SIDE-EFFECTS if( h12 == true) { // 12-HOUR MODE if( hPM == true) Serial.println(" PM."); } else Serial.println(" AM."); else // 24-HOUR MODE Serial.println(" in 24-hour mode.");
Note that supplying boolean constants as parameters will halt program compilation with an error. The parameters must be names of boolean variables defined in program code.
/*H******************************************************* * returns: byte = 1 to 7 * parameters: none * asserts: none * side effects: none * DS3231 register addressed: 0x03 ********************************************************/ byte theWeekday = myRTC.getDoW();
/*H******************************************************* * returns: byte = 1 to 28, 29, 30 or 31, depending on month and year * parameters: none * asserts: none * side effects: none * DS3231 register addressed: 0x04 ********************************************************/ byte theDate = myRTC.getDate();
/*H******************************************************* * returns: byte = 1 to 12 * parameters: one boolean variable, passed by reference * asserts: none * side effects: * boolean parameter is set or cleared * according to value of "Century" flag * in hardware register of DS3231 * DS3231 register addressed: 0x05 ********************************************************/ // declare a variable to receive Century bit bool CenturyBit; byte theDate = myRTC.getMonth(CenturyBit);
Note: according to datasheet, "The century bit (bit 7 of month register) is toggled when years register overflows from 99 to 00."
Note also that supplying a boolean constant as parameter will halt program compilation with an error. The parameter must be name of a boolean variable defined in program code.
/*H******************************************************* * returns: byte = 00 to 99 * parameters: none * asserts: none * side effects: none * DS3231 register addressed: 0x06 ********************************************************/ byte theDate = myRTC.getDate();
The Century bit may be useful when operating DS3231 near end of a century. For example, bit would have toggled when year changed from 1999 to 2000. It would have been important to recognize that a year "00" actually represented an increase of time compared to year "99".
The bit will toggle again when year changes from 2099 to 2100, and so forth.
For reasons best understood by its designers, Century bit is stored in "month" register of DS3231, rather than in "year" register.
It might have been nicer if DS3231 afforded capacity to maintain a 4-digit year value.
We users of device might find little use for Century bit during years 2000 through 2098 or so. Anyone planning to use this Library with a DS3231 in year 2099 may wish to experiment with code to evaluate and correctly use Century bit.
My beard will probably not grow long enough for me to reach that future era. Even so, by then I would probably look for a different RTC chip. The reason is DS3231 makes no promise to handle Leap Years correctly in or after year 2100.