Netduino home hardware projects downloads community

Jump to content


Photo

I2C strange behavior


  • Please log in to reply
14 replies to this topic

#1 hareega

hareega

    New Member

  • Members
  • Pip
  • 4 posts

Posted 15 November 2015 - 02:53 AM

Greeting guys 

 

what i'm tying to do is sort of pinging a device for a three times

and i want to know why the I2C execution return 0 in the first time but not in the rest.

            var config = new I2CDevice.Configuration(83, 100);
            var I2C_Bus = new I2CDevice(config);

            I2CDevice.I2CTransaction[] transactions;
            transactions = new I2CDevice.I2CTransaction[2];

            transactions[0] = I2CDevice.CreateWriteTransaction(new byte[1]);
            transactions[1] = I2CDevice.CreateReadTransaction(new byte[1]);

            for (var i = 0; i < 3; i++)
            {
                int result = I2C_Bus.Execute(transactions, 1000);

                if (result > 0)
                {
                    Debug.Print("found");
                }
                else
                {
                    Debug.Print("not found");
                }
            }

This is the debug result i get :

not found
found
found

any ideas?  :huh:



#2 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 15 November 2015 - 03:28 AM

Hi 

 

Try strobing the SDA line just before you open the port

using (OutputPort i2cPort = new OutputPort(Pins.GPIO_PIN_SDA, true))
{
   i2cPort.Write(false);
   Thread.Sleep(100);
}

I think the earliest reference to this issue may have been

 

http://forums.netdui...5-barometer-i2c

 

 

@KiwiBryn

blog.devmobile.co.nz



#3 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1070 posts
  • LocationNorth Yorkshire, UK

Posted 15 November 2015 - 08:39 AM

Usual question with I2C - have you installed pull-up resistors if the board you are using does not already have them installed.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#4 hareega

hareega

    New Member

  • Members
  • Pip
  • 4 posts

Posted 15 November 2015 - 02:15 PM

Hi 

 

Try strobing the SDA line just before you open the port

using (OutputPort i2cPort = new OutputPort(Pins.GPIO_PIN_SDA, true))
{
   i2cPort.Write(false);
   Thread.Sleep(100);
}

I think the earliest reference to this issue may have been

 

http://forums.netdui...5-barometer-i2c

 

 

@KiwiBryn

blog.devmobile.co.nz

 

that method worked thanks

 

Usual question with I2C - have you installed pull-up resistors if the board you are using does not already have them installed.

 

Regards,

Mark

 

i thought the board has built in pull-up resistor

so yeah that's what i missed



#5 NitinSontakke

NitinSontakke

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationPune, Maharashtra, India

Posted 17 November 2015 - 05:20 PM

@hareega,

 

If you could care to mention the device / chip you are using, please!

 

I am using NP2 and NETMF 4.3 and....

 

I have been struggling to get i2c working with PCF8574 and PCF8575 over days now with no luck so far and any help from anyone on forum is hugely appreciated.

 

I using classes generously provided by NooM

 

The data-sheet indicates address 0x40, which i tried and also tried the default of 0x20.

 

Pins -> Absolutely nothing special. Pull-up registers are applied as am working on break-out board.

 

Please help!



#6 scardinale

scardinale

    Member

  • Members
  • PipPip
  • 27 posts
  • LocationNew York, USA

Posted 18 November 2015 - 12:32 AM

0x40 is the 8 bit I2C address. NetMF uses 7 bit addressing, so try 0x40 >> 1 as your I2C Address.



#7 NitinSontakke

NitinSontakke

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationPune, Maharashtra, India

Posted 18 November 2015 - 02:10 AM

Thanks! Will try and update.



#8 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1070 posts
  • LocationNorth Yorkshire, UK

Posted 18 November 2015 - 06:51 AM

If the data sheet says the peripheral address is 0x40 then shouldn't you be using this in the .NET code: 

0x40 << 1

The top 7 bits are used to hold the address of the peripheral, the bottom bit indicates if it is a read or write transaction.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#9 scardinale

scardinale

    Member

  • Members
  • PipPip
  • 27 posts
  • LocationNew York, USA

Posted 18 November 2015 - 12:13 PM

That is true about the addressing but NetMF uses a 7-bit addressing scheme and automatically appends the correct bit when reading or writing internally. So all you need to do is supply the 8-bit address right shifted by one.



#10 NitinSontakke

NitinSontakke

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationPune, Maharashtra, India

Posted 18 November 2015 - 05:03 PM

Hello,

 

Right shifting 0x40 by 1 bit gives me 0x20 (decimal 32). Now it makes sense to me why this value is being used in all the code i see here. While it was step in right direction, unfortunately, it hasn't help resolve the issue.

 

Shown below is the code i copied from this thread, added the "magic code block" right at the beginning and changed the address to 0x20.

 

I commented out the "read" transaction because data-sheet mentions that this address is for "write" transaction.

 

The output is "not found" three times.

 

PCF8574 -> N2P connections are as follows:

VCC -> 5V - 10k pull-up registered applied

INT -> Nothing

SCL -> SCL

SDA -> SDA

A0, A1, A2 -> Ground

 

I had taken the photo of assembly but i am facing problem uploading it here.

    public static void Main()
    {
      using (OutputPort i2cPort = new OutputPort(Pins.GPIO_PIN_SDA, true))
      {
        i2cPort.Write(false);
        Thread.Sleep(100);
      }

      I2CDevice.Configuration config = new I2CDevice.Configuration(0x20, 100);
      I2CDevice I2C_Bus = new I2CDevice(config);

      I2CDevice.I2CTransaction[] transactions;
      transactions = new I2CDevice.I2CTransaction[1];

      transactions[0] = I2CDevice.CreateWriteTransaction(new byte[0xff]);
      //transactions[1] = I2CDevice.CreateReadTransaction(new byte[1]);

      for (Int32 i = 0; i < 3; i++)
      {
        Int32 result = I2C_Bus.Execute(transactions, 1000);

        if (result > 0)
        {
          Debug.Print("found");
        }
        else
        {
          Debug.Print("not found");
        }
      }
    }



#11 scardinale

scardinale

    Member

  • Members
  • PipPip
  • 27 posts
  • LocationNew York, USA

Posted 18 November 2015 - 11:49 PM

Try using this example from GHI's codeshare. It will tell you what the I2C address of your device is. If you get an value back it will tell you the 8bit address and verify that your setup is correct.

 

https://www.ghielect...share/entry/294



#12 NitinSontakke

NitinSontakke

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationPune, Maharashtra, India

Posted 19 November 2015 - 05:27 PM

@scardinale, You are a STAR!

 

I copied and executed the I2C Scanner and it gave me Int32 address 112, which i right shifted by 1 and used and it WORKED for my other project!

 

Not sure how on earth one (read, novice like me!) would think of running a scanner to determine an address of a device and then use it.

 

Anyway, I thank you very much for taking away the biggest stumbling block in my journey towards ambitious home automation project.

 

Thanks, again!



#13 scardinale

scardinale

    Member

  • Members
  • PipPip
  • 27 posts
  • LocationNew York, USA

Posted 20 November 2015 - 01:48 AM

Glad to help.



#14 David Weaver

David Weaver

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts
  • LocationBloomington IL

Posted 24 November 2015 - 02:08 PM

This may help,

 

http://netmftoolbox....017/Mcp23017.cs

 

Stefan Thoolen did a great job with the Toolbox.



#15 David Weaver

David Weaver

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts
  • LocationBloomington IL

Posted 24 November 2015 - 02:13 PM

Here is the link to the 12c source code from stefan's Toolbox.

 

http://netmftoolbox....ore/MultiI2C.cs






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.