Parts: I2C Real-time Clock Calendar (PCF8563)

pcf8563

The PCF8563 is a real-time clock/calendar/alarm chip with an I2C interface. This would be useful in projects where the primary microcontroller doesn’t have enough resources for an interrupt driven clock.

We demonstrate the PCF8563 using the Bus Pirate after the break. For a limited time you can get your own Bus Pirate, fully assembled and shipped worldwide, for only $30.

pcf8563

PCF8563 real-time clock calendar (Octopart search, $1.33). Datasheet (PDF).

The schematic above shows a bare-bones circuit for the PCF8563. It requires a simple external oscillator circuit with a 32.768khz watch crystal (Q1). The oscillator input pin needs an external capacitor (C1, 12pF), but the oscillator output pin already has an internal capacitor. C2 is a 0.1uf decoupling capacitor for the power supply pin. The power supply can be 1.5 to 5.5volts.

The datasheet also recommends a diode on the voltage input. We didn’t use this in our test.

Bus Pirate PCF8563 (pin #)
GND GND (4)
MOSI SDA (5)
CLK SCL (6)
3.3volts or 5volts V+ (8)
Vpullup V+ (8)

We used our Bus Pirate universal serial interface to demonstrate this chip, but the transaction sequence will be the same for any microcontroller implementation. We connected the Bus Pirate to the PCF8563 as shown in the table above. We setup the Bus Pirate for I2C mode (M, 4) , and enabled the on-board power supply (capital ‘W’).

Don’t forget that you need pull-up resistors somewhere on the I2C bus. If you’re using a Bus Pirate, attach the Vpullup input to the circuit power supply then press p to configure the pullup resistors (or attach the pull-up jumpers for hardware v1a).

Interface

I2C>(1)<–search I2C address macro
Searching 7bit I2C address space.
Found devices at:
0xA2 0xA3
I2C>

The PCF8563 I2C  write address is 0xa2, and the read address is 0xa3. You can find this in the datasheet, or use the Bus Pirate search macro (1) to check all possible addresses.

Address | Register name | Bits
0x00 control_status_1
0x01 control_status_2
0x02 VL_seconds (0:6)
0x03 minutes (0:6)
0x04 hours (0:5)
0x05 days (0:5)
0x06 weekdays (0:2)
0x07 months (0:4)
0x08 years (0:7)
(see datasheet page 6)

This RTC has 16 one-byte registers that configure the clock, and set/retrieve the time. Bytes 0-8, shown in the table above, contain status and time information. The upper 7 bytes configure an alarm, timers, and other advanced features. We’re just going to focus on the clock functions.

The registers are accessed just like an I2C EEPROM. Write values by sending the I2C write address (0xa2), the address to start writing (0-15), and the data bytes(s) to write.

Read values from the chip in two steps. First, use the write command to position the read pointer, but don’t send any data bytes. Second, use the read address (0xa3) to read bytes starting at the position set during the write command.

I2C>{0xa2 2 0 30 12 31 1 5 9 }
I2C START CONDITION
WRITE: 162 GOT ACK: YES <–I2C write address (0xa2=162)
WRITE: 2 GOT ACK: YES <–register to begin writing
WRITE: 0 GOT ACK: YES <–seconds (0)
WRITE: 30 GOT ACK: YES <–minutes (30)
WRITE: 12 GOT ACK: YES <–hours (12/noon)
WRITE: 31 GOT ACK: YES<–day of the month (31)
WRITE: 1 GOT ACK: YES <–day of the week (1/Sunday)
WRITE: 5 GOT ACK: YES <–month (5/May)
WRITE: 9 GOT ACK: YES <–year (09/2009)
I2C STOP CONDITION
I2C>

Set the time by writing to registers 0x02 to 0x08. The values are entered in binary coded decimal format, with all numerical date representations being fairly standard (see datasheet pages 6-9). We set the time to 12:30:00 May 31, 2009.

First, send an I2C start condition to tell the chip to listen for its address (Bus Pirate command {). Next, send the PFC8563 write address (0xa2), and set the write pointer to the seconds register (2). Finally, write 7 bytes of data to the time registers at addresses 2-8. End the transaction with an I2C stop condition (Bus Pirate command }).

I2C>{0xa2 2 { 0xa3 r:7}
I2C START CONDITION
WRITE: 162 GOT ACK: YES <–send write address (0xa2=162)
WRITE: 2 GOT ACK: YES <–set pointer to register 2, seconds
I2C START CONDITION <–repeated start condition
WRITE: 163 GOT ACK: YES <–send read address (0xa3=163)
BULK READ 7 BYTES: <–read back 7 bytes
17 31 12 31 1 5 9 <–time: 12:31:17 Sunday, May 31, 2009
I2C STOP CONDITION
I2C>

We set the Bus Pirate’s output mode to decimal (menu ‘o’) before reading the time. This displays the values in the more familiar decimal format.

Retrieving the time takes two steps. First, a partial write transaction sets the memory location to read. Then, instead of sending any data, send a second start condition ({) and the PCF8563 I2C read address (0xa3) to put the chip in read mode. Finally, read 7 bytes (r:7) from registers 2 to 8. The output shows that a minute has passed since we set the time.

I2C>{0xa2 2 { 0xa3 r:7}
I2C START CONDITION
WRITE: 162 GOT ACK: YES <–send write address (0xa2=162)
WRITE: 2 GOT ACK: YES <–set pointer to register 2, seconds
I2C START CONDITION <–repeated start condition
WRITE: 163 GOT ACK: YES <–send read address (0xa3=163)
BULK READ 7 BYTES: <–read back 7 bytes
34 32 12 31 33 37 9 <–day of week (33) and month (37) appear wrong
I2C STOP CONDITION
I2C>

Sometimes the chip appears to return garbage results. The above output is actually a valid time reading, even though it’s obviously not the 33rd day of the week or the 37th month of the year.

Each register has several ‘do not care’ bits (see datasheet page 6). In most devices  ‘do not care’ bits are always set to 0, but the PCF8563 appears to use them in some time keeping capacity.

Day of week reads 33, or 0b00100001 in binary. If we ignore the upper 5 bits we get 0b001, or 1/Sunday, the proper day of the week. Similarly, ignore the upper three bits of month (37 = 0b00100101), giving 0b00101 or 5/May.

Like this post? Check out the parts posts you may have missed. Want to request a part post? Please leave your suggestions in the comments.

8 thoughts on “Parts: I2C Real-time Clock Calendar (PCF8563)

  1. I hope their is a example of using the bus pirate & SPI interface.
    This seems to be a better way to read and write to test to see if a circuit works than even a arduino…
    The ads1256 has some hard spi data sheets… it times out and sleeps I wonder how this would be if the chip has thowse sort of issues?

    For basic things it looks useful but without a example for the protocol that is needed in the documentation how would someone guess?

  2. grml – I don’t like nxp’s sample policy!

    At TI or Maxim you choose your chip and get a sample within 2 weeks – but nxp does not offer any samples to people who don’t have a direct sales agreement – and all german distributors don’t ship samples or rather it’s really hard to buy these parts in low quantities in germany, without paying ridiculous shipping expenses.

    grrrrrr…

  3. @bro
    At maxim you can find a DS1307, and others in the same series, with similar features. The DS1307 doesn’t actually have an alarm, but that can be easily implemented from code (at higher power consumption I guess). It has a nice feature that it can easily use a button cell to store the date/time set if main power drops.

    I managed to interface that with an Arduino with practically no idea what I was doing.

  4. I’m another fan of the 1307. It’s got the battery backup (which is really fantastic), and if I’m not mistaken, it does have 2 built in alarms that you can set. I’ve got one in my alarm clock, and it’s really (really) nice to be able to plug in a clock and not have to set the time / date…

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.