Notes About Working with Various Arduino & Netduino Microcontroller Boards

Sunday, December 2, 2012

Reading Time & Date from a Chronodot with a Netduino Plus 2

A few months back, I posted some C# code for a Netduino Plus (v. 1) that read the time and date from a Chronodot v. 2.1. It worked, but it wasn't the best and relied on a couple additional classes. I have written a new version for use with the Netduino Plus 2.

This code requires the updated firmware for the Netduino Plus 2 to fix problems with I2C. This is a fairly easy update and only requires a few minutes.

Connections:
SDA to SDA (with pull-up resistor)
SCL to SCL (with pull-up resistor) 
VCC to 3V3
GND to GND

Code:
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace chronodot
{
    public class Program
    {
        public static void Main()
        {
            // Chronodot's I2C address is 0x68
            // 100 is clock speed
            I2CDevice chronodot = new I2CDevice(new I2CDevice.Configuration(0x68, 100));
            byte[] timeCmd = { 0 };
            byte[] timeData = { 0, 0, 0 };
            I2CDevice.I2CTransaction[] time = new I2CDevice.I2CTransaction[] {
                I2CDevice.CreateWriteTransaction(timeCmd),
                I2CDevice.CreateReadTransaction(timeData)
            };
            int bytesRead = chronodot.Execute(time, 100);

            byte[] dateCmd = { 3 };
            byte[] dateData = { 0, 0, 0, 0 };
            I2CDevice.I2CTransaction[] date = new I2CDevice.I2CTransaction[] {
                I2CDevice.CreateWriteTransaction(dateCmd),
                I2CDevice.CreateReadTransaction(dateData)
            };
            bytesRead = chronodot.Execute(date, 100);
            int hour = bcdToDec(timeData[2]) & 0x3F;
            int min = bcdToDec(timeData[1]);
            int sec = bcdToDec(timeData[0]) & 0x7F;
            int month = bcdToDec(dateData[2]);
            int day = bcdToDec(dateData[1]);
            int year = bcdToDec(dateData[3]);
            string timeDate = padZero(hour);
            timeDate += ":";
            timeDate += padZero(min);
            timeDate += ":";
            timeDate += padZero(sec);
            timeDate += " ";
            timeDate += padZero(month);
            timeDate += "/";
            timeDate += padZero(day);
            timeDate += "/";
            timeDate += year.ToString();
            Debug.Print(timeDate);
            chronodot.Dispose();
        }

        public static byte bcdToDec(byte val)
        {
            return (byte)((val / 16 * 10) + (val % 16));
        }

        public static string padZero(int num)
        {
            return num >= 10 ? num.ToString() : "0" + num.ToString();
        }

    }
}

2 comments:

  1. Could you say if/how time can be read to the ten-thousandths of a second? I am looking for timing that tight, and am not sure if the Netduino/MicroFramework/Chronodot combination can support that. I am investigating the Netduino platform for some precision timing stuff (have not bought anything yet) and trying to get my full parts shopping list together. :)

    Thanks.

    ReplyDelete
    Replies
    1. Hi -

      The Chronodot's time-keeping registers only track whole seconds, and its square wave generator only goes to 8.192kHz, so the Chronodot won't get you the accuracy you need. For a discussion of fine-grained timing on the Netduino, perhaps this discussion in the Netduino forum will help: http://forums.netduino.com/index.php?/topic/4927-microsecond-timing/. I don't have any experience with timing at that scale.

      Delete