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

本文关键字:读取 数据 传感器 使用 Netduino i2c | 更新日期: 2023-09-27 18:35:17

我不久前开始学习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);

        // ???
    }
}

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

看起来你错过了I2C。执行调用。在不知道您与之通信的设备的情况下,这至少会开始传输。

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

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