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:
- Netduino 4.2 beta 1
- Breakout Board for PCF8575 I2C Expander - Datasheet
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