Characteristic Locus
Inheritance System.Object > DS323x > DS3231
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 DS323x.dll

Syntax

public class DS323x

Remarks

The DS323x ICs offer a low cost accurate real time clock with a temperature compensation crystal oscillator. This range of chips offers the following functionality:

  • Temperature compensation
  • Battery backup
  • I2C (DS3231) and SPI (DS3234) interfaces.
  • Two programmable alarms
  • 32.768 KHz square wave output

Purchasing

A variety of modules are available including low cost modules with integrated EEPROM:

Examples

The following application sets an alarm to trigger at when the current second is equal to 15. The interrupt routine displays the time and then clears the interrupt flag:

using System.Threading;
using Meadow;
using Meadow.Foundation.RTCs;

namespace LedSample
{
    public class Program
    {
        static IApp _app; 
        public static void Main()
        {
            _app = new App();
        }
    }

    public class App : AppBase<F7Micro, App>
    {
        public App ()
        {
            DS3231 rtc = new DS3231(0x68, 100, Device.Pins.D08);
            rtc.ClearInterrupt(DS323x.Alarm.BothAlarmsRaised);
            rtc.SetAlarm(DS323x.Alarm.Alarm1Raised, new DateTime(2017, 10, 29, 9, 43, 15), DS323x.AlarmType.WhenSecondsMatch);
            rtc.OnAlarm1Raised += RtcOnAlarm1Raised;
        }

        static void RtcOnAlarm1Raised(object sender)
        {
            DS3231 rtc = (DS3231) sender;
            Debug.Print("Alarm 1 has been activated: " + rtc.CurrentDateTime.ToString());
            rtc.ClearInterrupt(DS323x.Alarm.Alarm1Raised);
        }
    }
}
Example Circuit

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

Meadow Pin Sensor Pin Wire Color
3.3V Vcc Red
GND GND Black
SC SCK Blue
SD SDA White
SQW Digital Interrupt Orange

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.

The orange wire is only required if the alarms are being used to interrupt the Meadow.

Fields

View Source

_ds323x

DS323x Real Time Clock object.

Declaration
protected II2cPeripheral _ds323x

Field Value

Type Description
II2cPeripheral
View Source

_interruptPort

Interrupt port attached to the DS323x RTC module.

Declaration
protected IDigitalInputPort _interruptPort

Field Value

Type Description
IDigitalInputPort

Properties

View Source

ControlRegister

Control register.

Declaration
protected byte ControlRegister { get; set; }

Property Value

Type Description
System.Byte

Remarks

Control register contains the following bit (in sequence b7 - b0): EOSC - BBSQW - CONV - RS1 - RS2 - INTCN - A2IE - A1IE

View Source

ControlStatusRegister

Control and status register.

Declaration
protected byte ControlStatusRegister { get; set; }

Property Value

Type Description
System.Byte

Remarks

Control and status register contains the following bit (in sequence b7 - b0): OSF - 0 - 0 - 0 - EN32KHZ - BSY - A2F - A1F

View Source

CurrentDateTime

Get / Set the current date and time.

Declaration
public DateTime CurrentDateTime { get; set; }

Property Value

Type Description
System.DateTime
View Source

Temperature

Get the current die temperature.

Declaration
public double Temperature { get; }

Property Value

Type Description
System.Double
View Source

WhichAlarm

Determine which alarm has been raised.

Declaration
protected DS323x.Alarm WhichAlarm { get; }

Property Value

Type Description
DS323x.Alarm

Methods

View Source

ClearInterrupt(DS323x.Alarm)

Clear the alarm interrupt flag for the specified alarm.

Declaration
public void ClearInterrupt(DS323x.Alarm alarm)

Parameters

Type Name Description
DS323x.Alarm alarm

Alarm to clear.

View Source

DayOfWeekToByte(DayOfWeek)

Convert the day of the week to a byte.

Declaration
protected byte DayOfWeekToByte(DayOfWeek day)

Parameters

Type Name Description
System.DayOfWeek day

Day of the week

Returns

Type Description
System.Byte

Byte representation of the day of the week (Sunday = 1).

View Source

DecodeDateTimeRegisters(Byte[])

Decode the register contents and create a DateTime version of the register contents.

Declaration
protected DateTime DecodeDateTimeRegisters(byte[] data)

Parameters

Type Name Description
System.Byte[] data

Register contents.

Returns

Type Description
System.DateTime

DateTime object version of the data.

View Source

DisplayRegisters()

Display the registers.

Declaration
public void DisplayRegisters()
View Source

EnableDisableAlarm(DS323x.Alarm, Boolean)

Enable or disable the specified alarm.

Declaration
public void EnableDisableAlarm(DS323x.Alarm alarm, bool enable)

Parameters

Type Name Description
DS323x.Alarm alarm

Alarm to enable / disable.

System.Boolean enable

Alarm state, true = on, false = off.

View Source

EncodeDateTimeRegisters(DateTime)

Encode the a DateTime object into the format used by the DS323x chips.

Declaration
protected byte[] EncodeDateTimeRegisters(DateTime dt)

Parameters

Type Name Description
System.DateTime dt

DateTime object to encode.

Returns

Type Description
System.Byte[]

Bytes to send to the DS323x chip.

View Source

InterruptPort_Changed(Object, EventArgs)

Alarm interrupt has been raised, work out which one and raise the necessary event.

Declaration
protected void InterruptPort_Changed(object sender, EventArgs e)

Parameters

Type Name Description
System.Object sender
System.EventArgs e
View Source

SetAlarm(DS323x.Alarm, DateTime, DS323x.AlarmType)

Set one of the two alarms on the DS323x module.

Declaration
public void SetAlarm(DS323x.Alarm alarm, DateTime time, DS323x.AlarmType type)

Parameters

Type Name Description
DS323x.Alarm alarm

Define the alarm to be set.

System.DateTime time

Date and time for the alarm.

DS323x.AlarmType type

Type of alarm to set.

Events

View Source

OnAlarm1Raised

Event raised when Alarm1 is triggered.

Declaration
public event DS323x.AlarmRaised OnAlarm1Raised

Event Type

Type Description
DS323x.AlarmRaised
View Source

OnAlarm2Raised

Event raised when Alarm2 is triggered.

Declaration
public event DS323x.AlarmRaised OnAlarm2Raised

Event Type

Type Description
DS323x.AlarmRaised