DS3234 SPI Real-Time Clock

The DS3234 is a low-cost, extremely accurate SPI bus real-time clock with an integrated temperature-compensated crystal oscillator and crystal. It's suitable for 2.0V to 5.5V and is an SPI version of the DS3231 I2C Real-Time Clock. A breakout board is available from SparkFun [1]:

DS3234.jpg

It maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections of leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided.

Setting the time

First the following defvar defines the pin used for the select pin:

(defvar ss 53)

The following routine sets the time:

(defun ds3234-set (hr min)
  (with-spi (str ss 4000 1 3)
    (write-byte #x80 str)
    (write-byte 0 str)
    (write-byte min str)
    (write-byte hr str)))

The first call to write-byte sets the starting register to write to in the DS3234; in this case the register is 0, the seconds register, and add #x80 to indicate a write. We then set the seconds to 0, the minutes to min, and the hours to hr. The times are defined in binary-coded decimal, so to set the time to 12:34 you need to evaluate:

(ds3234-set #x12 #x34)

Once you have set the time the backup battery will keep the time correct in the DS3234.

Reading the time

Here's the routine ds3234-time to read the time:

(defun ds3234-time ()
(with-spi (str ss 4000 1 3) (write-byte #x01 str) (list (read-byte str) (read-byte str))))

The first call to write-byte sets the starting register to read from, in this case #x01, the minutes regiater. It reads the minutes and hours, and returns them as a list of two binary-coded decimal numbers; for example 12:34 will be:

(52 18)

because 52 is #x34 and 18 is #x12.


  1. ^ SparkFun DeadOn RTC Breakout - DS3234 on SparkFun.