我在面包板上有这个模块:
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。
非常感谢任何帮助。