Remarks

DS1307
Status
Source code GitHub
NuGet package

The DS1307 is a low-power realtime clock (RTC) controlled via I2C.

Code Example

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected Ds1307 ds1307;

    public DS1307App()
    {
        ds1307 = new Ds1307(Device.CreateI2cBus());

        if (ds1307.IsRunning == false)
        {
            Console.WriteLine("Starting RTC...");
            dSs1307.IsRunning = true;
        }

        while (true)
        {
            for (int i = 0; i < 3; i++)
            {
                var now = ds1307.GetTime();
                Console.WriteLine($"Current time: {now.ToString("MM/dd/yy HH:mm:ss")}");
                Thread.Sleep(1000);
            }

            var rand = new Random();

            if (now.Year < 2019)
            {
                now = DateTime.Now;
            }
            else
            {
                now = now.AddSeconds(rand.Next(1, 30));
            }

            var data = new byte[56];
            for (int i = 0; i < 56; i++)
            {
                data[i] = (byte)rand.Next(256);
            }

            Console.WriteLine($"Writing to RTC RAM   : {BitConverter.ToString(data)}");
            ds1307.WriteRAM(0, data);
            Console.Write($"Reading from RTC RAM : ");
            data = ds1307.ReadRAM(0, 56);
            Console.WriteLine(BitConverter.ToString(data));
        }
    }
}

Sample projects available on GitHub

Circuit Example

The DS3231 real time clock module (see image below) requires only four (for simple timekeeping) or five (for alarms) connections

DS3231 Meadow Pin
GND GND
VCC 3V3
SCL D08 (SCL Pin)
SDA D07 (SDA Pin)

The 32K pin outputs the 32,768 Hz clock signal from the module. This signal is only available when power is supplied by Vcc, it is not available when the module is on battery power.

Characteristic Locus
Inheritance System.Object > Ds1307
Inherited Members System.Object.ToString() System.Object.Equals(System.Object) System.Object.Equals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) System.Object.GetHashCode() System.Object.GetType() System.Object.MemberwiseClone()
Namespace Meadow.Foundation.RTCs
Assembly Ds1307.dll

Syntax

public class Ds1307

Constructors

View Source

Ds1307(II2cBus)

Declaration
public Ds1307(II2cBus bus)

Parameters

Type Name Description
II2cBus bus

Properties

View Source

Address

Declaration
public byte Address { get; }

Property Value

Type Description
System.Byte
View Source

IsRunning

Stops or starts the clock oscillator. Stopping the oscillator decreases power consumption (and stops the clock)

Declaration
public bool IsRunning { get; set; }

Property Value

Type Description
System.Boolean

Methods

View Source

GetTime()

Declaration
public DateTime GetTime()

Returns

Type Description
System.DateTime
View Source

ReadRAM(Int32, Int32)

The DS1307 has 56 bytes of battery-backed RAM. Use this method to Read from that memory.

Declaration
public byte[] ReadRAM(int offset, int count)

Parameters

Type Name Description
System.Int32 offset

Offset to the start of the read (0-55)

System.Int32 count

The number of bytes to read

Returns

Type Description
System.Byte[]
View Source

SetTime(DateTime)

Declaration
public void SetTime(DateTime time)

Parameters

Type Name Description
System.DateTime time
View Source

SquareWaveOutput(Ds1307.SquareWaveFrequency)

Declaration
public void SquareWaveOutput(Ds1307.SquareWaveFrequency freq)

Parameters

Type Name Description
Ds1307.SquareWaveFrequency freq
View Source

WriteRAM(Int32, Byte[])

The DS1307 has 56 bytes of battery-backed RAM. Use this method to Write to that memory

Declaration
public void WriteRAM(int offset, params byte[] data)

Parameters

Type Name Description
System.Int32 offset
System.Byte[] data