Saturday, 14 June 2014

ADXL345 and FEZmini via I2C

posted 7 Feb 2011 14:51 by David Taylor   [ updated 8 Feb 2011 00:57 ]

I cracket it (with some help from my friends).
The Beginners guide to NETMF.pdf (available from TinyCLR) contains some introductory code samples for I2C communication. Looking at the documentation for the ADXL345 they provide information as if you were handcrafting the full I2C communication stack (like you have to do on boards like the Arduino). The problem (in this instant) was that .Net provides amuch simplified model for I2C interface, but looking at the low level examples, I didn't know what to leave out and what to add.
First, wiring the ADXL345 Breakout board to the FEZmini could not be easier. The SDA and SCL lines on the FEZmini 1.3 board already contain all the required pull-up resistors. The 3.3V supply from the FEZmini also has more than enough spare capacity to power the ADXL345.
Once you have it all wired up, the code (once you get it all sorted out), couldn't be simpler either.
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:      public class ADXL345I2C
9:      {
10:         private I2CDevice ADXL345;
11:         private I2CDevice.I2CTransaction[] xActions;
12: 
13:         public ADXL345I2C()
14:         {
15:             int x;
16:             int y;
17:             int z;
18: 
19:             Debug.Print("ADXL345 Example");
20: 
21:             InitialiseADXL345();
22: 
23:             while (true)
24:             {
25:                 x = ReadADXL345(0x32);
26:                 y = ReadADXL345(0x34);
27:                 z = ReadADXL345(0x36);
28: 
29:                 Debug.Print("x=" + x.ToString() + ", y=" + y.ToString() + 30: ", z=" + z.ToString());
31:                 Thread.Sleep(100);
32:             }
33:         }
34: 
35:         private int ReadADXL345(byte RegisterAddress)
36:         {
37:             xActions = new I2CDevice.I2CTransaction[2];
38:             byte[] SendBytes = new byte[1] { RegisterAddress };
39:             xActions[0] = I2CDevice.CreateWriteTransaction(SendBytes);
40:             byte[] ReceiveBytes = new byte[2];
41:             xActions[1] = I2CDevice.CreateReadTransaction(ReceiveBytes);
42:             int response = ADXL345.Execute(xActions, 1000);
43: 
44:             return (ReceiveBytes[1] << 8) | ReceiveBytes[0];
45:         }
46: 
47:         private void InitialiseADXL345()
48:         {
49:             I2CDevice.Configuration ADXL345Config = new I2CDevice.Configuration(0x53, 400);
50:             ADXL345 = new I2CDevice(ADXL345Config);
51: 
52:             xActions = new I2CDevice.I2CTransaction[3];
53:             byte[] SendByte = new byte[2] { 0x2D, 0 };
54:             xActions[0] = I2CDevice.CreateWriteTransaction(SendByte);
55:             SendByte = new byte[2] { 0x2D, 1 << 4 };
56:             xActions[1] = I2CDevice.CreateWriteTransaction(SendByte);
57:             SendByte = new byte[2] { 0x2D, 1 << 3 };
58:             xActions[2] = I2CDevice.CreateWriteTransaction(SendByte);
59:             int response = ADXL345.Execute(xActions, 1000);
60:         }
61:     }
62: }
Interesting parts in the code above:
0x53 in line 49 is the alternative address for the ADXL345 since I have SDO tied to Ground. 400 is the I2C interface Speed.
0x2D in Line  53, 55 and 57 is the power register. Setting it to 0 resets the buffer. Setting it to 16 (1 << 4) set's it in Standby mode and finally 8 set's it into Measure mode.
The rest is basic C#.
Some resources I used to get to this point:
The next task is to have a chat with my ITG-3200 Triple Axis Gyroscope on the same I2C channel.
Send Comments/Feedback by logging on with any Google Acount. (Sign In link below in the footer of this page)

No comments:

Post a Comment