//This is rewrite and expansion of the class from netduino forum by Rusko //needs cleaned up a little but seems to fuction correctly using System; using System.Text; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; namespace RTC_DS3234 { class DS3234 { private SPI.Configuration Cfg; private SPI ds3234; public DS3234(Cpu.Pin SSPin) { Cfg = new SPI.Configuration( SSPin, false, 0, 0, false, false, 2000, SPI_Devices.SPI1); ds3234 = new SPI(Cfg); write(0x8E, 0x60); } public void Set(DateTime date) { byte[] buf = { toBCD(date.Second), toBCD(date.Minute), toBCD(date.Hour), toBCD((int)date.DayOfWeek), toBCD(date.Day), toBCD(date.Month), toBCD(date.Year-2000) }; write(0x80, buf); return; } private int toHex(byte bcd) { return (int)(((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f)); } public DateTime Get() { byte[] rbuf = read(0x00, 7); int year = 2000 + toHex(rbuf[6]), month = toHex(rbuf[5]), day = toHex(rbuf[4]), hour = toHex(rbuf[2]), minute = toHex(rbuf[1]), second = toHex(rbuf[0]); return new DateTime(year, month, day, hour, minute, second); } public byte ReadSRAM(byte Address) { byte[] cmdbuf = { 0x98, Address }; byte[] rbuf = new byte[1]; ds3234.Write(cmdbuf); cmdbuf[0] = 0x19; ds3234.WriteRead(cmdbuf, 0, 1, rbuf, 0, 1, 1); return rbuf[0]; } public byte[] ReadSRAM(byte Address, int count) { byte[] cmdbuf = { 0x98, Address }; byte[] rbuf = new byte[count]; ds3234.Write(cmdbuf); cmdbuf[0] = 0x19; ds3234.WriteRead(cmdbuf, 0, 1, rbuf, 0, count, 1); return rbuf; } public void WriteSRAM(byte Address, byte Value) { byte[] wbuf = { 0x98, Address, Value }; ds3234.Write(wbuf); return; } public void WriteSRAM(byte Address, byte[] values) { byte[] wbuf = new byte[values.Length + 2]; wbuf[0] = 0x98; wbuf[1] = Address; values.CopyTo(wbuf,2); ds3234.Write(wbuf); return; } public float readTemp() { byte[] rbuf = new byte[2]; byte[] wbuf = { 0x11 }; ds3234.WriteRead(wbuf, 0, 1, rbuf, 0, 2, 1); int rc = (int)(rbuf[0] << 2) + (rbuf[1] >> 6); return rc / 4.0F; } // Private methods private byte toBCD(int val) { byte a = (byte)(val / 10); byte b = (byte)(val % 10); return (byte)((a << 4) | B); } private void write(byte address, byte value) { byte[] buffer = { address, value }; ds3234.Write(buffer); } private void write(byte address, byte[] values) { byte[] buffer = new byte[values.Length + 1]; values.CopyTo(buffer, 1); buffer[0] = address; ds3234.Write(buffer); } private byte read(byte address){ byte[] rbuf = new byte[1]; byte[] wbuf = { address }; ds3234.WriteRead(wbuf, 0, 1, rbuf, 0, 1, 1); return rbuf[0]; } private byte[] read(byte address, int count) { byte[] rbuf = new byte[count]; byte[] wbuf = { address }; ds3234.WriteRead(wbuf, 0, 1, rbuf, 0, count, 1); return rbuf; } } }
- Netduino Forums
- → Viewing Profile: Posts: rcomeau
The Netduino forums have been replaced by new forums at community.wildernesslabs.co.
This site has been preserved for archival purposes only
and the ability to make new accounts or posts has been turned off.
Community Stats
- Group Members
- Active Posts 3
- Profile Views 4404
- Member Title New Member
- Age Age Unknown
- Birthday Birthday Unknown
-
Gender
Not Telling
0
Neutral
User Tools
Friends
rcomeau hasn't added any friends yet.
Posts I've Made
In Topic: SPI interface with RTC DS3234
27 May 2012 - 04:13 PM
This is a rewrite and expansion of Rusto's class
In Topic: Netduino Firmware v4.2.0 RC5 (Netduino + Netduino Plus)
23 May 2012 - 04:23 PM
Ok as one new to the netduino plus, where do I find EXACT instructions on how to use SAM-BA to install the 4.2 bootloader?
Then what files have to be MFDeployed?
- Netduino Forums
- → Viewing Profile: Posts: rcomeau
- Privacy Policy