Library for MMA7660FC Accelerometer device

Dependents:   TestCode_MMA7660FC 3D_Accelerometer_Tester RTOS-aap-board-modules embed_Grove_3-Axis_Digital_Accelerometer ... more

Committer:
edodm85
Date:
Wed Aug 07 19:50:09 2013 +0000
Revision:
8:122af194c74b
Parent:
7:74eb2a4803ba
Child:
9:2cc4a317402d
Added the functions read_Side() and read_Orientation()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edodm85 0:eb135a8de811 1 // Author: Edoardo De Marchi
edodm85 0:eb135a8de811 2 /* Copyright (C) 2012 mbed.org, MIT License
edodm85 0:eb135a8de811 3 *
edodm85 0:eb135a8de811 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
edodm85 0:eb135a8de811 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
edodm85 0:eb135a8de811 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
edodm85 0:eb135a8de811 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
edodm85 0:eb135a8de811 8 * furnished to do so, subject to the following conditions:
edodm85 0:eb135a8de811 9 *
edodm85 0:eb135a8de811 10 * The above copyright notice and this permission notice shall be included in all copies or
edodm85 0:eb135a8de811 11 * substantial portions of the Software.
edodm85 0:eb135a8de811 12 *
edodm85 0:eb135a8de811 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
edodm85 0:eb135a8de811 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
edodm85 0:eb135a8de811 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
edodm85 0:eb135a8de811 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
edodm85 0:eb135a8de811 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
edodm85 0:eb135a8de811 18 */
edodm85 0:eb135a8de811 19
edodm85 0:eb135a8de811 20
edodm85 0:eb135a8de811 21 #include "MMA7660FC.h"
edodm85 0:eb135a8de811 22
edodm85 4:6e931d9bbf4b 23 float TILT_XY[64] = {0, 2.69, 5.38, 8.08, 10.81, 13.55, 16.33, 19.16, 22.02, 24.95, 27.95, 31.04, 34.23, 37.54, 41.01, 44.68, 48.59, 52.83, 57.54, 62.95, 69.64, 79.86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -79.86, -69.64, -62.95, -57.54, -52.83, -48.59, -44.68, -41.01, -37.54, -34.23, -31.04, -27.95, -24.95, -22.02, -19.16, -16.33, -13.55, -10.81, -8.08, -5.38, -2.69};
edodm85 7:74eb2a4803ba 24 float TILT_Z[64] = {90.00, 87.31, 84.62, 81.92, 79.19, 76.45, 73.67, 70.84, 67.98, 65.05, 62.05, 58.96, 55.77, 52.46, 48.99, 45.32, 41.41, 37.17, 32.46, 27.05, 20.36, 10.14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -10.14, -20.36, -27.05, -32.46, -37.17, -41.41, -45.32, -48.99, -52.46, -55.77, -58.96, -62.05, -65.05, -67.98, -70.84, -73.67, -76.45, -79.19, -81.92, -84.62, -87.31};
edodm85 4:6e931d9bbf4b 25
edodm85 4:6e931d9bbf4b 26
edodm85 0:eb135a8de811 27
edodm85 0:eb135a8de811 28 // Connect module at I2C address addr using I2C port pins sda and scl
edodm85 7:74eb2a4803ba 29 MMA7660FC::MMA7660FC(PinName sda, PinName scl, int addr) : m_i2c(sda, scl)
edodm85 0:eb135a8de811 30 {
edodm85 8:122af194c74b 31 SPI_W_Address = addr; // Write address
edodm85 8:122af194c74b 32 SPI_R_Address = SPI_W_Address | 1; // Read address
edodm85 0:eb135a8de811 33 }
edodm85 0:eb135a8de811 34
edodm85 0:eb135a8de811 35
edodm85 0:eb135a8de811 36 // Destroys instance
edodm85 0:eb135a8de811 37 MMA7660FC::~MMA7660FC()
edodm85 0:eb135a8de811 38 {
edodm85 0:eb135a8de811 39 }
edodm85 0:eb135a8de811 40
edodm85 0:eb135a8de811 41
edodm85 0:eb135a8de811 42 // Device initialization
edodm85 0:eb135a8de811 43 void MMA7660FC::init()
edodm85 8:122af194c74b 44 {
edodm85 0:eb135a8de811 45 write_reg(INTSU_STATUS, 0x10); // automatic interrupt after every measurement
edodm85 8:122af194c74b 46 write_reg(SR_STATUS, 0x07); // 1 Samples/Second
edodm85 0:eb135a8de811 47 }
edodm85 0:eb135a8de811 48
edodm85 0:eb135a8de811 49
edodm85 4:6e931d9bbf4b 50 // Reads the tilt angle
edodm85 4:6e931d9bbf4b 51 void MMA7660FC::read_Tilt(float *x, float *y, float *z)
edodm85 0:eb135a8de811 52 {
edodm85 0:eb135a8de811 53 const char Addr_X = OUT_X;
edodm85 0:eb135a8de811 54 char buf[3] = {0,0,0};
edodm85 0:eb135a8de811 55
edodm85 6:10aa804dee97 56 m_i2c.write(SPI_W_Address, &Addr_X, 1); // Pointer to the OUT_X register
edodm85 6:10aa804dee97 57 m_i2c.read(SPI_W_Address, buf, 3); // Read register content into buffer with 6bit
edodm85 0:eb135a8de811 58
edodm85 4:6e931d9bbf4b 59 // returns the x, y, z coordinates transformed into full degrees
edodm85 5:d2f90c0be13f 60 *x = TILT_XY[(int)buf[0]];
edodm85 5:d2f90c0be13f 61 *y = TILT_XY[(int)buf[1]];
edodm85 8:122af194c74b 62 *z = TILT_Z[(int)buf[2]];
edodm85 0:eb135a8de811 63 }
edodm85 0:eb135a8de811 64
edodm85 0:eb135a8de811 65
edodm85 3:df25c72e16be 66 // Reads x data
edodm85 5:d2f90c0be13f 67 int MMA7660FC::read_x()
edodm85 3:df25c72e16be 68 {
edodm85 3:df25c72e16be 69 m_i2c.start(); // Start
edodm85 6:10aa804dee97 70 m_i2c.write(SPI_W_Address); // A write to device
edodm85 3:df25c72e16be 71 m_i2c.write(OUT_X); // Register to read
edodm85 3:df25c72e16be 72 m_i2c.start();
edodm85 6:10aa804dee97 73 m_i2c.write(SPI_R_Address); // Read from device
edodm85 3:df25c72e16be 74 char x = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 75 m_i2c.stop();
edodm85 3:df25c72e16be 76
edodm85 5:d2f90c0be13f 77 return (int)x;
edodm85 3:df25c72e16be 78 }
edodm85 3:df25c72e16be 79
edodm85 3:df25c72e16be 80
edodm85 3:df25c72e16be 81 // Reads y data
edodm85 5:d2f90c0be13f 82 int MMA7660FC::read_y()
edodm85 3:df25c72e16be 83 {
edodm85 3:df25c72e16be 84 m_i2c.start(); // Start
edodm85 6:10aa804dee97 85 m_i2c.write(SPI_W_Address); // A write to device
edodm85 3:df25c72e16be 86 m_i2c.write(OUT_Y); // Register to read
edodm85 3:df25c72e16be 87 m_i2c.start();
edodm85 6:10aa804dee97 88 m_i2c.write(SPI_R_Address); // Read from device
edodm85 3:df25c72e16be 89 char y = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 90 m_i2c.stop();
edodm85 3:df25c72e16be 91
edodm85 5:d2f90c0be13f 92 return (int)y;
edodm85 3:df25c72e16be 93 }
edodm85 3:df25c72e16be 94
edodm85 3:df25c72e16be 95
edodm85 3:df25c72e16be 96 // Reads z data
edodm85 5:d2f90c0be13f 97 int MMA7660FC::read_z()
edodm85 3:df25c72e16be 98 {
edodm85 3:df25c72e16be 99 m_i2c.start(); // Start
edodm85 6:10aa804dee97 100 m_i2c.write(SPI_W_Address); // A write to device
edodm85 3:df25c72e16be 101 m_i2c.write(OUT_Z); // Register to read
edodm85 3:df25c72e16be 102 m_i2c.start();
edodm85 6:10aa804dee97 103 m_i2c.write(SPI_R_Address); // Read from device
edodm85 3:df25c72e16be 104 char z = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 105 m_i2c.stop();
edodm85 3:df25c72e16be 106
edodm85 5:d2f90c0be13f 107 return (int)z;
edodm85 8:122af194c74b 108 }
edodm85 3:df25c72e16be 109
edodm85 8:122af194c74b 110 // Reads MMA7660FC Orientation
edodm85 8:122af194c74b 111 char const* MMA7660FC::read_Side()
edodm85 8:122af194c74b 112 {
edodm85 8:122af194c74b 113 char tiltStatus = read_reg(TILT_STATUS);
edodm85 8:122af194c74b 114
edodm85 8:122af194c74b 115 if((tiltStatus & 0x03) == 0x01)
edodm85 8:122af194c74b 116 return "Front";
edodm85 8:122af194c74b 117 if((tiltStatus & 0x03) == 0x02)
edodm85 8:122af194c74b 118 return "Back";
edodm85 8:122af194c74b 119
edodm85 8:122af194c74b 120 return "Unknown";
edodm85 3:df25c72e16be 121 }
edodm85 3:df25c72e16be 122
edodm85 3:df25c72e16be 123
edodm85 8:122af194c74b 124 // Reads MMA7660FC Orientation
edodm85 8:122af194c74b 125 char const* MMA7660FC::read_Orientation()
edodm85 8:122af194c74b 126 {
edodm85 8:122af194c74b 127 char tiltStatus = read_reg(TILT_STATUS);
edodm85 8:122af194c74b 128
edodm85 8:122af194c74b 129 if((tiltStatus & 0x1c) == 0x04)
edodm85 8:122af194c74b 130 return "Left";
edodm85 8:122af194c74b 131 if((tiltStatus & 0x1c) == 0x08)
edodm85 8:122af194c74b 132 return "Right";
edodm85 8:122af194c74b 133 if((tiltStatus & 0x1c) == 0x14)
edodm85 8:122af194c74b 134 return "Down";
edodm85 8:122af194c74b 135 if((tiltStatus & 0x1c) == 0x18)
edodm85 8:122af194c74b 136 return "Up";
edodm85 8:122af194c74b 137
edodm85 8:122af194c74b 138 return "Unknown";
edodm85 8:122af194c74b 139 }
edodm85 8:122af194c74b 140
edodm85 8:122af194c74b 141
edodm85 8:122af194c74b 142
edodm85 0:eb135a8de811 143 // Read from specified MMA7660FC register
edodm85 0:eb135a8de811 144 char MMA7660FC::read_reg(char addr)
edodm85 8:122af194c74b 145 {
edodm85 0:eb135a8de811 146 m_i2c.start(); // Start
edodm85 6:10aa804dee97 147 m_i2c.write(SPI_W_Address); // A write to device
edodm85 0:eb135a8de811 148 m_i2c.write(addr); // Register to read
edodm85 3:df25c72e16be 149 m_i2c.start();
edodm85 6:10aa804dee97 150 m_i2c.write(SPI_R_Address); // Read from device
edodm85 0:eb135a8de811 151 char c = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 152 m_i2c.stop();
edodm85 0:eb135a8de811 153
edodm85 8:122af194c74b 154 return c;
edodm85 0:eb135a8de811 155 }
edodm85 0:eb135a8de811 156
edodm85 0:eb135a8de811 157
edodm85 0:eb135a8de811 158 // Write register (The device must be placed in Standby Mode to change the value of the registers)
edodm85 0:eb135a8de811 159 void MMA7660FC::write_reg(char addr, char data)
edodm85 0:eb135a8de811 160 {
edodm85 0:eb135a8de811 161 char cmd[2] = {0, 0};
edodm85 0:eb135a8de811 162
edodm85 0:eb135a8de811 163 cmd[0] = MODE_STATUS;
edodm85 6:10aa804dee97 164 cmd[1] = 0x00; // Standby Mode on
edodm85 6:10aa804dee97 165 m_i2c.write(SPI_W_Address, cmd, 2);
edodm85 0:eb135a8de811 166
edodm85 0:eb135a8de811 167 cmd[0] = addr;
edodm85 6:10aa804dee97 168 cmd[1] = data; // New value of the register
edodm85 6:10aa804dee97 169 m_i2c.write(SPI_W_Address, cmd, 2);
edodm85 0:eb135a8de811 170
edodm85 0:eb135a8de811 171 cmd[0] = MODE_STATUS;
edodm85 6:10aa804dee97 172 cmd[1] = 0x01; // Active Mode on
edodm85 8:122af194c74b 173 m_i2c.write(SPI_W_Address, cmd, 2);
edodm85 0:eb135a8de811 174 }
edodm85 7:74eb2a4803ba 175
edodm85 7:74eb2a4803ba 176
edodm85 7:74eb2a4803ba 177 // check if the address exist on an I2C bus
edodm85 7:74eb2a4803ba 178 int MMA7660FC::check()
edodm85 8:122af194c74b 179 {
edodm85 7:74eb2a4803ba 180 return m_i2c.write(SPI_W_Address, NULL, 0);
edodm85 7:74eb2a4803ba 181 }