|
| 1 | +# Read Date and Time from DS3234 Real Time Clock |
| 2 | + |
| 3 | +This application reads the date and time values from the [Sparkfun DeadOn RTC Breakout Board](https://www.sparkfun.com/products/10160). This board contains the DS3234 Real Time Clock module. |
| 4 | + |
| 5 | +# Code |
| 6 | +```csharp |
| 7 | +using Microsoft.SPOT; |
| 8 | +using Microsoft.SPOT.Hardware; |
| 9 | +using SecretLabs.NETMF.Hardware.NetduinoPlus; |
| 10 | +using System.Threading; |
| 11 | + |
| 12 | +namespace ReadDS3234 |
| 13 | +{ |
| 14 | + public class Program |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Position of the time elements in the array of bytes read from the DS3234. |
| 18 | + /// </summary> |
| 19 | + public const int REG_SECONDS = 0; |
| 20 | + public const int REG_MINUTE = REG_SECONDS + 1; |
| 21 | + public const int REG_HOUR = REG_MINUTE + 1; |
| 22 | + public const int REG_WEEKDAY = REG_HOUR + 1; |
| 23 | + public const int REG_DAY = REG_WEEKDAY + 1; |
| 24 | + public const int REG_DATE = REG_DAY + 1; |
| 25 | + public const int REG_MONTH = REG_DATE + 1; |
| 26 | + public const int REG_YEAR = REG_MONTH + 1; |
| 27 | + public const int REG_SIZE = REG_YEAR + 1; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Convert the BCD byte into a a byte. |
| 31 | + /// </summary> |
| 32 | + /// <param name="value">Value to convert</param> |
| 33 | + /// <returns>Decimal represention of the BCD number.</returns> |
| 34 | + public static byte ConvertBCDToByte(byte value) |
| 35 | + { |
| 36 | + byte result; |
| 37 | + |
| 38 | + result = (byte) ((value & 0xf0) >> 4); |
| 39 | + result *= 10; |
| 40 | + result += (byte) (value & 0x0f); |
| 41 | + return (result); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Convert a byte value into a BCD encoded version of the data. |
| 46 | + /// </summary> |
| 47 | + /// <param name="value">Byte value to convert.</param> |
| 48 | + /// <returns>Byte containing the BCD representation of value.</returns> |
| 49 | + public static byte ConvertByteToBCD(byte value) |
| 50 | + { |
| 51 | + byte result = (byte) (value % 10); |
| 52 | + result |= (byte) ((value / 10) << 4); |
| 53 | + return (result); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Decode the data from the DS3234 and return a string representation of the date / time. |
| 58 | + /// </summary> |
| 59 | + /// <param name="dataFromChip">Byte array containing the date / time registers from the DS3234.</param> |
| 60 | + /// <returns>String representation of the date and time.</returns> |
| 61 | + public static string DecodeTime(byte[] dataFromChip) |
| 62 | + { |
| 63 | + byte hour, minutes, seconds, day, month; |
| 64 | + int year; |
| 65 | + |
| 66 | + seconds = ConvertBCDToByte(dataFromChip[REG_SECONDS]); |
| 67 | + minutes = ConvertBCDToByte(dataFromChip[REG_MINUTE]); |
| 68 | + if ((dataFromChip[REG_HOUR] & 0x40) > 0) |
| 69 | + { |
| 70 | + hour = ConvertBCDToByte((byte) (dataFromChip[REG_HOUR] & 0x1f)); |
| 71 | + if ((dataFromChip[REG_HOUR] & 0x20) > 0) |
| 72 | + { |
| 73 | + hour += 12; |
| 74 | + } |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + hour = ConvertBCDToByte((byte) (dataFromChip[REG_HOUR] & 0x3f)); |
| 79 | + } |
| 80 | + day = ConvertBCDToByte(dataFromChip[REG_DAY]); |
| 81 | + month = ConvertBCDToByte((byte) (dataFromChip[REG_MONTH] & 0x7f)); |
| 82 | + year = 1900 + ConvertBCDToByte(dataFromChip[REG_YEAR]); |
| 83 | + if ((dataFromChip[REG_MONTH] & 0x80) > 0) |
| 84 | + { |
| 85 | + year += 100; |
| 86 | + } |
| 87 | + return ("Current time: " + hour.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString() + " Date: " + day.ToString() + "-" + month.ToString() + "-" + year.ToString()); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Convert the data byte into a hexadecimal string representation of the number. |
| 92 | + /// </summary> |
| 93 | + /// <param name="data">Value to convert.</param> |
| 94 | + /// <returns>String containing the hexadecimal representation of the number.</returns> |
| 95 | + public static string Hexadecimal(byte data) |
| 96 | + { |
| 97 | + const string characters = "0123456789abcdef"; |
| 98 | + string result = "0x"; |
| 99 | + |
| 100 | + result += characters[data >> 4]; |
| 101 | + result += characters[data & 0x0f]; |
| 102 | + return (result); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Main program loop. |
| 107 | + /// </summary> |
| 108 | + public static void Main() |
| 109 | + { |
| 110 | + SPI.Configuration spiConfig = new SPI.Configuration( |
| 111 | + ChipSelect_Port: Pins.GPIO_PIN_D7, // Chip select is digital IO 7. |
| 112 | + ChipSelect_ActiveState: false, // Chip select is active low. |
| 113 | + ChipSelect_SetupTime: 0, // Amount of time between selection and the clock starting |
| 114 | + ChipSelect_HoldTime: 0, // Amount of time the device must be active after the data has been read. |
| 115 | + Clock_Edge: false, // Sample on the falling edge. |
| 116 | + Clock_IdleState: false, // Clock is idle when low. |
| 117 | + Clock_RateKHz: 10, // 10KHz clock speed. |
| 118 | + SPI_mod: SPI_Devices.SPI1 // Use SPI1 |
| 119 | + ); |
| 120 | + |
| 121 | + SPI spi = new SPI(spiConfig); |
| 122 | + int count = 0; |
| 123 | + while (true) |
| 124 | + { |
| 125 | + byte[] dataOut = new byte[] { 0 }; |
| 126 | + byte[] dataIn = new byte[REG_SIZE]; |
| 127 | + spi.WriteRead(dataOut, dataIn, 1); |
| 128 | + count++; |
| 129 | + string message = "Read " + count.ToString() + ":"; |
| 130 | + for (int index = 0; index < dataIn.Length; index++) |
| 131 | + { |
| 132 | + message += " " + Hexadecimal(dataIn[index]); |
| 133 | + } |
| 134 | + Debug.Print(message); |
| 135 | + Debug.Print(DecodeTime(dataIn)); |
| 136 | + Thread.Sleep(2000); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +``` |
| 143 | + |
| 144 | +# Hardware |
| 145 | + |
| 146 | +The DS3234 should be connected to the Netduino as follows: |
| 147 | + |
| 148 | + |
0 commit comments