Skip to content

Commit

Permalink
Added Aging register get/set on DS323X RTCs (agingGet and agingSet fu…
Browse files Browse the repository at this point in the history
…nctions).

Version bump: 6.4.0
  • Loading branch information
Naguissa committed Aug 6, 2020
1 parent a2df533 commit 23d0a6e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ meltingSnowdrift - https://github.com/meltingSnowdrift - Find unhandled centiry
David Brown - https://github.com/davidhbrown - Fix century bit on year read.
WoBo68 - https://github.com/WoBo68 - Power lost support for DS1307.
mrfaptastic - https://github.com/mrfaptastic - enableBattery support (DS3231 and DS3232)
Eleksir - https://github.com/eleksir - DS3231 and DS3232 aging factor register get/set
6 changes: 6 additions & 0 deletions examples/uRTCLib_example_full/uRTCLib_example_full.ino
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ delay (2000);
}


Serial.print("Aging register value: ");
Serial.println(rtc.agingGet());

Serial.println("Not changing aging register value. To do so you can execute: rtc.agingSet(newValue)");


Serial.println("Testing SQWG/INT output:");

Serial.println("fixed 0:");
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=uRTCLib
version=6.3.0
version=6.4.0
author=Naguissa <naguissa@foroelectro.net>
maintainer=Naguissa <naguissa@foroelectro.net>
sentence=Really tiny library to basic RTC functionality on Arduino. DS1307, DS3231 and DS3232 RTCs are supported. See https://github.com/Naguissa/uEEPROMLib for EEPROM support. Temperature, Alarms, SQWG, Power lost and RAM support.
Expand Down
87 changes: 85 additions & 2 deletions src/uRTCLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @see <a href="https://www.foroelectro.net/librerias-arduino-ide-f29/rtclib-arduino-libreria-simple-y-eficaz-para-rtc-y-t95.html">https://www.foroelectro.net/librerias-arduino-ide-f29/rtclib-arduino-libreria-simple-y-eficaz-para-rtc-y-t95.html</a>
* @see <a href="mailto:naguissa@foroelectro.net">naguissa@foroelectro.net</a>
* @see <a href="https://github.com/Naguissa/uEEPROMLib">See uEEPROMLib for EEPROM support.</a>
* @version 6.3.0
* @version 6.4.0
*/

#include <Arduino.h>
Expand Down Expand Up @@ -118,7 +118,7 @@ void uRTCLib::refresh() {
uRTCLIB_YIELD
_year = uRTCLIB_bcdToDec(_year);

_temp = 9999; // Some obvious error value
_temp = URTCLIB_TEMP_ERROR; // Some obvious error value

// Now we need to read extra requested bytes depending on the RTC model again:
switch (_model) {
Expand Down Expand Up @@ -1333,4 +1333,87 @@ bool uRTCLib::ramWrite(const uint8_t address, byte data) {
}



/**
* \brief Reads actual aging value on the RTC
*
* @return Aging register value on RTC, 2-complement recalculated (use as regular int8_t)
*/
int8_t uRTCLib::agingGet() {
int8_t ret = 0;
switch (_model) {
case URTCLIB_MODEL_DS3231:
case URTCLIB_MODEL_DS3232:
URTCLIB_WIRE.beginTransmission(_rtc_address);
uRTCLIB_YIELD
URTCLIB_WIRE.write(0x10);
uRTCLIB_YIELD
URTCLIB_WIRE.endTransmission();
uRTCLIB_YIELD
URTCLIB_WIRE.requestFrom(_rtc_address, 1);
uRTCLIB_YIELD
ret = URTCLIB_WIRE.read(); //2's complement portion
if (ret & 0b10000000) {
ret--;
}
break;

}
return ret;
}

/**
* \brief Sets aging value on the RTC
*
* @param val new value (use as regular int8_t, 2-complement conversion is done internally)
*
* @return True when executed, false if RTC doesn't support it.
*/
bool uRTCLib::agingSet(int8_t val) {
bool ret = false;
switch (_model) {
case URTCLIB_MODEL_DS3231:
case URTCLIB_MODEL_DS3232:
if (val < 0) {
val++;
}
URTCLIB_WIRE.beginTransmission(_rtc_address);
uRTCLIB_YIELD
URTCLIB_WIRE.write(0x10);
uRTCLIB_YIELD
URTCLIB_WIRE.write(val);
uRTCLIB_YIELD
URTCLIB_WIRE.endTransmission();
uRTCLIB_YIELD

ret = true;

// Read status register 0x0E
URTCLIB_WIRE.beginTransmission(_rtc_address);
uRTCLIB_YIELD
URTCLIB_WIRE.write(0x0E);
uRTCLIB_YIELD
URTCLIB_WIRE.endTransmission();
uRTCLIB_YIELD
URTCLIB_WIRE.requestFrom(_rtc_address, 1);
uRTCLIB_YIELD
byte status = URTCLIB_WIRE.read();
uRTCLIB_YIELD

// Enable CONV bit on status register 0x0E to apply changes inmediately
status |= 0b00100000;
URTCLIB_WIRE.beginTransmission(_rtc_address);
uRTCLIB_YIELD
URTCLIB_WIRE.write(0x0E);
uRTCLIB_YIELD
URTCLIB_WIRE.write(status);
uRTCLIB_YIELD
URTCLIB_WIRE.endTransmission();
uRTCLIB_YIELD

}
return ret;
}


/*** EEPROM functionality has been moved to separate library: https://github.com/Naguissa/uEEPROMLib ***/
8 changes: 7 additions & 1 deletion src/uRTCLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @see <a href="https://www.foroelectro.net/librerias-arduino-ide-f29/rtclib-arduino-libreria-simple-y-eficaz-para-rtc-y-t95.html">https://www.foroelectro.net/librerias-arduino-ide-f29/rtclib-arduino-libreria-simple-y-eficaz-para-rtc-y-t95.html</a>
* @see <a href="mailto:naguissa@foroelectro.net">naguissa@foroelectro.net</a>
* @see <a href="https://github.com/Naguissa/uEEPROMLib">See uEEPROMLib for EEPROM support.</a>
* @version 6.3.0
* @version 6.4.0
*/
/** \file uRTCLib.h
* \brief uRTCLib header file
Expand Down Expand Up @@ -336,6 +336,12 @@
byte ramRead(const uint8_t);
bool ramWrite(const uint8_t, byte);

/************ Aging *************/
// Only DS3231 and DS3232. Address 0x10h
int8_t agingGet();
bool agingSet(int8_t);


private:
// Address
int _rtc_address = URTCLIB_ADDRESS;
Expand Down

0 comments on commit 23d0a6e

Please sign in to comment.