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

DS1307 Sparkfun I2C


  • Please log in to reply
34 replies to this topic

#1 gomore11

gomore11

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 14 July 2011 - 02:31 PM

Hello everybody.I´m new in the forum but i would be very glad if somebody could help me. I´m trying to read the seven registers of the DS1307 where there are the time values of this RTC. The registers go from 0x00 to 0x06.I have the SDA pin conected to my Netduino´s Analog pin 4 and the SCL pin to my Netduino´s Analog pin 5. The way i see if the operations are well done, is to see how much data is transfered by the bus only to see if something has bee read. It always gives me 0 data read. The code is the next:

Please, i´ve been 3 days with it and i can´t find a solution.

namespace I2C_RTC
{
    public class Program
    {
        public static void Main()
        {
            byte[] buffer_read = new byte[7];
            byte[] buffer_write = new byte[1];


            I2CDevice.Configuration config = new I2CDevice.Configuration(0x68, 100);
            I2CDevice RTC = new I2CDevice(config);

            buffer_write[0] = 0x00;

            I2CDevice.I2CReadTransaction tran_read = I2CDevice.CreateReadTransaction(buffer_read);
            I2CDevice.I2CWriteTransaction tran_write = I2CDevice.CreateWriteTransaction(buffer_write);

            

            I2CDevice.I2CTransaction[] tran = new I2CDevice.I2CTransaction[]{tran_write};

            int data = RTC.Execute(tran, 100);

            Debug.Print("\nThe number of data transfered is: "+ data.ToString());

            tran = new I2CDevice.I2CTransaction[] { tran_read };
           
            data = RTC.Execute(tran, 100);

            Debug.Print("\nThe number of data transfered is: " + data.ToString());

        }

    }
}


#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 14 July 2011 - 02:41 PM

Hi Gomore,

Welcome to the boards!
I've got a simular board, samples and connection schematic @ http://wiki.netduino...k-Breakout.ashx

I say simular board, since mine isn't from Sparkfun but from Adafruit.
A big difference I noticed are the pullup resistors which aren't placed on the Sparkfun version. You should put them in yourself I suppose.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 gomore11

gomore11

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 15 July 2011 - 07:38 AM

Hi Stefan. First thank you for answering me. I already tried to use your proyect´s code but there is no way, it doesn´t work. I also tried with pullup resistors and without them, but nothing.......the extrangest thing is that it seams to send data to the board but, i don´t know why, it can´t read nothing from the board(the registers)......

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 15 July 2011 - 08:20 AM

Strange, I see in this thread: http://forums.netdui...eal-time-clock/ Also some suggestions. Perhaps it can help?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 gomore11

gomore11

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 15 July 2011 - 08:38 AM

When i use the code you told me yesterday, it gives me this error:

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll"

And the compiler tells that the problem is in this part of the code:

 public DateTime Now()
        {
            var data = new byte[7];
            int result = Read(0x00, data);

            //TODO: Add exception handling if result == 0

            var dt = new DateTime(
                (int)2000 + data[6].FromBCD(),               // Year
                (int)data[5].FromBCD(),                      // Month
                (int)data[4].FromBCD(),                      // Day
                (int)(((byte)(data[2] & 0x3f)).FromBCD()),     // Hour
                (int)data[1].FromBCD(),                      // Minute
                (int)(((byte)(data[0] & 0x7f)).FromBCD())      // Second
                );
            return dt;
        }

I don´t know wehat could be the problem, the hardware or the software.........

#6 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 15 July 2011 - 08:47 AM

The "//TODO: Add exception handling if result == 0" tells me that there can happen an error if no date is set. The first time you use the chip, it needs to know the current date. Have you used the SetClock method since you placed the battery?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#7 gomore11

gomore11

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 15 July 2011 - 09:01 AM

Yes, the date is ok, we tried the hardware with an Arduino and it worked well,and at the first time we introduced the correct date in the boards DS1307 registers. The problem came when we started to work with the Netduino and .net MicroFramework, thats the reason why I think that the problem is probabilly a software problem,becouse the conections are well done and the board works fine with Arduino. I don´t know what I am doing wrong but its trying me crazy!

#8 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 15 July 2011 - 09:31 AM

Just a wild guess, have you tried swapping the analog pins 4 and 5? I know the chip doesn't work on 3.3V, only 5V. perhaps that could be an issue as well? I completely understand your frustrations, but I know the project in that other tread works with the Adafruit board which has the same chip and the only difference I can see are the missing resistors on the Sparkfun board.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#9 Stuart Crawshaw

Stuart Crawshaw

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts
  • LocationLeeds, United Kingdom

Posted 15 July 2011 - 09:39 AM

Just to add as i dont know if its been touched on in this thread. The DS1207 requires the repeated start support of one of the custom built FW's (Im using 4.1 with CW's OneWire Library) Can you confirm if you are using the stock firmware or a custom build with the repeated start bit support?
Intelligent People Aren't Afraid To Ask For Help.

#10 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 15 July 2011 - 09:40 AM

Can you confirm if you are using the stock firmware or a custom build with the repeated start bit support?

Good one! I can confirm it works with:
- Netduino Firmware v4.1.1 BETA 1
- Netduino Firmware v4.2.0 BETA 1
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#11 Stuart Crawshaw

Stuart Crawshaw

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts
  • LocationLeeds, United Kingdom

Posted 15 July 2011 - 09:42 AM

Also i noticed this in your code

I2CDevice.I2CTransaction[] tran = new I2CDevice.I2CTransaction[]{tran_write}; 
 
            int data = RTC.Execute(tran, 100); 
 
            Debug.Print("\nThe number of data transfered is: "+ data.ToString()); 
 
            tran = new I2CDevice.I2CTransaction[] { tran_read }; 
            
            data = RTC.Execute(tran, 100); 


After each Execute, it would produce a stop bit. Thus cancelling the address it was at (I think) and the read may not know where to reference.

Stefan, Can you confirm this? as i dont have my DS1307....yet...
Intelligent People Aren't Afraid To Ask For Help.

#12 gomore11

gomore11

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 15 July 2011 - 09:42 AM

jajaj yes yes, i tried swapping and trying to work with 3.3v also but nothing..........what a hell jejejeejejejeje

#13 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 15 July 2011 - 09:44 AM

Stefan, Can you confirm this? as i dont have my DS1307....yet...

Can't confirm nor contradict it; I didn't wrote the code, only successfully implemented it. But I used the 4.1.1 beta firmware at that time.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#14 Stuart Crawshaw

Stuart Crawshaw

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts
  • LocationLeeds, United Kingdom

Posted 15 July 2011 - 09:49 AM

Can't confirm nor contradict it; I didn't wrote the code, only successfully implemented it. But I used the 4.1.1 beta firmware at that time.


Ahh ok,

I believe it would thought, makes sense that it would to me.

If it is the case, then the read command would be executed without an address being sent to the device.
Intelligent People Aren't Afraid To Ask For Help.

#15 gomore11

gomore11

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 15 July 2011 - 10:30 AM

I have the 4.1.0.6 firmware version......this could be the problem??????, I also have to say that doing the adders write and the read transacations at the same Execute it doesn´t work neither.

#16 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 15 July 2011 - 10:51 AM

I have the 4.1.0.6 firmware version......this could be the problem??????

Absolutely, could you try the 4.1.1 beta 1 firmware? According to it's release notes:

I2C "repeated start bit" enhancements.


"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#17 gomore11

gomore11

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 15 July 2011 - 11:24 AM

I updated the firmware to 4.1.1 beta version but I´m in the same way, it doesn´t work neither with my code not with that one you posted, it gives me the same error that i have told you before..........

#18 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 15 July 2011 - 11:30 AM

I updated the firmware to 4.1.1 beta version but I´m in the same way, it doesn´t work neither with my code not with that one you posted, it gives me the same error that i have told you before..........

After the firmware you connected everyting -including resistors- on 5v I suppose?
When I'm home, I'll try your code with my board. Hopefully we can work to a solution!
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#19 Stuart Crawshaw

Stuart Crawshaw

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts
  • LocationLeeds, United Kingdom

Posted 15 July 2011 - 11:35 AM

I updated the firmware to 4.1.1 beta version but I´m in the same way, it doesn´t work neither with my code not with that one you posted, it gives me the same error that i have told you before..........


OK,

Can you post the code you have used, please bare in mind that the transaction for a read should include an initial write (to write the address of the register to be read) and then a read.

Please see this excract of code from the FusionWare I2C Library (attached).
/// <summary>Performs a common Write-repeatstart-read command response operation</summary>
/// <param name="Config">Configuration for the bus during this operation</param>
/// <param name="WriteBuffer">Buffer containing data to write</param>
/// <param name="ReadBuffer">Buffer to receive data. The Length property of ReadBuffer determines the number of bytes to read</param>
/// <param name="TimeOut">Millisecond time out value</param>
/// <remarks>
/// Many I2C devices have a simple command response protocol of some sort
/// this method simplifies the implementation of device specific drivers
/// by wrapping up the I2CTransaction creation and timeout detection etc.
/// to support a simple command/response type of protocol. It creates a write
/// transaction and a read transaction with a repeat-start condition in between
/// to maintain control of the bus for the entire operation. 
/// </remarks>
/// <exception cref="T:System.IO.IOException">Operation failed to complete</exception>
public void WriteRead(I2CDevice.Configuration Config, byte[] WriteBuffer, byte[] ReadBuffer, int TimeOut)
{
    ThrowIfDisposed();
    lock(this.Device)
    {
        this.Device.Config = Config;
        I2CDevice.I2CTransaction[] xacts = new I2CDevice.I2CTransaction[] {
            I2CDevice.CreateWriteTransaction(WriteBuffer),
            I2CDevice.CreateReadTransaction(ReadBuffer)
        };

        // I2CDevice.Execute returns the total number of bytes
        // transfered in BOTH directions for all transactions
        int byteCount = this.Device.Execute(xacts, TimeOut);
        if(byteCount < (WriteBuffer.Length + ReadBuffer.Length))
            throw new System.IO.IOException();
    }
}

EDIT: Corrected spelling of FusionWare

Attached Files


Intelligent People Aren't Afraid To Ask For Help.

#20 gomore11

gomore11

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 15 July 2011 - 11:36 AM

Yes, I connected everithing well again......thank you very much for wasting your time with my problem, shure that the problem it´s a little detail(a dumb detail shure) but if I can i would try to put a photo of whow I´ve got the connections.........




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.