Saturday, 14 June 2014

ITG-3200 and FEZmini via I2C

posted 9 Feb 2011 14:47 by David Taylor

Following the success of getting my Accelorometer to talk to FEZ via I2C I tackled the ITG-3200 tripple axis Gyroscope tonight.
The ITG-3200 is slightly easier to wire up since the address selector pin is wired high by default. The only link you have to solder in is to attach VIO and VDD together.
Microsoft C# .Net
1:  using System;
2:  using Microsoft.SPOT;
3:  using Microsoft.SPOT.Hardware;
4:  using System.Threading;
5:  
6:  namespace Tri_Rot_Version_1
7:  {
8:      class ITG3200I2C
9:      {
10:         private I2CDevice ITG3200;
11:         private I2CDevice.I2CTransaction[] xActions;
12: 
13:         public ITG3200I2C()
14:         {
15:             int x;
16:             int y;
17:             int z;
18:             int t;
19: 
20:             Debug.Print("ITG-3200 Example");
21: 
22:             InitialiseITG3200();
23: 
24:             while (true)
25:             {
26:                 x = ReadITG3200(0x1D);
27:                 y = ReadITG3200(0x1F);
28:                 z = ReadITG3200(0x21);
29:                 t = ReadITG3200(0x1B);
30: 
31:                 Debug.Print("x=" + x.ToString() + ", y=" + y.ToString() + ", z=" + z.ToString() + ", t=" + t.ToString());
32:                 Thread.Sleep(100);
33:             }
34:         }
35:
36:        private int ReadITG3200(byte RegisterAddress)
37:        {
38:            xActions = new I2CDevice.I2CTransaction[2];
39:            byte[] SendBytes = new byte[1] { RegisterAddress };
40:            xActions[0] = I2CDevice.CreateWriteTransaction(SendBytes);
41:            byte[] ReceiveBytes = new byte[2];
42:            xActions[1] = I2CDevice.CreateReadTransaction(ReceiveBytes);
43:            int response = ITG3200.Execute(xActions, 1000);
44:
45:            return (ReceiveBytes[1] << 8) | ReceiveBytes[0];
46:        }
47:
48:        private void InitialiseITG3200()
49:        {
50:            I2CDevice.Configuration ITG3200Config = new I2CDevice.Configuration(0x69, 400);
51:            ITG3200 = new I2CDevice(ITG3200Config);
52:
53:            xActions = new I2CDevice.I2CTransaction[1];
54:            byte[] SendByte = new byte[2] { 0x3E, 1 << 7 }; //Reg:[PWR_MGM] Bit7 = 1 - Hardware Reset
55:            xActions[0] = I2CDevice.CreateWriteTransaction(SendByte);
56:            int bytesTransfered = ITG3200.Execute(xActions, 1000);
57:            Thread.Sleep(100);
58:            SendByte = new byte[2] { 0x16, 0x18 }; //Reg:[DLPF_FS] +-2000Deg/Sec at 8KHz with 256Hz LPF
59:            xActions[0] = I2CDevice.CreateWriteTransaction(SendByte);
60:            bytesTransfered = ITG3200.Execute(xActions, 1000);
61:            Thread.Sleep(100);
62:            SendByte = new byte[2] { 0x3E, 1 }; //Reg:[PWR_MGM] PLL with X Gyro reference - All other bits = 0
63:            xActions[0] = I2CDevice.CreateWriteTransaction(SendByte);
64:            bytesTransfered = ITG3200.Execute(xActions, 1000);
65:        }
66:    }
67:}
and that's it.
The next thing I will be tackling is to make sense of all this data that is coming back from the Accelorometers and Gyroscopes and output usefull IMU Data. So far the best option I have found is "A Guide To using IMU (Accelerometer and Gyroscope Devices) in Embedded Applications" on starlino
Wish me luck!

No comments:

Post a Comment