![]() |
MD_DS3231 Real Time Clock Library
1.3
Library to control DS3231 high precision clock with alarms
|
#include <MD_DS3231.h>
Public Member Functions | |
MD_DS3231 () | |
MD_DS3231 (int sda, int scl) | |
Methods for object and hardware control. | |
boolean | control (codeRequest_t item, uint8_t value) |
codeStatus_t | status (codeRequest_t item) |
Methods for RTC operations | |
boolean | readTime (void) |
boolean | writeTime (void) |
boolean | setCentury (uint8_t c) |
uint8_t | getCentury (void) |
void | now (void) |
boolean | isRunning (void) |
Methods for Alarm 1 operations | |
boolean | readAlarm1 (void) |
boolean | writeAlarm1 (almType_t almType) |
boolean | setAlarm1Type (almType_t almType) |
almType_t | getAlarm1Type (void) |
boolean | checkAlarm1 (void) |
boolean | setAlarm1Callback (void(*cb)(void)) |
Methods for Alarm 2 operations | |
boolean | readAlarm2 (void) |
boolean | writeAlarm2 (almType_t almType) |
boolean | setAlarm2Type (almType_t almType) |
almType_t | getAlarm2Type (void) |
boolean | checkAlarm2 (void) |
boolean | setAlarm2Callback (void(*cb)(void)) |
Miscellaneous methods | |
uint8_t | readRAM (uint8_t addr, uint8_t *buf, uint8_t len) |
uint8_t | writeRAM (uint8_t addr, uint8_t *buf, uint8_t len) |
uint8_t | calcDoW (uint16_t yyyy, uint8_t mm, uint8_t dd) |
float | readTempRegister (void) |
Public Attributes | |
Public variables for reading and writing time data | |
uint16_t | yyyy |
Year including the millennium and century. See setCentury() for more information. | |
uint8_t | mm |
Month (1-12) | |
uint8_t | dd |
Date of the month (1-31) | |
uint8_t | h |
Hour of the day (1-12) or (0-23) depending on the am/pm or 24h mode setting. | |
uint8_t | m |
Minutes past the hour (0-59) | |
uint8_t | s |
Seconds past the minute (0-59) | |
uint8_t | dow |
Day of the week (1-7). Sequential number; day coding depends on the application and zero is an undefined value. | |
uint8_t | pm |
Non-zero if 12 hour clock mode and PM, always zero for 24 hour clock. Check the time and if < 12 then check this indicator. | |
Core object for the MD_DS3231 library
MD_DS3231::MD_DS3231 | ( | ) |
Class Constructor
Instantiate a new instance of the class. One instance of the class is created in the libraries as the RTC object.
MD_DS3231::MD_DS3231 | ( | int | sda, |
int | scl | ||
) |
Overloaded Class Constructor (ESP8266 only)
Provides a way to assign custom SCL and SDA pins if the architecture supports them.
sda | Pin number for the SDA signal |
scl | Pin number for the SCL signal |
uint8_t MD_DS3231::calcDoW | ( | uint16_t | yyyy, |
uint8_t | mm, | ||
uint8_t | dd | ||
) |
Calculate day of week for a given date
Given the specified date, calculate the day of week.
yyyy | year for specified date. yyyy must be > 1752. |
mm | month for the specified date where mm is in the range [1..12], 1 = January. |
dd | date for the specified date in the range [1..31], where 1 = first day of the month. |
boolean MD_DS3231::checkAlarm1 | ( | void | ) |
Check if Alarm 1 has triggered
The method checks if the alarm triggered flag has been set. If it has the callback function is invoked. In either case the triggered flag is reset.
boolean MD_DS3231::checkAlarm2 | ( | void | ) |
Check if Alarm 2 has triggered
The method checks if the alarm triggered flag has been set. If it has the callback function is invoked. In either case the triggered flag is reset.
boolean MD_DS3231::control | ( | codeRequest_t | item, |
uint8_t | value | ||
) |
Set the control status of the specified parameter to the specified value.
The device has a number of control parameters that can be set through this method. The type of control action required is passed through the mode parameter and should be one of the control actions defined by codeRequest_t. The value that needs to be supplied on the control action required is one of the defined actions in codeStatus_t or a numeric parameter suitable for the control action. Not all combinations of item and value are valid.
item | one of the codeRequest_t values. |
value | one codeStatus_t values. |
almType_t MD_DS3231::getAlarm1Type | ( | void | ) |
Get the Alarm 1 trigger type
Read the alarm trigger from the RTC. This will be one of the values in almType_t, noting that not all values are valid for each alarm.
almType_t MD_DS3231::getAlarm2Type | ( | void | ) |
Get the Alarm 2 trigger type
Read the alarm trigger from the RTC. This will be one of the values in almType_t, noting that not all values are valid for each alarm.
uint8_t MD_DS3231::getCentury | ( | void | ) |
Get the current century for year handling in the library
Return the current century library setting.
boolean MD_DS3231::isRunning | ( | void | ) |
Compatibility function - Check if RTC is running
Wrapper for a HALT status check
void MD_DS3231::now | ( | void | ) |
Compatibility function - Read the current time
Wrapper to read the current time.
boolean MD_DS3231::readAlarm1 | ( | void | ) |
Read the current Alarm 1 time into the interface registers
Query the RTC for the current alarm 1 trigger time and load it into the library interface registers (dd, h, m, s, dow, pm) from which the data can be accessed.
boolean MD_DS3231::readAlarm2 | ( | void | ) |
Read the current Alarm 2 time into the interface registers
Query the RTC for the current alarm 2 trigger time and load it into the library interface registers (dd, h, m, s, dow, pm) from which the data can be accessed.
uint8_t MD_DS3231::readRAM | ( | uint8_t | addr, |
uint8_t * | buf, | ||
uint8_t | len | ||
) |
Read the raw RTC clock data
Read len bytes from the RTC clock starting at addr as raw data into the buffer supplied. The size of the buffer should be at least MAX_BUF bytes long (defined in the library cpp file).
addr | starting address for the read. |
buf | address of the receiving byte buffer. |
len | number of bytes to read. |
float MD_DS3231::readTempRegister | ( | void | ) |
Read the temperature register in the RTC
Read the temperature compensation register in the RTC in degrees C. Resolution is to 0.25 degrees C on the fractional part.
boolean MD_DS3231::readTime | ( | void | ) |
Read the current time into the interface registers
Query the RTC for the current time and load that into the library interface registers (yyyy, mm, dd, h, m, s, dow, pm) from which the data can be accessed.
boolean MD_DS3231::setAlarm1Callback | ( | void(*)(void) | cb | ) |
Set the callback function for Alarm 1
Pass the address of the callback function to the libraries. The callback function prototype is
void functionName(void);
and is invoked when the checkAlarm1() method is invoked. Set to NULL (default) to disable this feature.
cb | the address of the callback function. |
boolean MD_DS3231::setAlarm1Type | ( | almType_t | almType | ) |
Set the Alarm 1 trigger type
Set the alarm trigger to one of the values in almType_t, noting that not all values are valid for each alarm.
almType | the type of alarm trigger required |
boolean MD_DS3231::setAlarm2Callback | ( | void(*)(void) | cb | ) |
Set the callback function for Alarm 2
Pass the address of the callback function to the libraries. The callback function prototype is
void functionName(void);
and is invoked when the checkAlarm2() method is invoked. Set to NULL (default) to disable this feature
cb | the address of the callback function. |
boolean MD_DS3231::setAlarm2Type | ( | almType_t | almType | ) |
Set the Alarm 2 trigger type
Set the alarm trigger to one of the values in almType_t, noting that not all values are valid for each alarm.
almType | the type of alarm trigger required |
boolean MD_DS3231::setCentury | ( | uint8_t | c | ) |
Set the current century for year handling in the library
The RTC only stores the last 2 digits of the year and has a century indicator. This means in practice that dates in a range of 199 years from a base date can be represented by the library. This method allows user code to set a base century for date handling in the library. The default for this value in the libraries is 20, which allows dates in the range from 2000 (20*100) to 2199.
c | the year base century. Dates will start from (c*100). |
codeStatus_t MD_DS3231::status | ( | codeRequest_t | item | ) |
Obtain the current setting for the specified parameter.
Any of the parameters that can be set from the control() method can be queried using this method. The codeStatus_t value returned will be one of the valid values for the control() method for each specific codeRequest_t parameter.
item | one of the codeRequest_t values. |
boolean MD_DS3231::writeAlarm1 | ( | almType_t | almType | ) |
Write the current Alarm 1 time from the interface registers
Write the data in the interface registers (dd, h, m, s, dow, pm) as the Alarm 1 trigger time in the RTC and set the alarm trigger to one of the values in almType_t, noting that not all values are valid for each alarm.
almType | the type of alarm trigger required |
boolean MD_DS3231::writeAlarm2 | ( | almType_t | almType | ) |
Write the current Alarm 2 time from the interface registers
Write the data in the interface registers (dd, h, m, s, dow, pm) as the Alarm 2 trigger time in the RTC and set the alarm trigger to one of the values in almType_t, noting that not all values are valid for each alarm.
almType | the type of alarm trigger required |
uint8_t MD_DS3231::writeRAM | ( | uint8_t | addr, |
uint8_t * | buf, | ||
uint8_t | len | ||
) |
Write the raw RTC clock data
Write len bytes of data in the buffer supplied to the RTC clock starting at addr. The size of the buffer should be at least len bytes long.
addr | starting address for the write. |
buf | address of the data buffer. |
len | number of bytes to write. |
boolean MD_DS3231::writeTime | ( | void | ) |
Write the current time from the interface registers
Write the data in the interface registers (yyyy, mm, dd, h, m, s, dow, pm) as the current time in the RTC.