The opinions expressed herein are my own personal opinions. They are not necessarily fact or sactioned by any other person or organization. If you disagree that's your right. It's also my right to not care.
© Copyright 2010, Chris Tacke
For those of you who may ever need to interface to a DS1307 I2C real time clock (it would wourk for the 1306 SPI RTC as well) here's a useful class.
private class RTCTime { private byte[] m_bytes = new byte[7]; public RTCTime() { } public RTCTime(byte[] data) { m_bytes = data; } public RTCTime(DateTime dateTime) { this.Second = dateTime.Second; this.Minute = dateTime.Minute; this.Hour = dateTime.Hour; this.Day = dateTime.Day; this.Month = dateTime.Month; this.Year = dateTime.Year; } public static implicit operator byte[](RTCTime rtc) { return rtc.m_bytes; } public static implicit operator RTCTime(byte[] bytes) { return new RTCTime(bytes); } public static explicit operator DateTime(RTCTime rtc) { return new DateTime(rtc.Year, rtc.Month, rtc.Day, rtc.Hour, rtc.Minute, rtc.Second, 0); } public int Second { get { return BCD_TO_WORD(m_bytes[0]); } set { m_bytes[0] = WORD_TO_BCD(value); } } public int Minute { get { return BCD_TO_WORD(m_bytes[1]); } set { m_bytes[1] = WORD_TO_BCD(value); } } public int Hour { get { return BCD_TO_WORD(m_bytes[2]); } set { m_bytes[2] = WORD_TO_BCD(value); } } public int DayOfWeek { get { return BCD_TO_WORD(m_bytes[3]); } set { m_bytes[3] = WORD_TO_BCD(value); } } public int Day { get { return BCD_TO_WORD(m_bytes[4]); } set { m_bytes[4] = WORD_TO_BCD(value); } } public int Month { get { return BCD_TO_WORD(m_bytes[5]); } set { m_bytes[5] = WORD_TO_BCD(value); } } public int Year { get { return BCD_TO_WORD(m_bytes[6]); } set { m_bytes[6] = WORD_TO_BCD(value); } } public int Length { get { return m_bytes.Length; } } private int BCD_TO_WORD(byte x) { return (x & 0x0f) + ((x >> 4) * 10); } private byte WORD_TO_BCD(int x) { return (byte)((x % 10) + ((x / 10) * 0x10)); } }
Remember Me