MCP7940
v1.2.0
Arduino Library for the MCP7940N and MCP7940M Real-time clocks
|
Main class definition with forward declarations. More...
Public Member Functions | |
~MCP7940_Class () | |
Unused Class constructor. | |
bool | begin (const uint32_t i2cSpeed) const |
Unused Class destructor. More... | |
bool | begin (const uint8_t sda=SDA, const uint8_t scl=SCL, const uint32_t i2cSpeed=I2C_STANDARD_MODE) const |
bool | deviceStatus () const |
bool | deviceStart () const |
bool | deviceStop () const |
DateTime | now () const |
void | adjust () |
void | adjust (const DateTime &dt) |
int8_t | calibrate () const |
int8_t | calibrate (const int8_t newTrim) |
int8_t | calibrate (const DateTime &dt) |
int8_t | calibrate (const float fMeas) const |
int8_t | getCalibrationTrim () const |
uint8_t | weekdayRead () const |
uint8_t | weekdayWrite (const uint8_t dow) const |
bool | setMFP (const bool value) const |
uint8_t | getMFP () const |
bool | setAlarm (const uint8_t alarmNumber, const uint8_t alarmType, const DateTime &dt, const bool state=true) const |
void | setAlarmPolarity (const bool polarity) const |
DateTime | getAlarm (const uint8_t alarmNumber, uint8_t &alarmType) const |
bool | clearAlarm (const uint8_t alarmNumber) const |
bool | setAlarmState (const uint8_t alarmNumber, const bool state) const |
bool | getAlarmState (const uint8_t alarmNumber) const |
bool | isAlarm (const uint8_t alarmNumber) const |
uint8_t | getSQWSpeed () const |
bool | setSQWSpeed (uint8_t frequency, bool state=true) const |
bool | setSQWState (const bool state) const |
bool | getSQWState () const |
bool | setBattery (const bool state) const |
bool | getBattery () const |
bool | getPowerFail () const |
bool | clearPowerFail () const |
DateTime | getPowerDown () const |
DateTime | getPowerUp () const |
int8_t | calibrateOrAdjust (const DateTime &dt) |
int32_t | getPPMDeviation (const DateTime &dt) const |
void | setSetUnixTime (uint32_t aTime) |
uint32_t | getSetUnixTime () const |
template<typename T > | |
uint8_t | readRAM (const uint8_t &addr, T &value) const |
template<typename T > | |
uint8_t | writeRAM (const uint8_t &addr, const T &value) const |
template<typename T > | |
uint8_t | readEUI (const uint8_t &addr, T &value) const |
template<typename T > | |
uint8_t | writeEUI (const uint8_t &addr, T &value) const |
Main class definition with forward declarations.
void MCP7940_Class::adjust | ( | ) |
sets the current date/time (overloaded)
This is an overloaded function. With no parameters then the RTC is set to the date/time when the program was compiled. Since different core version handle these compiler variables differently, the values are copied to temporary strings and then passed to the adjust() function
void MCP7940_Class::adjust | ( | const DateTime & | dt | ) |
sets the current date/time (overloaded)
This is an overloaded function. Set to the DateTime class instance value. The oscillator is stopped during the process and is restarted upon completion.
bool MCP7940_Class::begin | ( | const uint32_t | i2cSpeed | ) | const |
Unused Class destructor.
Start I2C device communications
Starts I2C comms with the device, using a default speed if one is not specified
[in] | i2cSpeed | defaults to I2C_STANDARD_MODE, otherwise use speed in Herz |
bool MCP7940_Class::begin | ( | const uint8_t | sda = SDA , |
const uint8_t | scl = SCL , |
||
const uint32_t | i2cSpeed = I2C_STANDARD_MODE |
||
) | const |
Start I2C device communications
Starts I2C comms with the device, using default SDA and SCL ports as well as speed if they are not specified
[in] | sda | defaults to PIN_WIRE_SDA, otherwise use pin (ignored if not ESP8266) |
[in] | scl | defaults to PIN_WIRE_SCL, otherwise use pin (ignored if not ESP8266) |
[in] | i2cSpeed | defaults to I2C_STANDARD_MODE, otherwise use speed in Herz |
int8_t MCP7940_Class::calibrate | ( | ) | const |
Calibrate the MCP7940 (overloaded)
When called with no parameters the internal calibration is reset to 0
int8_t MCP7940_Class::calibrate | ( | const int8_t | newTrim | ) |
Calibrate the MCP7940 (overloaded)
When called with one int8 parameter that value is used as the new trim value
[in] | newTrim | New signed integer value to use for the trim register |
int8_t MCP7940_Class::calibrate | ( | const DateTime & | dt | ) |
Calibrate the MCP7940 (overloaded)
When called with a DateTime class value then an internal calibration is performed. Accepts a current date/time value and compares that to the current date/time of the RTC and computes a calibration factor depending upon the time difference between the two and how long the timespan between the two is. The longer the period between setting the clock and comparing the difference between real time and indicated time the better the resulting calibration accuracy will be. The datasheet explains the calibration formula on page 28. First, the error in parts-per-million is computed using PPM = (SecDev/ExpectedSeconds) * 10000000. Then the trim register is computed using the formula TRIMVAL = (PPM*32768*60)/(1000000*2). The code below is designed for easy reading, not for performance. The ppm <> 130 is computed by solving the equation for the maximum TRIM value of 127 and computing the corresponding max and min values for ppm. This allows the trim variable to be one byte rather than a long integer.
[in] | dt | Actual Date/time |
int8_t MCP7940_Class::calibrate | ( | const float | fMeas | ) | const |
Calibrate the MCP7940 (overloaded)
When called with one floating point value then that is used as the measured frequency
[in] | fMeas | Measured frequency in Herz |
int8_t MCP7940_Class::calibrateOrAdjust | ( | const DateTime & | dt | ) |
Calibrate the MCP7940 if the ppm deviation is < 130 and > -130 else Adjust the datetime.
If the time had changed significantly (like happens during daylight savings time) then just adjust the time to be the new time and leave the trim value alone. One hour deviation in 6 months is 225 ppm. If the ppm deviation is within -130 to 130 then assume we are just calibrating the clock.
[in] | dt | Actual Date/time |
bool MCP7940_Class::clearAlarm | ( | const uint8_t | alarmNumber | ) | const |
Clears the given alarm
[in] | alarmNumber | Alarm number 0 or 1 |
bool MCP7940_Class::clearPowerFail | ( | ) | const |
Clears the power failure status flag
bool MCP7940_Class::deviceStart | ( | ) | const |
Start the MCP7940 device
Sets the status register to turn on the device clock
bool MCP7940_Class::deviceStatus | ( | ) | const |
checks to see if the MCP7940 crystal has been turned on or off Sets the status register to turn on the device clock
bool MCP7940_Class::deviceStop | ( | ) | const |
Stop the MCP7940 device
Sets the status register to turn off the device clock
DateTime MCP7940_Class::getAlarm | ( | const uint8_t | alarmNumber, |
uint8_t & | alarmType | ||
) | const |
bool MCP7940_Class::getAlarmState | ( | const uint8_t | alarmNumber | ) | const |
Return whether a given alarm is on or off
[in] | alarmNumber | Alarm number 0 or 1 |
bool MCP7940_Class::getBattery | ( | ) | const |
Return the battery backup state
Has no effect on the MCP7940M, only on the MCP7940N
int8_t MCP7940_Class::getCalibrationTrim | ( | ) | const |
Return the TRIMVAL trim value
Since the number in the register can be negative but is not in excess-128 format any negative numbers need to be manipulated before returning
uint8_t MCP7940_Class::getMFP | ( | ) | const |
Gets the MFP (Multifunction Pin) value
On is true and Off is false. This is read from the control register if no alarms are enabled, otherwise the two alarm states must be checked.
DateTime MCP7940_Class::getPowerDown | ( | ) | const |
returns the date/time that the power went off
This is set back to zero once the power fail flag is reset.
bool MCP7940_Class::getPowerFail | ( | ) | const |
Return the power failure status
DateTime MCP7940_Class::getPowerUp | ( | ) | const |
returns the date/time that the power went back on
int32_t MCP7940_Class::getPPMDeviation | ( | const DateTime & | dt | ) | const |
Calculate the ppm deviation since the last time the clock was set
The internal _SetUniTime value is set when the clock is set. On this call the current time is converted to unitime and compared with the stored value to determine the part-per-million difference. One hour deviation in 6 months is 225 ppm
[in] | dt | Actual Date/time |
uint32_t MCP7940_Class::getSetUnixTime | ( | ) | const |
Get the time the clock was last calibrated or adjusted.
Returns the internal variable _SetUnixTime. This is the time the clock was last calibrated or adjusted. This should only used to testing.
uint8_t MCP7940_Class::getSQWSpeed | ( | ) | const |
returns the list value for the frequency of the square wave
bool MCP7940_Class::getSQWState | ( | ) | const |
Get the square wave generator state
bool MCP7940_Class::isAlarm | ( | const uint8_t | alarmNumber | ) | const |
Return whether a given alarm is active or not
[in] | alarmNumber | Alarm number 0 or 1 |
DateTime MCP7940_Class::now | ( | ) | const |
returns the current date/time
If the device is stopped then the stop time is returned
|
inline |
Template for readEUI()
As a template it can support compile-time data type definitions. This is a special call as it access a different I2C address and a different memory block
[in] | addr | Memory address |
[in] | value | Data Type "T" to read |
|
inline |
Template for readRAM()
As a template it can support compile-time data type definitions
[in] | addr | Memory address |
[in] | value | Data Type "T" to read |
bool MCP7940_Class::setAlarm | ( | const uint8_t | alarmNumber, |
const uint8_t | alarmType, | ||
const DateTime & | dt, | ||
const bool | state = true |
||
) | const |
Sets one of the 2 alarms
In order to configure the alarm modules, the following steps need to be performed in order:
[in] | alarmNumber | Alarm 0 or Alarm 1 |
[in] | alarmType | Alarm type from 0 to 7, see detailed description |
[in] | dt | DateTime alarm value used to set the alarm |
[in] | state | Alarm state to set to (0 for "off" and 1 for "on") |
void MCP7940_Class::setAlarmPolarity | ( | const bool | polarity | ) | const |
Sets the alarm polarity
Alarm polarity (see also TABLE 5-10 on p.27 of the datasheet). Note: the MFP pin is open collector, it needs an external pull-up resistor.
If only one alarm is set:
polarity = 0: MFP high when no alarm, low when alarm.
polarity = 1: MFP low when no alarm, high when alarm.
If both alarms are set:
polarity = 0: MFP high when no alarm or one alarm, low when both alarms go off.
polarity = 1: MFP low when no alarm, high when eihter or both alarms go off.
In most situations you will want to set polarity to 1 if you have two alarms set, to be able to see when an alarm goes off using the MFP pin.
[in] | polarity | Boolean value to for polarity |
bool MCP7940_Class::setAlarmState | ( | const uint8_t | alarmNumber, |
const bool | state | ||
) | const |
Turns an alarm on or off without changing the alarm condition
[in] | alarmNumber | Alarm number 0 or 1 |
[in] | state | State to the set the alarm to |
bool MCP7940_Class::setBattery | ( | const bool | state | ) | const |
Enable or disable the battery backup
Has no effect on the MCP7940M, only on the MCP7940N
[in] | state | True for "on", False for "off" |
bool MCP7940_Class::setMFP | ( | const bool | value | ) | const |
Sets the MFP (Multifunction Pin) to the requested state
void MCP7940_Class::setSetUnixTime | ( | uint32_t | aTime | ) |
Set the time the clock was last calibrated or adjusted.
Set the internal variable _SetUnixTime. This is the time the clock was last calibrated or adjusted. This should only used to testing.
[in] | aTime | is the Unix time to set the time the clock was last calibrated or adjusted to. |
bool MCP7940_Class::setSQWSpeed | ( | uint8_t | frequency, |
bool | state = true |
||
) | const |
set the square wave speed to a value
[in] | frequency | Integer value index for the square wave frequency: 0 = 1Hz 1 = 4.096KHz 2 = 8.192KHz 3 = 32.768KHz 4 = 64Hz |
[in] | state | Boolean value set to false if square wave is to be turned off, otherwise true |
bool MCP7940_Class::setSQWState | ( | const bool | state | ) | const |
set the square wave generator on or off
[in] | state | Boolean value set to false if square wave is to be turned off, otherwise true |
uint8_t MCP7940_Class::weekdayRead | ( | ) | const |
return the weekday number from the RTC
This number is user-settable and is incremented when the day shifts. It is set as part of the adjust() method where Monday is weekday 1
uint8_t MCP7940_Class::weekdayWrite | ( | const uint8_t | dow | ) | const |
set the RTC weekday number
This number is user-settable and is incremented when the day shifts. The library uses 1 for Monday and 7 for Sunday
[in] | dow | Day of week (1-7) |
|
inline |
Template for writeEUI()
As a template it can support compile-time data type definitions. This is a special call as it access a different I2C address and a different memory block and also has to unlock the area prior to writing
[in] | addr | Memory address |
[in] | value | Data Type "T" to read |
|
inline |
Template for writeRAM()
As a template it can support compile-time data type definitions
[in] | addr | Memory address |
[in] | value | Data Type "T" to write |