This is the first time that I have attempted I2C. I am trying to get a Parallax gyro ( L3G4200D ) working with my Netduino and I don't seem to be getting good numbers for the x/y/z ( I get initial numbers and they never change with movement, nor do they look correct).
I lifted some of the I2C code from LoveElectronics.Sensors.Accelerometers. I tried to use this: "Arduino example with the L3G4200D" and this: "Parallax kickstart"as other points of reference.
I have posted a very simple version of my code below. I don't feel comfortable with what I am passing to the Configuration constructor, but I am not sure.
Any help here would be appreciated.
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using LoveElectronics.Sensors.Accelerometers; using LoveElectronics.Resources; namespace NetduinoApplication1 { public class gyroTest : I2CDevice { private int m_Timeout = 1000; // int Addr = 105; // 0x69 Adress for L3G4200D private ushort m_Address; public gyroTest() : this(0x69) { } public gyroTest(ushort address) : base(new Configuration(address, 100)) { m_Address = address; } public byte[] Read(byte memoryAddress, int responseLength = 1) { var buffer = new byte[responseLength]; I2CDevice.I2CTransaction[] transaction; transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { memoryAddress }), I2CDevice.CreateReadTransaction(buffer) }; int result = Execute(transaction, m_Timeout); return buffer; } public void Write(byte memoryAddress, byte value) { I2CTransaction[] transaction; transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { memoryAddress, value }) }; int result = Execute(transaction, m_Timeout); } } public class Program { public static void Main() { gyroTest t = new gyroTest(); t.Write(0x1F, 0x20); // Turn on XYZ axes t.Write(0x08, 0x22); // control t.Write(0x80, 0x23); // Scale to 500 deg/sec Thread.Sleep(1000); int timerPeriod = 50; Timer timer = new Timer(delegate(object o) { byte[] MSBx = t.Read(0x29, 1); byte[] LSBx = t.Read(0x28, 1); int x = ((MSBx[0] << 8) | LSBx[0]); byte[] MSBy = t.Read(0x2B, 1); byte[] LSBy = t.Read(0x2A, 1); int y = ((MSBy[0] << 8) | LSBy[0]); byte[] MSBz = t.Read(0x2D, 1); byte[] LSBz = t.Read(0x2C, 1); int z = ((MSBz[0] << 8) | LSBz[0]); Debug.Print("X: " + x + " Y: " + y + " Z: " + z); }, null, 0, timerPeriod); // Make sure the device runs the timer and doesnt end execution. while (true) { Thread.Sleep(Timeout.Infinite); } } } }