RTClib
|
RTC based on the PCF8523 chip connected via I2C and the Wire library. More...
#include <RTClib.h>
Public Member Functions | |
bool | begin (TwoWire *wireInstance=&Wire) |
Start I2C for the PCF8523 and test succesful connection. More... | |
void | adjust (const DateTime &dt) |
Set the date and time, set battery switchover mode. More... | |
bool | lostPower (void) |
Check the status register Oscillator Stop flag to see if the PCF8523 stopped due to power loss. More... | |
bool | initialized (void) |
Check control register 3 to see if we've run adjust() yet (setting the date/time and battery switchover mode) More... | |
DateTime | now () |
Get the current date/time. More... | |
void | start (void) |
Resets the STOP bit in register Control_1. | |
void | stop (void) |
Sets the STOP bit in register Control_1. | |
uint8_t | isrunning () |
Is the PCF8523 running? Check the STOP bit in register Control_1. More... | |
Pcf8523SqwPinMode | readSqwPinMode () |
Read the mode of the INT/SQW pin on the PCF8523. More... | |
void | writeSqwPinMode (Pcf8523SqwPinMode mode) |
Set the INT/SQW pin mode on the PCF8523. More... | |
void | enableSecondTimer (void) |
Enable the Second Timer (1Hz) Interrupt on the PCF8523. More... | |
void | disableSecondTimer (void) |
Disable the Second Timer (1Hz) Interrupt on the PCF8523. | |
void | enableCountdownTimer (PCF8523TimerClockFreq clkFreq, uint8_t numPeriods, uint8_t lowPulseWidth) |
Enable the Countdown Timer Interrupt on the PCF8523. More... | |
void | enableCountdownTimer (PCF8523TimerClockFreq clkFreq, uint8_t numPeriods) |
Enable Countdown Timer using default interrupt low pulse width. More... | |
void | disableCountdownTimer (void) |
Disable the Countdown Timer Interrupt on the PCF8523. More... | |
void | deconfigureAllTimers (void) |
Stop all timers, clear their flags and settings on the PCF8523. More... | |
void | calibrate (Pcf8523OffsetMode mode, int8_t offset) |
Compensate the drift of the RTC. More... | |
RTC based on the PCF8523 chip connected via I2C and the Wire library.
bool RTC_PCF8523::begin | ( | TwoWire * | wireInstance = &Wire | ) |
Start I2C for the PCF8523 and test succesful connection.
wireInstance | pointer to the I2C bus |
void RTC_PCF8523::adjust | ( | const DateTime & | dt | ) |
Set the date and time, set battery switchover mode.
dt | DateTime to set |
bool RTC_PCF8523::lostPower | ( | void | ) |
Check the status register Oscillator Stop flag to see if the PCF8523 stopped due to power loss.
When battery or external power is first applied, the PCF8523's crystal oscillator takes up to 2s to stabilize. During this time adjust() cannot clear the 'OS' flag. See datasheet OS flag section for details.
bool RTC_PCF8523::initialized | ( | void | ) |
Check control register 3 to see if we've run adjust() yet (setting the date/time and battery switchover mode)
DateTime RTC_PCF8523::now | ( | ) |
Get the current date/time.
uint8_t RTC_PCF8523::isrunning | ( | void | ) |
Is the PCF8523 running? Check the STOP bit in register Control_1.
Pcf8523SqwPinMode RTC_PCF8523::readSqwPinMode | ( | ) |
Read the mode of the INT/SQW pin on the PCF8523.
void RTC_PCF8523::writeSqwPinMode | ( | Pcf8523SqwPinMode | mode | ) |
Set the INT/SQW pin mode on the PCF8523.
mode | The mode to set, see the Pcf8523SqwPinMode enum for options |
void RTC_PCF8523::enableSecondTimer | ( | void | ) |
Enable the Second Timer (1Hz) Interrupt on the PCF8523.
The INT/SQW pin will pull low for a brief pulse once per second.
void RTC_PCF8523::enableCountdownTimer | ( | PCF8523TimerClockFreq | clkFreq, |
uint8_t | numPeriods, | ||
uint8_t | lowPulseWidth | ||
) |
Enable the Countdown Timer Interrupt on the PCF8523.
The INT/SQW pin will be pulled low at the end of a specified countdown period ranging from 244 microseconds to 10.625 days. Uses PCF8523 Timer B. Any existing CLKOUT square wave, configured with writeSqwPinMode(), will halt. The interrupt low pulse width is adjustable from 3/64ths (default) to 14/64ths of a second.
clkFreq | One of the PCF8523's Timer Source Clock Frequencies. See the PCF8523TimerClockFreq enum for options and associated time ranges. |
numPeriods | The number of clkFreq periods (1-255) to count down. |
lowPulseWidth | Optional: the length of time for the interrupt pin low pulse. See the PCF8523TimerIntPulse enum for options. |
void RTC_PCF8523::enableCountdownTimer | ( | PCF8523TimerClockFreq | clkFreq, |
uint8_t | numPeriods | ||
) |
Enable Countdown Timer using default interrupt low pulse width.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
clkFreq | One of the PCF8523's Timer Source Clock Frequencies. See the PCF8523TimerClockFreq enum for options and associated time ranges. |
numPeriods | The number of clkFreq periods (1-255) to count down. |
void RTC_PCF8523::disableCountdownTimer | ( | void | ) |
Disable the Countdown Timer Interrupt on the PCF8523.
For simplicity, this function strictly disables Timer B by setting TBC to 0. The datasheet describes TBC as the Timer B on/off switch. Timer B is the only countdown timer implemented at this time. The following flags have no effect while TBC is off, they are not cleared:
void RTC_PCF8523::deconfigureAllTimers | ( | void | ) |
Stop all timers, clear their flags and settings on the PCF8523.
This includes the Countdown Timer, Second Timer, and any CLKOUT square wave configured with writeSqwPinMode().
void RTC_PCF8523::calibrate | ( | Pcf8523OffsetMode | mode, |
int8_t | offset | ||
) |
Compensate the drift of the RTC.
This method sets the "offset" register of the PCF8523, which can be used to correct a previously measured drift rate. Two correction modes are available:
offset
consecutive minutes every two hours. This is the most energy-efficient mode.offset
consecutive seconds every minute. Extra adjustments are performed on the last second of the minute is abs(offset)>60
.The offset
parameter sets the correction amount in units of roughly 4 ppm. The exact unit depends on the selected mode:
mode | offset unit |
---|---|
PCF8523_TwoHours | 4.340 ppm = 0.375 s/day = 2.625 s/week |
PCF8523_OneMinute | 4.069 ppm = 0.352 s/day = 2.461 s/week |
See the accompanying sketch pcf8523.ino for an example on how to use this method.
mode | Correction mode, either PCF8523_TwoHours or PCF8523_OneMinute . |
offset | Correction amount, from -64 to +63. A positive offset makes the clock slower. |