Skip to content

Peripherals: Real Time Clocks

Stefan Lenz edited this page Dec 26, 2022 · 10 revisions

How a real time clock works

The most common real time clock modules in the Arduino world are the DS1307, DS3231, and DS3232 modules. Countless libraries have been written for them. BASIC has a built-in support for the clock chips without using any of these libraries. Much of the functions the libraries offer are already included in BASIC. Therefore the libraries make little sense.

All three DS clock chips are I2C components. The default I2C address is 0x69. It can be configured sometimes to another value, typically 0x69.

The first 7 bytes of the clocks internal memory from address 0-6 are the same for all three chips. They contain the seconds, minutes, hours, the day of month, and the date in the order day, month, year in BCD encoded form. The following registers are different for the various clocks.

Almost all registers are read/write. They can simply be set and read from a single I2C command.

DS1307 clocks have 56 bytes of NVRAM that can be accessed as a register. DS3231 has no RAM. DS3232 has 236 bytes of RAM. The registers from 0x07 onwards are control registers with different function. They can all be accessed directly from BASIC.

Common functions

The special array @t() can be used to access all clock registers. The common functions for all three clock types are

  • @T(0): seconds (0-59)
  • @T(1): minutes (0-59)
  • @T(2): hours (0-23) (24 hour clock mode only is supported)
  • @T(3): day of week with range 1-7
  • @T(4): date - day (1-31)
  • @T(5): date - month (1-12)
  • @T(6): date - year (0-99)

The clock has to be set at least once to run by setting the seconds value @T(0) to any value (DS1307).

The special string @T$ contains the time in the format hour:minute:second-day/month/year.

BASIC handles the conversion from BCD to the internal BASIC data types automatically, when the first 7 registers are accessed. All access to the higher addresses using @T() will yield clock dependent results.

DS1307 features

@T(7) is the clock status register. It controls square wave output of the chip.

@T(8)-@T(63) are the NVRAM bytes.

DS1307 datasheet

DS3231 features

@T(7)-T(13) are alarm settings. They are currently not supported by BASIC but can be programmed through the registers.

@T(14)-T(16) are the clock status registers.

@T(17) is the most significant byte of the temperature in degrees, @T(18) is the least significant byte. Only the top most bits are set as the clocks temperature sensor only has an accuracy of 0.25 degrees.

There is no NVRAM available on this clock.

DS3231 datasheet

DS3232 features

This clock is like the DS3231 but registers @T(19)-@T(255) are NVRAM bytes which just like on the DS1307.

EEPROMS

Many clock modules have on board I2C EEPROMS. Many have them a 0x50, some on 0x57. Typical sizes are 4k. They are very useful as BASIC program storage. This is explained in the following chapter of this Wiki.

Clone this wiki locally