1

我在面包板上有这个模块:

https://www.pololu.com/product/2127

+3V3 到 Vdd,GND 到 Netduino GND,SDA 到 Netduino 引脚 A4,SCL 到 Netduino 引脚 A5。

我正在尝试使用 NETMF i2c 函数来简单地读取来自 WHO_AM_I 寄存器的响应。我应该看到 0x49,而不是我总是看到 0x73。

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace HelloWorld
{
    public class Program
    {   
        static ushort I2C_ADDRESS = 0x3A>>1;//0011 101*
        static int I2C_CLOCK_RATE_KHZ = 400;
        static I2CDevice.Configuration LSM303D_I2C_CONFIG = new I2CDevice.Configuration(I2C_ADDRESS, I2C_CLOCK_RATE_KHZ);
        static I2CDevice LSM303D = new I2CDevice(LSM303D_I2C_CONFIG);

        public static void Main()
        {   
            byte[] WRITE_BUFFER;
            byte[] READ_BUFFER;
#region write
            WRITE_BUFFER = new byte[2];
            WRITE_BUFFER[0] = (byte)0x0F;

            I2CDevice.I2CTransaction[] writeTransaction = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateWriteTransaction(WRITE_BUFFER)
            };

            int bytesWritten = LSM303D.Execute(writeTransaction, 1000);

            while (bytesWritten < WRITE_BUFFER.Length)
            {
                byte[] temp = new byte[WRITE_BUFFER.Length - bytesWritten];
                Array.Copy(WRITE_BUFFER, bytesWritten, temp, 0, temp.Length);

                writeTransaction = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateWriteTransaction(temp)
            };

                bytesWritten += LSM303D.Execute(writeTransaction, 1000);
            }

            // make sure the data was sent
            if (bytesWritten != WRITE_BUFFER.Length)
            {
                throw new Exception("Could not write to device.");
            }
#endregion

#region read
            READ_BUFFER = new byte[2];
            I2CDevice.I2CTransaction[] readTransaction = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateReadTransaction(READ_BUFFER)
            };

            int bytesRead = LSM303D.Execute(readTransaction, 1000);

            // make sure the data was read
            if (bytesRead != READ_BUFFER.Length)
            {
                throw new Exception("Could not read from device.");
            }
#endregion
            Debug.Print("Response - "+READ_BUFFER[0].ToString()); 
        }   
    }
}

我觉得这很简单,因为我有同一个模块与我的 pic18 对话,尽管它使用了位敲击 i2c。

任何帮助将不胜感激。

4

0 回答 0