Standard library · Hardware

hw.ds1307

Set and read the DS1307 real-time clock chip over I2C, with BCD conversion and date formatting.

import hw.ds1307

hw.ds1307 drives the DS1307 real-time clock chip over hw.i2c. The chip keeps the time in binary-coded decimal; the package converts it, accepts and returns plain numbers in a Reading and formats a reading as 2026-12-03 21:45:09. Without a chip on the bus, on the simulation too until something is written, Detected is false and Now returns the default reading.

Setting the time

Check the chip with Detected, then write a time with Set and read it back with Now. Make builds the reading and Format prints it.

hw-ds1307-guide-set.salam
Try it
import hw.i2c
import hw.ds1307

func main:
    clock := ds1307.Open(i2c.Open(0))
    if not ds1307.Detected(clock):
        println "no clock"
        ds1307.Set(clock, ds1307.Make(2026, 12, 3, 4, 21, 45, 9))
    end
    println ds1307.Format(ds1307.Now(clock))
end
Output
no clock
2026-12-03 21:45:09

Halting and restarting

Halt stops the oscillator so the chip keeps its time while the board is off for a while. Running tells whether it counts, and Set starts it again.

hw-ds1307-guide-halt.salam
Try it
import hw.i2c
import hw.ds1307

func main:
    clock := ds1307.Open(i2c.Open(0))
    ds1307.Set(clock, ds1307.Make(2027, 1, 1, 6, 0, 0, 0))
    println ds1307.Running(clock)
    ds1307.Halt(clock)
    println ds1307.Running(clock)
    ds1307.Set(clock, ds1307.Make(2027, 1, 1, 6, 0, 0, 0))
    println ds1307.Running(clock)
end
Output
true
false
true

Types

Reading

#
struct Reading

A date and time: year, month, day, weekday, hour, minute and second.

NameTypeDescription
yearint = 2000The full year, such as 2026. The chip stores only two digits and Now adds 2000.
monthint = 1The month, 1 to 12.
dayint = 1The day of the month, 1 to 31.
weekdayint = 1The day of the week, 1 to 7. Which day is 1 is up to the program.
hourint = 0The hour, 0 to 23. Now reads a chip set to 12-hour mode and converts it.
minuteint = 0The minute, 0 to 59.
secondint = 0The second, 0 to 59.
hw-ds1307-Clock.salam
Try it
import hw.i2c
import hw.ds1307

func main:
    clock := ds1307.Open(i2c.Open(0))
    println ds1307.ADDRESS, ds1307.Detected(clock)
    r := ds1307.Make(2026, 12, 3, 4, 21, 45, 9)
    println ds1307.Format(r)
    println ds1307.Set(clock, r)
    println ds1307.Format(ds1307.Now(clock))
    println ds1307.Running(clock)
    println ds1307.Halt(clock), ds1307.Running(clock)
end
Output
104 false
2026-12-03 21:45:09
true
2026-12-03 21:45:09
true
true false

Device

#
struct Device

A DS1307 on an I2C bus.

NameTypeDescription
busi2c.BusThe I2C bus the chip is on.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Constants and variables

ADDRESS

#
const ADDRESS: i32 = 104

The I2C address of the chip, 0x68.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Functions

Make

#
func Make(year: int, month: int, day: int, weekday: int, hour: int, minute: int, second: int): Reading

Builds a Reading from its parts. Handy where a struct literal is awkward.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Format

#
func Format(r: Reading): str

Writes a reading as text in the form 2026-12-03 21:45:09.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Open

#
func Open(bus: i2c.Bus): Device

Makes a Device for the chip on a bus. It does not touch the bus.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Detected

#
func Detected(d: Device): bool

True when something answers at the chip address.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Running

#
func Running(d: Device): bool

True when the oscillator runs, that is when the clock-halt bit of the seconds register is clear and the register could be read.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Set

#
func Set(d: Device, r: Reading): bool

Writes a reading to the chip, in BCD, and starts the clock because the seconds register is written last with the halt bit clear. Returns false when the first write fails.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Halt

#
func Halt(d: Device): bool

Stops the clock by setting the halt bit. Returns false when the chip does not answer.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam

Now

#
func Now(d: Device): Reading

Reads the current date and time. A chip that does not answer gives the default reading, 2000-01-01 00:00:00.

The example for this item is shown with the ones above: hw-ds1307-Clock.salam