Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

ITG-3200 Repeated Start Bit


  • Please log in to reply
2 replies to this topic

#1 spcboog

spcboog

    Member

  • Members
  • PipPip
  • 17 posts

Posted 22 October 2011 - 10:01 PM

Hi all, I am trying to read the WHO_AM_I register from my ITG-3200 (Sparkfun breakout board: http://www.sparkfun.com/products/9801). I couldn't get this to work with 4.1.0.6 so I performed the required upgrade steps to get to 4.2: - Netduino Plus - .NET MF SDK 4.2 RTM - Netduino SDK 4.2.0 June 11 Beta - Updated tinybooterloader and firmware (4.2 RC3) Now I'm trying with the following code: var gyroscope = new I2CDevice(new I2CDevice.Configuration(0x69, 400)); var readBuffer = new byte[1]; var actions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x00 }), CreateReadTransaction(readBuffer, 0x00, 1) }; var result = gyroscope.Execute(actions, 1000); Where CreateReadTransaction is the method posted here: http://forums.netdui...rt-bit-support/ 0x00 is the WHO_AM_I register address. Previously (with 4.1.0.6), gyroscope.Execute(actions, 1000) would return 1, I'm assuming that the read action failed since the buffer remained zeroed. With the new firmware, gyroscope.Execute(actions, 1000) returns 0 (implying that neither action is successful?). Does anyone have any ideas? thanks The ITG-3200 datasheet is here: http://invensense.co...200-00-01.4.pdf

#2 spcboog

spcboog

    Member

  • Members
  • PipPip
  • 17 posts

Posted 26 October 2011 - 10:04 AM

Sorry folks, I didn't have pullups, added 2 4k7's and the read works now. I had mistakenly thought that the sparkfun breakout board already had pullups included but there are just spots for you to add them yourself. I got a BlinkM to see if I could get anything I2C to work at all and when I couldn't get that to work either (also due to me thinking the pullups were already included), I read this: http://forums.netdui...__fromsearch__1 which pointed to the missing pullups.

#3 Jack Chidley

Jack Chidley

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 09 October 2012 - 10:46 AM

I got this to work too, thanks to this forum entry and a host of other, unremembered, entries. I used 10k pull up resistors.

My horrible but work-in-progress, code.
using System;
using System.Reflection;
using System.Net;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace NPlus_IT3200
{
    public class Program
    {
        //create a empty i2c device object
        static I2CDevice myI2C = new I2CDevice(new I2CDevice.Configuration(0, 0));

        public static void Main()
        {
            int r;

            while (true)
            {
                r = (int)read_gyro_state(); //read value from gyro
                Debug.Print("R = " + r.ToString());
                Thread.Sleep(1000);
            }
        }


        I2CDevice.I2CWriteTransaction CreateWriteTransaction(byte[] buffer, uint internalAddress, byte internalAddressSize)
        {
            I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(buffer);
            Type writeTransactionType = typeof(I2CDevice.I2CWriteTransaction);

            FieldInfo fieldInfo = writeTransactionType.GetField("Custom_InternalAddress", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(writeTransaction, internalAddress);

            fieldInfo = writeTransactionType.GetField("Custom_InternalAddressSize", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(writeTransaction, internalAddressSize);

            return writeTransaction;
        }

        I2CDevice.I2CReadTransaction CreateReadTransaction(byte[] buffer, uint internalAddress, byte internalAddressSize)
        {
            I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(buffer);
            Type readTransactionType = typeof(I2CDevice.I2CReadTransaction);

            FieldInfo fieldInfo = readTransactionType.GetField("Custom_InternalAddress", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(readTransaction, internalAddress);

            fieldInfo = readTransactionType.GetField("Custom_InternalAddressSize", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(readTransaction, internalAddressSize);

            return readTransaction;
        }


        //Function to read value from ITG3200 gyro
        //16-bit value over two registers 
        public static double read_gyro_state()
        {
            byte[] WHO_AM_I = new byte [] {0x00};
            byte[] SMPLRT_DIV = new byte[] { 0x15 };
            byte[] DLPF_FS = new byte[] { 0x16 };
            byte[] GYRO_XOUT_H = new byte[] { 0x1D };
            byte[] GYRO_XOUT_L = new byte[] { 0x1E };
            byte[] GYRO_YOUT_H = new byte[] { 0x1F };
            byte[] GYRO_YOUT_L = new byte[] { 0x20 };
            byte[] GYRO_ZOUT_H = new byte[] { 0x21 };
            byte[] GYRO_ZOUT_L = new byte[] { 0x22 };
            byte[] readBuffer = new byte[1];
            int val = 0;
            double readX = 0, readY = 0, readZ = 0;

            myI2C.Config = new I2CDevice.Configuration(0x69, 400); //Set i2c device configuration

            readX = ReadGyroAxis(GYRO_XOUT_H, GYRO_XOUT_L, readBuffer, val);
            readY = ReadGyroAxis(GYRO_YOUT_H, GYRO_YOUT_L, readBuffer, val);
            readZ = ReadGyroAxis(GYRO_ZOUT_H, GYRO_ZOUT_L, readBuffer, val);

            return readX;
        }

        private static int ReadGyroAxis(byte[] GYRO_XOUT_H, byte[] GYRO_XOUT_L, byte[] readBuffer, int val)
        {
            myI2C.Execute(new I2CDevice.I2CTransaction[] {
                    I2CDevice.CreateWriteTransaction(GYRO_XOUT_H)
                    ,
                I2CDevice.CreateReadTransaction(readBuffer)
                }, 5000);

            val = (sbyte)readBuffer[0] << 8; //bit shift first resiter value

            myI2C.Execute(new I2CDevice.I2CTransaction[] {
                    I2CDevice.CreateWriteTransaction(GYRO_XOUT_L)
                    ,
                I2CDevice.CreateReadTransaction(readBuffer)
                }, 5000);

            val = val | readBuffer[0]; //combined with second register value

            return (int) (val / 14.735); //apply scaling and return
        }
    }

}

Jack




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.