Netduino home hardware projects downloads community

Jump to content


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.
Photo

SPI interface with RTC DS3234


  • Please log in to reply
23 replies to this topic

#21 Rusko

Rusko

    New Member

  • Members
  • Pip
  • 9 posts

Posted 01 October 2011 - 04:30 PM

Thanks a lot Thomas!
I just finished to write a few lines of code to manage the RTC DS3234 (see file "csDS3234.cs" inside the enclosed zip), hope can be useful!

Ah.. the connections:
CLK - pin 13
MISO - pin 12
MOSI - pin 11
SS - SW defined (pin 10 in my code)

Attached Files


--
Rusko

#22 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 14 October 2011 - 09:04 AM

Thanks a lot Thomas!
I just finished to write a few lines of code to manage the RTC DS3234 (see file "csDS3234.cs" inside the enclosed zip), hope can be useful!

Ah.. the connections:
CLK - pin 13
MISO - pin 12
MOSI - pin 11
SS - SW defined (pin 10 in my code)


Dear Rusko,

Thank you for sharing your codes.
For me, after downloading your zip and test out the code with your recommended connections, including the VCC and GND on the Netdunio plus, I got the exception when doing the readback check. I still get 255 from the read buffer[0].

I have also inserted the battery.

Any advice?

#23 Shy_B

Shy_B

    New Member

  • Members
  • Pip
  • 3 posts

Posted 08 February 2012 - 09:35 AM

Dear Rusko,

Thank you for sharing your codes.
For me, after downloading your zip and test out the code with your recommended connections, including the VCC and GND on the Netdunio plus, I got the exception when doing the readback check. I still get 255 from the read buffer[0].

I have also inserted the battery.

Any advice?



Was this ever solved ?

I'm getting 0xff or random values in buffer[0].

Thanks

#24 rcomeau

rcomeau

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 May 2012 - 04:13 PM

This is a rewrite and expansion of Rusto's class

//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;
		}

    }
}






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.