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

Netduino 4.2 beta 1 + PCF8575 I2C Port expander


  • Please log in to reply
8 replies to this topic

#1 vzgromozdun

vzgromozdun

    New Member

  • Members
  • Pip
  • 3 posts

Posted 31 July 2011 - 03:38 PM

Hi All,

This is my first post here.

I have problems with connecting PCF8575 port extender to my netduino.

Goal: Implement trivial blink using port extender.
Actual result: there are always logical "1" at output ports.

Could anybody please look what I'm doing wrong?
Have red post about "i2c internal address" - i'm not 100% sure what this is required for me, but I have found in expander datasheet following:

To
write to the ports (output mode), the master first addresses the slave device, setting the last bit of the byte
containing the slave address to logic 0.



I'm using following hardware:

Wiring:
  • SCL - attached to pin 5
  • SDA - attached to pin 4
  • VCC - used pullup resitors to +5V
  • GND - nothing special

Here is my code:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Reflection;

namespace NetduinoApplication1
{
    public class Program
    {
        static ushort DEVICE_ID = 0x20;

        public static void Main()
        {
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

            I2CDevice.Configuration conf = new I2CDevice.Configuration(DEVICE_ID, 100);
            I2CDevice dev = new I2CDevice(conf);

            while (true)
            {
                led.Write(true);
                I2CDevice.I2CWriteTransaction writeUpTrans = CreateWriteTransaction(new byte[2] { 255, 255 }, DEVICE_ID, 1);
                int sended = dev.Execute(new I2CDevice.I2CTransaction[] { writeUpTrans }, 1000);
                Thread.Sleep(100);

                led.Write(false);
                I2CDevice.I2CWriteTransaction writeDownTrans = CreateWriteTransaction(new byte[2] { 0, 0 }, DEVICE_ID, 1);
                sended = dev.Execute(new I2CDevice.I2CTransaction[] {  writeDownTrans }, 1000);
                Thread.Sleep(100);

            }
        }

        static I2CDevice.I2CWriteTransaction CreateWriteTransaction(byte[] buffer, uint internalAddress, byte internalAddressSize)
        {
            I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(buffer);
            Type writeTransactionType = typeof(I2CDevice.I2CWriteTransaction);

            FieldInfo fieldInfo = writeTransactionType.GetField("Custom_InternalAddress", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(writeTransaction, internalAddress);

            fieldInfo = writeTransactionType.GetField("Custom_InternalAddressSize", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(writeTransaction, internalAddressSize);

            return writeTransaction;
        }

        static I2CDevice.I2CReadTransaction CreateReadTransaction(byte[] buffer, uint internalAddress, byte internalAddressSize)
        {
            I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(buffer);
            Type readTransactionType = typeof(I2CDevice.I2CReadTransaction);

            FieldInfo fieldInfo = readTransactionType.GetField("Custom_InternalAddress", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(readTransaction, internalAddress);

            fieldInfo = readTransactionType.GetField("Custom_InternalAddressSize", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(readTransaction, internalAddressSize);

            return readTransaction;
        }
    }
}

Thanks in advance,
Dmitry

#2 vzgromozdun

vzgromozdun

    New Member

  • Members
  • Pip
  • 3 posts

Posted 08 August 2011 - 07:28 PM

Well, it works right now actually! I did stupid mistake - I have used digital ports 4 and 5 for I2C instead of analog ones. And another one correction - expander works fine with 5k pullup resistors.

#3 fastlink30

fastlink30

    New Member

  • Members
  • Pip
  • 4 posts

Posted 11 August 2011 - 10:01 AM

i was trying i2c on netduino, for this i attached 8574a to it, but after many try, nothing happen, so , i looked output level on a5/a4.
a4 0=0v, 1=3,3
a5 000v, 1=2,5v

is normal? a5 is blowed?

firmware 4.2 beta1

i tried look levels with this simple code:
        Dim led1 As New OutputPort(Pins.GPIO_PIN_A4, False)
        Dim led2 As New OutputPort(Pins.GPIO_PIN_A5, False)
        Dim led3 As New OutputPort(Pins.GPIO_PIN_A3, False)

        led.Write(True)

        While (True)
            led1.Write(True)
            led2.Write(True)
            led3.Write(True)
            Thread.Sleep(4000)
            led1.Write(False)
            led2.Write(False)
            led3.Write(False)
            Thread.Sleep(4000)
        End While


#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 August 2011 - 01:09 AM

Hi fastlink30, With I2C, the voltages are 3.3V or 5.0V by default due to "open drain" and 0V when driven to GND by the Netduino or by your I2C devices. Do you currently have pullups on A4/A5? Try using TristatePort and switching its state (between high-z and driving 0 volts). Chris

#5 fastlink30

fastlink30

    New Member

  • Members
  • Pip
  • 4 posts

Posted 12 August 2011 - 08:18 AM

Hi fastlink30,

With I2C, the voltages are 3.3V or 5.0V by default due to "open drain" and 0V when driven to GND by the Netduino or by your I2C devices.

Do you currently have pullups on A4/A5? Try using TristatePort and switching its state (between high-z and driving 0 volts).

Chris


yes, a4/a5 have pullup, also i modified my code as follow:
        Dim led3 As New OutputPort(Pins.GPIO_PIN_A3, False)
        Dim led1 As TristatePort = New TristatePort(
            Pins.GPIO_PIN_A4,
            False,
            False,
            Port.ResistorMode.PullUp)
        Dim led2 As TristatePort = New TristatePort(
            Pins.GPIO_PIN_A5,
            False,
            False,
            Port.ResistorMode.PullUp)

        While (True)
            Try
                led1.Write(True) <<<<<<<
                led2.Write(True)
                Thread.Sleep(4000)
                led1.Write(False)
                led2.Write(False))
                Thread.Sleep(4000)
            Catch ex As Exception
                Dim s As String = ""
            End Try
        End While

but now, when i arrive to led1.write, i caught an exception

Exception first-chance of type 'System.InvalidOperationException' in Microsoft.SPOT.Hardware.dll

??? maybe framework beta version??

all my reference are for 4.2

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 12 August 2011 - 09:34 AM

Exception first-chance of type 'System.InvalidOperationException' in Microsoft.SPOT.Hardware.dll

You'd probably need to switch the port to output mode:

  ...
  led1.Active = True
  led1.Write(True)


#7 fastlink30

fastlink30

    New Member

  • Members
  • Pip
  • 4 posts

Posted 12 August 2011 - 02:59 PM

You'd probably need to switch the port to output mode:

  ...
  led1.Active = True
  led1.Write(True)


yes, with this code, loop are ok,
level with board power now are
a4 0=0 1=3.3
a5 0=0 1=4,8

but if i leave the loop and put the code for i2c, i get error, i presume becouse i2c routine not want floating pins

so, i not understand where is the problem, program is quite simple.

        Dim data1(0) As Byte
        Dim data2(0) As Byte
        Dim sended As Integer

        Dim DEVICE_ID As UShort = &H70 >> 1 'pcf8574a address 0

        Debug.Print("Started " & DEVICE_ID.ToString)

        Dim conf As Microsoft.SPOT.Hardware.I2CDevice.Configuration = New I2CDevice.Configuration(DEVICE_ID, 100)
        Dim PCF8574A As I2CDevice = New I2CDevice(conf)

        data1(0) = &HAA
        Dim writeUpTrans As I2CDevice.I2CWriteTransaction = I2CDevice.CreateWriteTransaction(data1)
        Dim WriteGPIO() As I2CDevice.I2CTransaction = New I2CDevice.I2CTransaction() {writeUpTrans}

        data2(0) = &H55
        Dim writeUpTrans2 As I2CDevice.I2CWriteTransaction = I2CDevice.CreateWriteTransaction(data2)
        Dim WriteGPIO2() As I2CDevice.I2CTransaction = New I2CDevice.I2CTransaction() {writeUpTrans2}

        ' Dim readUpTrans As I2CDevice.I2CReadTransaction = I2CDevice.CreateReadTransaction(data1)
        'Dim ReadGPIO() As I2CDevice.I2CTransaction = New I2CDevice.I2CTransaction() {readUpTrans}

        'sended = PCF8574A.Execute(ReadGPIO, 100)
        'Debug.Print("Get =" & sended.ToString & " - " & data(0).ToString)

        While (True)
            sended = PCF8574A.Execute(WriteGPIO, 100)
            'Debug.Print("Send1=" & sended.ToString)
            led.Write(True)
            Thread.Sleep(200)

            sended = PCF8574A.Execute(WriteGPIO2, 100)
            'Debug.Print("Send2=" & sended.ToString)
            led.Write(False)
            Thread.Sleep(200)
        End While


#8 vzgromozdun

vzgromozdun

    New Member

  • Members
  • Pip
  • 3 posts

Posted 23 August 2011 - 09:23 AM

        Dim DEVICE_ID As UShort = &H70 >> 1 'pcf8574a address 0


Not clearly understand why your expander have such address.
Mine works just fine with default 0x20 address (all address pins of expander connected to GND).

Regards,
Dmitry

#9 fastlink30

fastlink30

    New Member

  • Members
  • Pip
  • 4 posts

Posted 25 August 2011 - 07:54 AM

Not clearly understand why your expander have such address.
Mine works just fine with default 0x20 address (all address pins of expander connected to GND).

Regards,
Dmitry


becouse probably you are using pcf8574 not 8574a, they have different address mask




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.