Add an external RTC

This is using a simple DS1672 counter because .NET handles all the time conversion and it’s about the cheapest I’ve found so far with a trickle charge. A 220uF capacitor keeps it running for about 30min and a 1000uF lasts about 2.5hrs. It can hold the time much longer but the counter stops.

One problem I have is that timespan totalseconds isn’t available to me for some reason so I just added it manually.

[url]http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=DS1672-33%2B-ND[/url]


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Text;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.System;

namespace FEZ_Mini_Application1
{
    public class Program
    {

        static bool ledState = true;
        static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);

        static I2CDevice DS1672RTC = new I2CDevice(new I2CDevice.Configuration(0x68, 400));

        public static void Main()
        {
            Thread.Sleep(1000);

            Init();
            StartCharger();

            Set(new DateTime(2010,10,9,14,36,45));

            Timer updateTimer = new Timer(new TimerCallback(Update), null, 500, 1000);

            Thread.Sleep(Timeout.Infinite);
        }

        static void Update(object o)
        {
            Debug.Print("Time = " + UnixTimestampToDateTime(Read()));
        }

        static void Init()
        {
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
            byte[] output = new byte[2];
            output[0] = 0x04;//select control register
            output[1] = 0x00;//set EOSC to 0 and start oscillator
            xActions[0] = I2CDevice.CreateWriteTransaction(output);
            DS1672RTC.Execute(xActions, 100);
        }

        static void StartCharger()
        {
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
            byte[] output = new byte[2];
            output[0] = 0x05;//select charger register
            output[1] = 0xAB;//set one diode, 4k resistor
            xActions[0] = I2CDevice.CreateWriteTransaction(output);
            DS1672RTC.Execute(xActions, 100);
        }

        static long Read()
        {
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
            byte[] input = new byte[4];
            byte[] output = new byte[1];
            output[0] = 0x00;//select 1st register
            xActions[0] = I2CDevice.CreateWriteTransaction(output);
            xActions[1] = I2CDevice.CreateReadTransaction(input);
            DS1672RTC.Execute(xActions, 100);

            return ((long)input[3] << 24) | ((long)input[2] << 16) |
                   ((long)input[1] << 8) | input[0];
        }

        static void Set(DateTime _DateTime)
        {
            long seconds = DateTimeToUnixTimestamp(_DateTime);

            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];

            byte[] output = new byte[5];
            output[0] = 0x00;//select 1st register
            output[1] = (byte)(seconds & 0xFF); ;//set register 0 LSB
            output[2] = (byte)((seconds >> 8) & 0xFF); ;//set register 1
            output[3] = (byte)((seconds >> 16) & 0xFF); ;//set register 2
            output[4] = (byte)((seconds >> 24) & 0xFF); ;//set register 3 MSB
            xActions[0] = I2CDevice.CreateWriteTransaction(output);

            DS1672RTC.Execute(xActions, 100);
        }

        static DateTime UnixTimestampToDateTime(long _UnixTimeStamp)
        {
            return (new DateTime(1970, 1, 1, 0, 0, 0)).AddSeconds(_UnixTimeStamp);
        }

        static long DateTimeToUnixTimestamp(DateTime _DateTime)
        {
            TimeSpan _UnixTimeSpan = (_DateTime - new DateTime(1970, 1, 1, 0, 0, 0));
            long seconds = (_UnixTimeSpan.Days * 86400) + 
                           (_UnixTimeSpan.Hours * 3600) + 
                           (_UnixTimeSpan.Minutes * 60) + 
                           _UnixTimeSpan.Seconds;
            return seconds;

        }
    }
}

Thank you for the code. It looks very need.
Looking forward to see it on the microframeworkprojects.com or fezzer.com :wink: