Skip to content

Commit f5cb591

Browse files
committed
Added ReadDS3234 (read SPI) example.
1 parent a616f81 commit f5cb591

7 files changed

Lines changed: 381 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Further information about the Netduino boards and the samples in this repository
1313
| [Glitch filtering](GlitchFilter/) | Filtering circuit noise using a `Glitch Filter` in the event model. |
1414
| [LCD Display](LCDDisplay/) | Driving a 16 x 2 LCD using the `MicroLiquidCrystal` library. | [Onboard Button and LED](OnboardButtonAndLed/) | Using the onboard button to light the onboard LED. |
1515
| [Potentiometer and RGB LED](PotentiometerControlled_RgbLed/) | Changing the color of an RGB LED using the `AnalogPort` to read a potentiometer. |
16+
| [Read DS3234 Using SPI](ReadDS3234) | Read the current date and time from a [DS3234](https://www.sparkfun.com/products/10160) Real Time Clock module. |
1617
| [RGB Blinky](RGB_Blinky/) | Simple sample driving a RGB LED. |
1718
| [RGB LED and PWM](RgbLed/) | Driving a RGB LED using PWM. |
1819
| [SD Cards](SDCardIO/) | Reading and writing an SD card. |
155 KB
Loading

ReadDS3234/README.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
![Netduino and DS3234](NetduinoDS3234Fritzing.png)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.40629.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadDS3234", "ReadDS3234\ReadDS3234.csproj", "{14424C06-54C7-4498-9F89-584C21D447C2}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{14424C06-54C7-4498-9F89-584C21D447C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{14424C06-54C7-4498-9F89-584C21D447C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{14424C06-54C7-4498-9F89-584C21D447C2}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{14424C06-54C7-4498-9F89-584C21D447C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{14424C06-54C7-4498-9F89-584C21D447C2}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{14424C06-54C7-4498-9F89-584C21D447C2}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
EndGlobal
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using Microsoft.SPOT;
2+
using Microsoft.SPOT.Hardware;
3+
using SecretLabs.NETMF.Hardware.NetduinoPlus;
4+
using System.Threading;
5+
6+
namespace ReadDS3234
7+
{
8+
public class Program
9+
{
10+
/// <summary>
11+
/// Position of the time elements in the array of bytes read from the DS3234.
12+
/// </summary>
13+
public const int REG_SECONDS = 0;
14+
public const int REG_MINUTE = REG_SECONDS + 1;
15+
public const int REG_HOUR = REG_MINUTE + 1;
16+
public const int REG_WEEKDAY = REG_HOUR + 1;
17+
public const int REG_DAY = REG_WEEKDAY + 1;
18+
public const int REG_DATE = REG_DAY + 1;
19+
public const int REG_MONTH = REG_DATE + 1;
20+
public const int REG_YEAR = REG_MONTH + 1;
21+
public const int REG_SIZE = REG_YEAR + 1;
22+
23+
/// <summary>
24+
/// Convert the BCD byte into a a byte.
25+
/// </summary>
26+
/// <param name="value">Value to convert</param>
27+
/// <returns>Decimal represention of the BCD number.</returns>
28+
public static byte ConvertBCDToByte(byte value)
29+
{
30+
byte result;
31+
32+
result = (byte) ((value & 0xf0) >> 4);
33+
result *= 10;
34+
result += (byte) (value & 0x0f);
35+
return (result);
36+
}
37+
38+
/// <summary>
39+
/// Convert a byte value into a BCD encoded version of the data.
40+
/// </summary>
41+
/// <param name="value">Byte value to convert.</param>
42+
/// <returns>Byte containing the BCD representation of value.</returns>
43+
public static byte ConvertByteToBCD(byte value)
44+
{
45+
byte result = (byte) (value % 10);
46+
result |= (byte) ((value / 10) << 4);
47+
return (result);
48+
}
49+
50+
/// <summary>
51+
/// Decode the data from the DS3234 and return a string representation of the date / time.
52+
/// </summary>
53+
/// <param name="dataFromChip">Byte array containing the date / time registers from the DS3234.</param>
54+
/// <returns>String representation of the date and time.</returns>
55+
public static string DecodeTime(byte[] dataFromChip)
56+
{
57+
byte hour, minutes, seconds, day, month;
58+
int year;
59+
60+
seconds = ConvertBCDToByte(dataFromChip[REG_SECONDS]);
61+
minutes = ConvertBCDToByte(dataFromChip[REG_MINUTE]);
62+
if ((dataFromChip[REG_HOUR] & 0x40) > 0)
63+
{
64+
hour = ConvertBCDToByte((byte) (dataFromChip[REG_HOUR] & 0x1f));
65+
if ((dataFromChip[REG_HOUR] & 0x20) > 0)
66+
{
67+
hour += 12;
68+
}
69+
}
70+
else
71+
{
72+
hour = ConvertBCDToByte((byte) (dataFromChip[REG_HOUR] & 0x3f));
73+
}
74+
day = ConvertBCDToByte(dataFromChip[REG_DAY]);
75+
month = ConvertBCDToByte((byte) (dataFromChip[REG_MONTH] & 0x7f));
76+
year = 1900 + ConvertBCDToByte(dataFromChip[REG_YEAR]);
77+
if ((dataFromChip[REG_MONTH] & 0x80) > 0)
78+
{
79+
year += 100;
80+
}
81+
return ("Current time: " + hour.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString() + " Date: " + day.ToString() + "-" + month.ToString() + "-" + year.ToString());
82+
}
83+
84+
/// <summary>
85+
/// Convert the data byte into a hexadecimal string representation of the number.
86+
/// </summary>
87+
/// <param name="data">Value to convert.</param>
88+
/// <returns>String containing the hexadecimal representation of the number.</returns>
89+
public static string Hexadecimal(byte data)
90+
{
91+
const string characters = "0123456789abcdef";
92+
string result = "0x";
93+
94+
result += characters[data >> 4];
95+
result += characters[data & 0x0f];
96+
return (result);
97+
}
98+
99+
/// <summary>
100+
/// Main program loop.
101+
/// </summary>
102+
public static void Main()
103+
{
104+
SPI.Configuration spiConfig = new SPI.Configuration(
105+
ChipSelect_Port: Pins.GPIO_PIN_D7, // Chip select is digital IO 7.
106+
ChipSelect_ActiveState: false, // Chip select is active low.
107+
ChipSelect_SetupTime: 0, // Amount of time between selection and the clock starting
108+
ChipSelect_HoldTime: 0, // Amount of time the device must be active after the data has been read.
109+
Clock_Edge: false, // Sample on the falling edge.
110+
Clock_IdleState: false, // Clock is idle when low.
111+
Clock_RateKHz: 10, // 10KHz clock speed.
112+
SPI_mod: SPI_Devices.SPI1 // Use SPI1
113+
);
114+
115+
SPI spi = new SPI(spiConfig);
116+
int count = 0;
117+
while (true)
118+
{
119+
byte[] dataOut = new byte[] { 0 };
120+
byte[] dataIn = new byte[REG_SIZE];
121+
spi.WriteRead(dataOut, dataIn, 1);
122+
count++;
123+
string message = "Read " + count.ToString() + ":";
124+
for (int index = 0; index < dataIn.Length; index++)
125+
{
126+
message += " " + Hexadecimal(dataIn[index]);
127+
}
128+
Debug.Print(message);
129+
Debug.Print(DecodeTime(dataIn));
130+
Thread.Sleep(2000);
131+
}
132+
}
133+
}
134+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ReadDS3234")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ReadDS3234")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Version information for an assembly consists of the following four values:
18+
//
19+
// Major Version
20+
// Minor Version
21+
// Build Number
22+
// Revision
23+
//
24+
[assembly: AssemblyVersion("1.0.0.0")]
25+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<AssemblyName>ReadDS3234</AssemblyName>
5+
<OutputType>Exe</OutputType>
6+
<RootNamespace>ReadDS3234</RootNamespace>
7+
<ProjectTypeGuids>{b69e3092-b931-443c-abe7-7e7b65f2a37f};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
8+
<ProductVersion>9.0.21022</ProductVersion>
9+
<SchemaVersion>2.0</SchemaVersion>
10+
<ProjectGuid>{14424C06-54C7-4498-9F89-584C21D447C2}</ProjectGuid>
11+
<TargetFrameworkVersion>v4.3</TargetFrameworkVersion>
12+
<NetMfTargetsBaseDir Condition="'$(NetMfTargetsBaseDir)'==''">$(MSBuildExtensionsPath32)\Microsoft\.NET Micro Framework\</NetMfTargetsBaseDir>
13+
<DeployDevice>Netduino</DeployDevice>
14+
<DeployTransport>USB</DeployTransport>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<Import Project="$(NetMfTargetsBaseDir)$(TargetFrameworkVersion)\CSharp.Targets" />
34+
<ItemGroup>
35+
<Compile Include="Properties\AssemblyInfo.cs" />
36+
<Compile Include="Program.cs" />
37+
</ItemGroup>
38+
<ItemGroup>
39+
<Reference Include="Microsoft.SPOT.Hardware" />
40+
<Reference Include="Microsoft.SPOT.Hardware.PWM" />
41+
<Reference Include="Microsoft.SPOT.Hardware.SerialPort" />
42+
<Reference Include="Microsoft.SPOT.Native" />
43+
<Reference Include="Microsoft.SPOT.Net" />
44+
<Reference Include="SecretLabs.NETMF.Hardware" />
45+
<Reference Include="SecretLabs.NETMF.Hardware.NetduinoPlus, Version=4.3.1.0, Culture=neutral, processorArchitecture=MSIL" />
46+
<Reference Include="System" />
47+
<Reference Include="GoBus" />
48+
</ItemGroup>
49+
</Project>

0 commit comments

Comments
 (0)