It keeps the date and the time of day while the board is off, with an alarm, a countdown timer and a voltage-low flag that says when the time can no longer be trusted. It runs from 1.8 V to 5.5 V, so it suits 3.3 V boards as well as an Uno.
Pins
| Pin | What it does |
|---|---|
VCC |
Supply, 1.8 to 5.5 V. |
GND |
Ground. |
SDA |
I2C data. The module carries the pull-ups. |
SCL |
I2C clock. |
INT |
Alarm and timer interrupt, active low, open drain: it needs a pull-up. |
CLKOUT |
Clock output, open drain: 32.768 kHz at power-on, or 1.024 kHz, 32 Hz or 1 Hz. |
The address is 0x51 (A2h to write, A3h to read), fixed.
Properties
start is the date and time it holds when the circuit starts, written
YYYY-MM-DD HH:MM:SS. lostpower makes it a chip whose time cannot be trusted: the
VL flag is set until a sketch writes the seconds, which is what RTClib's
lostPower() reads. The S button on the face sets it to your computer's clock.
What the model gets right
The registers. Sixteen of them, from NXP's data sheet: two control and status
registers, seven time registers from 0x02 in BCD (seconds with VL in bit 7,
minutes, hours in 24-hour form, day of the month, weekday 0 to 6, month with the
century bit, year), four alarm registers, CLKOUT_control, Timer_control and
Timer. The pointer steps after each byte and wraps from 0x0F to 0x00, and the
registers come up as the data sheet's reset table says.
The calendar. One tick a second. Every year divisible by four is a leap year, 00 included, and the century bit flips when the years roll from 99 to 00. While a transfer reads or writes the time registers the counters are held, and a tick that came meanwhile is taken at the STOP, as the data sheet describes.
STOP. Setting STOP in Control_status_1 holds the clock. Clearing it starts
the first tick 0.507813 s later, then one every second.
The alarm. Minute, hour, day and weekday, each on when its top bit is clear. When
every enabled one matches for the first time, AF is set, and with AIE the INT
pin goes low until a sketch clears AF.
The timer. An eight-bit countdown at 4096 Hz, 64 Hz, 1 Hz or once a minute. At
the end of each countdown it sets TF and starts again. With TIE, INT follows
TF, or with TI_TP gives one short pulse a countdown. Writing 0 clears AF or
TF and writing 1 leaves it, so clearing one never clears the other.
What it does not model
The 32.768 kHz CLKOUT, which is too fast to simulate cheaply: at that setting the
pin is let go. The EXT_CLK test mode and the power-on reset override: their bits are
kept and change nothing. The voltage-low detector: with the coin cell fitted the chip
never sees its supply drop, so VL comes only from the lostpower property. Below
1.8 V the chip leaves the bus and keeps time. The crystal's drift: this clock is
exact.
Code
#include <RTClib.h>
RTC_PCF8563 rtc;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("No PCF8563 at 0x51");
while (true) {}
}
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
rtc.start();
}
void loop() {
DateTime now = rtc.now();
Serial.println(now.timestamp());
delay(1000);
}
RTClib.h is Mokxi's own, with Adafruit's calls: begin(), now(), adjust(),
lostPower(), start(), stop(), isrunning() and the CLKOUT settings
(writeSqwPinMode()). Makuna's RtcPCF8563.h reads the part too.
Common mistakes
Reading the time before setting it: a chip with VL set gives whatever its registers
held. Expecting 12-hour time: the PCF8563 counts hours 0 to 23 only. Reading INT
with no pull-up: it is open drain, so use INPUT_PULLUP. Looking for it at 0x68: that
is the DS1307 and DS3231.
See it in action
PCF8563 clock at /templates sets the clock if it lost its time and prints the date and time every second.
Related
- The PCF8563 part page, with its pinout
- DS1307 real-time clock, the 5 V clock at 0x68