3軸加速度センサモジュール ADXL345(SPI/IIC)を使ってみよう

3軸加速度センサモジュール

3軸加速度センサモジュール ADXL345(SPI/IIC)を使ってみます。


SDA signal は cerb40の pin18
SCL signal は cerb40の pin19
Vs=Vdd として3.3Vの電源に繋ぎます
このようにするとI2Cのアドレスは0x1Dになります。

Program.cs
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.OSHW.Hardware;
using GHI.Hardware.FEZCerb;
using BoeBotLib;
using Pin = GHI.Hardware.FEZCerb.Pin;

namespace BoeBotLib
{
    public class Program
    {
        public static void Main()
        {
            new Program().main();
        }
        private void main()
        {
            double x, y, z;
            ADXL345_I2C accel=null;
            try
            {
                accel = new ADXL345_I2C();
                while(true)
                {
                    accel.Read(out x, out y, out z);
                    String sx = x.ToString()+"    ";
                    sx = sx.Substring(0, 5);
                    String sy = y.ToString() + "    ";
                    sy = sy.Substring(0, 5);
                    String sz = z.ToString() + "    ";
                    sz = sz.Substring(0, 5);
                    Debug.Print("x=" + sx + " y=" + sy + " z=" + sz);
                    CPU.delay(100);
                }
            }
            catch (Exception e) { Debug.Print("ERROR "+e.ToString()); return; }
        }
    }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 


ADXL345_I2C



ADXL345_I2C.cs
using System;
using System.Threading;  
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware; 
using GHI.OSHW.Hardware;        
using GHI.Hardware.FEZCerb;
using BoeBotLib;
using Pin = GHI.Hardware.FEZCerb.Pin;

class ADXL345_I2C
{
    // 3軸加速度センサモジュール ADXL345(SPI/IIC)
    // http://akizukidenshi.com/catalog/g/gM-06724/
    //SDA signal to pin18, SCL signal to pin19
    //With Vs=Vdd
    // -> I2C address = 0x1D

    ushort I2C_ADR;
    int CLOCK_SPEED;
    I2CDevice i2c;

    public ADXL345_I2C() : this(400) { }
    public ADXL345_I2C(int ClockSpeed)
    {
        I2C_ADR = 0x1D;
        CLOCK_SPEED = ClockSpeed;

        //create I2C object
        I2CDevice.Configuration config = new I2CDevice.Configuration(I2C_ADR, CLOCK_SPEED);
        i2c = new I2CDevice(config);
        Measure();
    }

    public void Measure()
    {
        WriteReg(0x2D, 0x08);
    }
    public void Read(out double x, out double y, out double z)
    {
        //read from device
        I2CDevice.I2CTransaction[] xActions;
        byte regAddress = 0x32; //first axis-acceleration-data register on the ADXL345

        //read the acceleration data from the ADXL345
        byte[] readBuf = new byte[6];//buffer for reading 6 bytes
        xActions = new I2CDevice.I2CTransaction[2];
        xActions[0] = I2CDevice.CreateWriteTransaction(new byte[1] { regAddress });
        xActions[1] = I2CDevice.CreateReadTransaction(readBuf);
        int n = 0;
        if ((n=i2c.Execute(xActions, 1000)) == 0)
            throw new Exception("Failed to communicate on I2C");
        ushort tmp = readBuf[1];  tmp <<= 8; tmp |= readBuf[0];
        x = (short)tmp / 256.0;
        tmp = readBuf[3]; tmp <<= 8; tmp |= readBuf[2];
        y = (short)tmp / 256.0;
        tmp = readBuf[5]; tmp <<= 8; tmp |= readBuf[4];
        z = (short)tmp / 256.0;
    }
    public byte ReadDeviceID()
    {
        return ReadReg(0);
    }
    public void SelfTest(bool sw)
    {
        WriteReg(0x31, (byte)(sw ? 0x80 : 0));
    }
    public void WriteReg(byte regAddress, byte data)
    {
        I2CDevice.I2CTransaction[] xActions;
        xActions = new I2CDevice.I2CTransaction[1];
        xActions[0] = I2CDevice.CreateWriteTransaction(new byte[2] { regAddress, data });
        if ((i2c.Execute(xActions, 1000)) == 0)
            throw new Exception("Failed to communicate on I2C");
    }
    public byte ReadReg(byte reg)
    {
        I2CDevice.I2CTransaction[] xActions;
        byte[] readBuf = new byte[1];
        xActions = new I2CDevice.I2CTransaction[2];
        xActions[0] = I2CDevice.CreateWriteTransaction(new byte[1] { reg });
        xActions[1] = I2CDevice.CreateReadTransaction(readBuf);
        if ((i2c.Execute(xActions, 1000)) == 0)
            throw new Exception("Failed to communicate on I2C");
        return readBuf[0];
    }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86