i2c使用Netduino从传感器读取数据

时间:2016-02-19 09:50:18

标签: c# .net frameworks i2c netduino

我很久以前就开始学习Netduino了。现在,我想将它与MS5803 30BAR传感器一起使用。此组件与I2C协议通信。我学会了这个协议,但还不够。

我写了代码介绍。当我来到主代码时,我什么也没做。我的代码如下。

有人可以帮忙解决这个问题吗?我会很高兴的。)

public class Program
{
    public static void Main()
    {
        // Configuration of MS5803 30BA
        I2CDevice i2c = new I2CDevice(new I2CDevice.Configuration(0x76>>1, 400));

        byte[] read = new byte[1];

        I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[1];
        i2cTx[0] = I2CDevice.CreateReadTransaction(read);


        // ???
    }
}

1 个答案:

答案 0 :(得分:1)

看起来你错过了I2C.Execute调用。如果不了解您正在与之通信的设备,至少会启动传输。

尝试在创建读取事务后添加此行。

i2c.Execute(i2cTX[0],500);

        byte[] returnByte = new byte[3];

        var readX = new I2CDevice.I2CTransaction[] {I2CDevice.CreateReadTransaction(returnByte) };
        int executed = 0;
        I2CDevice i2c = new I2CDevice(new I2CDevice.Configuration(0x76, 400));            
        executed = i2c.Execute(readX, 400);
            if (executed == 0)
            {
                //Debug.Print("Read FAIL!");
                                }
            else
            {
                //Debug.Print("Read SUCCESS!");
            }
            //throw new Exception("I2C transaction failed");

         //you will need to do some bit shifting with the readX array to get your values.

    }

这是关于netMF i2c的优秀文档:https://www.ghielectronics.com/docs/12/i2c

器件数据表: http://www.amsys-sensor.eu/sheets/amsys.en.ms5803_30ba.pdf