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:
Thu Jul 05 17:47:12 2012 +0000
Revision:
5:d2f90c0be13f
Parent:
4:6e931d9bbf4b
Child:
6:10aa804dee97
Rev 6

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 0:eb135a8de811 23 #define OUT_X 0x00 // [6:0] are Read Only 6-bit output value X (XOUT[5] is 0 if the g direction is positive, 1 is negative)
edodm85 0:eb135a8de811 24 #define OUT_Y 0x01 // [6:0] are Read Only 6-bit output value Y (YOUT[5] is 0 if the g direction is positive, 1 is negative)
edodm85 0:eb135a8de811 25 #define OUT_Z 0x02 // [6:0] are Read Only 6-bit output value Z (ZOUT[5] is 0 if the g direction is positive, 1 is negative)
edodm85 0:eb135a8de811 26 #define TILT_STATUS 0x03 // Tilt Status (Read only)
edodm85 0:eb135a8de811 27 #define SRST_STATUS 0x04 // Sample Rate Status Register (Read only)
edodm85 0:eb135a8de811 28 #define SPCNT_STATUS 0x05 // Sleep Count Register (Read/Write)
edodm85 0:eb135a8de811 29 #define INTSU_STATUS 0x06 // Interrupt Setup Register
edodm85 0:eb135a8de811 30 #define MODE_STATUS 0x07 // Mode Register (Read/Write)
edodm85 0:eb135a8de811 31 #define SR_STATUS 0x08 // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
edodm85 0:eb135a8de811 32 #define PDET_STATUS 0x09 // Tap/Pulse Detection Register (Read/Write)
edodm85 0:eb135a8de811 33 #define PD_STATUS 0xA // Tap/Pulse Debounce Count Register (Read/Write)
edodm85 0:eb135a8de811 34
edodm85 0:eb135a8de811 35
edodm85 4:6e931d9bbf4b 36 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 4:6e931d9bbf4b 37 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};
edodm85 4:6e931d9bbf4b 38
edodm85 4:6e931d9bbf4b 39
edodm85 0:eb135a8de811 40
edodm85 0:eb135a8de811 41 // Connect module at I2C address addr using I2C port pins sda and scl
edodm85 0:eb135a8de811 42 MMA7660FC::MMA7660FC(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
edodm85 0:eb135a8de811 43 {
edodm85 0:eb135a8de811 44
edodm85 0:eb135a8de811 45 }
edodm85 0:eb135a8de811 46
edodm85 0:eb135a8de811 47
edodm85 0:eb135a8de811 48 // Destroys instance
edodm85 0:eb135a8de811 49 MMA7660FC::~MMA7660FC()
edodm85 0:eb135a8de811 50 {
edodm85 0:eb135a8de811 51
edodm85 0:eb135a8de811 52 }
edodm85 0:eb135a8de811 53
edodm85 0:eb135a8de811 54
edodm85 0:eb135a8de811 55 // Device initialization
edodm85 0:eb135a8de811 56 void MMA7660FC::init()
edodm85 0:eb135a8de811 57 {
edodm85 0:eb135a8de811 58
edodm85 0:eb135a8de811 59 write_reg(INTSU_STATUS, 0x10); // automatic interrupt after every measurement
edodm85 0:eb135a8de811 60 write_reg(SR_STATUS, 0x00); // 120 Samples/Second
edodm85 0:eb135a8de811 61 write_reg(MODE_STATUS, 0x01); // Active Mode
edodm85 0:eb135a8de811 62
edodm85 0:eb135a8de811 63 }
edodm85 0:eb135a8de811 64
edodm85 0:eb135a8de811 65
edodm85 4:6e931d9bbf4b 66 // Reads the tilt angle
edodm85 4:6e931d9bbf4b 67 void MMA7660FC::read_Tilt(float *x, float *y, float *z)
edodm85 0:eb135a8de811 68 {
edodm85 0:eb135a8de811 69
edodm85 0:eb135a8de811 70 const char Addr_X = OUT_X;
edodm85 0:eb135a8de811 71 char buf[3] = {0,0,0};
edodm85 0:eb135a8de811 72
edodm85 0:eb135a8de811 73 m_i2c.write(m_addr, &Addr_X, 1); // Pointer to the OUT_X register
edodm85 0:eb135a8de811 74 m_i2c.read(m_addr, buf, 3); // Read register content into buffer with 6bit
edodm85 0:eb135a8de811 75
edodm85 4:6e931d9bbf4b 76 // returns the x, y, z coordinates transformed into full degrees
edodm85 5:d2f90c0be13f 77 *x = TILT_XY[(int)buf[0]];
edodm85 5:d2f90c0be13f 78 *y = TILT_XY[(int)buf[1]];
edodm85 5:d2f90c0be13f 79 *z = TILT_Z[(int)buf[2]];
edodm85 0:eb135a8de811 80
edodm85 0:eb135a8de811 81 }
edodm85 0:eb135a8de811 82
edodm85 0:eb135a8de811 83
edodm85 3:df25c72e16be 84 // Reads x data
edodm85 5:d2f90c0be13f 85 int MMA7660FC::read_x()
edodm85 3:df25c72e16be 86 {
edodm85 3:df25c72e16be 87
edodm85 3:df25c72e16be 88 m_i2c.start(); // Start
edodm85 3:df25c72e16be 89 m_i2c.write(0x98); // A write to device 0x98
edodm85 3:df25c72e16be 90 m_i2c.write(OUT_X); // Register to read
edodm85 3:df25c72e16be 91 m_i2c.start();
edodm85 3:df25c72e16be 92 m_i2c.write(0x99); // Read from device 0x99
edodm85 3:df25c72e16be 93 char x = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 94 m_i2c.stop();
edodm85 3:df25c72e16be 95
edodm85 5:d2f90c0be13f 96 return (int)x;
edodm85 3:df25c72e16be 97
edodm85 3:df25c72e16be 98 }
edodm85 3:df25c72e16be 99
edodm85 3:df25c72e16be 100
edodm85 3:df25c72e16be 101 // Reads y data
edodm85 5:d2f90c0be13f 102 int MMA7660FC::read_y()
edodm85 3:df25c72e16be 103 {
edodm85 3:df25c72e16be 104
edodm85 3:df25c72e16be 105 m_i2c.start(); // Start
edodm85 3:df25c72e16be 106 m_i2c.write(0x98); // A write to device 0x98
edodm85 3:df25c72e16be 107 m_i2c.write(OUT_Y); // Register to read
edodm85 3:df25c72e16be 108 m_i2c.start();
edodm85 3:df25c72e16be 109 m_i2c.write(0x99); // Read from device 0x99
edodm85 3:df25c72e16be 110 char y = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 111 m_i2c.stop();
edodm85 3:df25c72e16be 112
edodm85 5:d2f90c0be13f 113 return (int)y;
edodm85 3:df25c72e16be 114
edodm85 3:df25c72e16be 115 }
edodm85 3:df25c72e16be 116
edodm85 3:df25c72e16be 117
edodm85 3:df25c72e16be 118 // Reads z data
edodm85 5:d2f90c0be13f 119 int MMA7660FC::read_z()
edodm85 3:df25c72e16be 120 {
edodm85 3:df25c72e16be 121
edodm85 3:df25c72e16be 122 m_i2c.start(); // Start
edodm85 3:df25c72e16be 123 m_i2c.write(0x98); // A write to device 0x98
edodm85 3:df25c72e16be 124 m_i2c.write(OUT_Z); // Register to read
edodm85 3:df25c72e16be 125 m_i2c.start();
edodm85 3:df25c72e16be 126 m_i2c.write(0x99); // Read from device 0x99
edodm85 3:df25c72e16be 127 char z = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 128 m_i2c.stop();
edodm85 3:df25c72e16be 129
edodm85 5:d2f90c0be13f 130 return (int)z;
edodm85 3:df25c72e16be 131
edodm85 3:df25c72e16be 132 }
edodm85 3:df25c72e16be 133
edodm85 3:df25c72e16be 134
edodm85 0:eb135a8de811 135 // Read from specified MMA7660FC register
edodm85 0:eb135a8de811 136 char MMA7660FC::read_reg(char addr)
edodm85 0:eb135a8de811 137 {
edodm85 0:eb135a8de811 138
edodm85 0:eb135a8de811 139 m_i2c.start(); // Start
edodm85 0:eb135a8de811 140 m_i2c.write(0x98); // A write to device 0x98
edodm85 0:eb135a8de811 141 m_i2c.write(addr); // Register to read
edodm85 3:df25c72e16be 142 m_i2c.start();
edodm85 0:eb135a8de811 143 m_i2c.write(0x99); // Read from device 0x99
edodm85 0:eb135a8de811 144 char c = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 145 m_i2c.stop();
edodm85 0:eb135a8de811 146
edodm85 0:eb135a8de811 147 return c;
edodm85 0:eb135a8de811 148
edodm85 0:eb135a8de811 149 }
edodm85 0:eb135a8de811 150
edodm85 0:eb135a8de811 151
edodm85 0:eb135a8de811 152 // Write register (The device must be placed in Standby Mode to change the value of the registers)
edodm85 0:eb135a8de811 153 void MMA7660FC::write_reg(char addr, char data)
edodm85 0:eb135a8de811 154 {
edodm85 0:eb135a8de811 155
edodm85 0:eb135a8de811 156 char cmd[2] = {0, 0};
edodm85 0:eb135a8de811 157
edodm85 0:eb135a8de811 158 cmd[0] = MODE_STATUS;
edodm85 0:eb135a8de811 159 cmd[1] = 0x00; // Standby Mode on
edodm85 0:eb135a8de811 160 m_i2c.write(m_addr, cmd, 2);
edodm85 0:eb135a8de811 161
edodm85 0:eb135a8de811 162 cmd[0] = addr;
edodm85 0:eb135a8de811 163 cmd[1] = data; // New value of the register
edodm85 0:eb135a8de811 164 m_i2c.write(m_addr, cmd, 2);
edodm85 0:eb135a8de811 165
edodm85 0:eb135a8de811 166 cmd[0] = MODE_STATUS;
edodm85 0:eb135a8de811 167 cmd[1] = 0x01; // Active Mode on
edodm85 0:eb135a8de811 168 m_i2c.write(m_addr, cmd, 2);
edodm85 0:eb135a8de811 169
edodm85 0:eb135a8de811 170 }