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:
Tue Jul 03 18:37:19 2012 +0000
Revision:
3:df25c72e16be
Parent:
0:eb135a8de811
Child:
4:6e931d9bbf4b
Rev 4

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 0:eb135a8de811 36
edodm85 0:eb135a8de811 37 // Connect module at I2C address addr using I2C port pins sda and scl
edodm85 0:eb135a8de811 38 MMA7660FC::MMA7660FC(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
edodm85 0:eb135a8de811 39 {
edodm85 0:eb135a8de811 40
edodm85 0:eb135a8de811 41 }
edodm85 0:eb135a8de811 42
edodm85 0:eb135a8de811 43
edodm85 0:eb135a8de811 44 // Destroys instance
edodm85 0:eb135a8de811 45 MMA7660FC::~MMA7660FC()
edodm85 0:eb135a8de811 46 {
edodm85 0:eb135a8de811 47
edodm85 0:eb135a8de811 48 }
edodm85 0:eb135a8de811 49
edodm85 0:eb135a8de811 50
edodm85 0:eb135a8de811 51 // Device initialization
edodm85 0:eb135a8de811 52 void MMA7660FC::init()
edodm85 0:eb135a8de811 53 {
edodm85 0:eb135a8de811 54
edodm85 0:eb135a8de811 55 write_reg(INTSU_STATUS, 0x10); // automatic interrupt after every measurement
edodm85 0:eb135a8de811 56 write_reg(SR_STATUS, 0x00); // 120 Samples/Second
edodm85 0:eb135a8de811 57 write_reg(MODE_STATUS, 0x01); // Active Mode
edodm85 0:eb135a8de811 58
edodm85 0:eb135a8de811 59 }
edodm85 0:eb135a8de811 60
edodm85 0:eb135a8de811 61
edodm85 0:eb135a8de811 62 // Reads X, Y, Z data
edodm85 0:eb135a8de811 63 void MMA7660FC::read_g(int *x, int *y, int *z)
edodm85 0:eb135a8de811 64 {
edodm85 0:eb135a8de811 65
edodm85 0:eb135a8de811 66 const char Addr_X = OUT_X;
edodm85 0:eb135a8de811 67 char buf[3] = {0,0,0};
edodm85 0:eb135a8de811 68
edodm85 0:eb135a8de811 69 m_i2c.write(m_addr, &Addr_X, 1); // Pointer to the OUT_X register
edodm85 0:eb135a8de811 70 m_i2c.read(m_addr, buf, 3); // Read register content into buffer with 6bit
edodm85 0:eb135a8de811 71
edodm85 0:eb135a8de811 72 *x = buf[0];
edodm85 0:eb135a8de811 73 *y = buf[1];
edodm85 0:eb135a8de811 74 *z = buf[2];
edodm85 0:eb135a8de811 75
edodm85 0:eb135a8de811 76 }
edodm85 0:eb135a8de811 77
edodm85 0:eb135a8de811 78
edodm85 3:df25c72e16be 79 // Reads x data
edodm85 3:df25c72e16be 80 float MMA7660FC::read_x()
edodm85 3:df25c72e16be 81 {
edodm85 3:df25c72e16be 82
edodm85 3:df25c72e16be 83 m_i2c.start(); // Start
edodm85 3:df25c72e16be 84 m_i2c.write(0x98); // A write to device 0x98
edodm85 3:df25c72e16be 85 m_i2c.write(OUT_X); // Register to read
edodm85 3:df25c72e16be 86 m_i2c.start();
edodm85 3:df25c72e16be 87 m_i2c.write(0x99); // Read from device 0x99
edodm85 3:df25c72e16be 88 char x = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 89 m_i2c.stop();
edodm85 3:df25c72e16be 90
edodm85 3:df25c72e16be 91 return (float)x;
edodm85 3:df25c72e16be 92
edodm85 3:df25c72e16be 93 }
edodm85 3:df25c72e16be 94
edodm85 3:df25c72e16be 95
edodm85 3:df25c72e16be 96 // Reads y data
edodm85 3:df25c72e16be 97 float MMA7660FC::read_y()
edodm85 3:df25c72e16be 98 {
edodm85 3:df25c72e16be 99
edodm85 3:df25c72e16be 100 m_i2c.start(); // Start
edodm85 3:df25c72e16be 101 m_i2c.write(0x98); // A write to device 0x98
edodm85 3:df25c72e16be 102 m_i2c.write(OUT_Y); // Register to read
edodm85 3:df25c72e16be 103 m_i2c.start();
edodm85 3:df25c72e16be 104 m_i2c.write(0x99); // Read from device 0x99
edodm85 3:df25c72e16be 105 char y = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 106 m_i2c.stop();
edodm85 3:df25c72e16be 107
edodm85 3:df25c72e16be 108 return (float)y;
edodm85 3:df25c72e16be 109
edodm85 3:df25c72e16be 110 }
edodm85 3:df25c72e16be 111
edodm85 3:df25c72e16be 112
edodm85 3:df25c72e16be 113 // Reads z data
edodm85 3:df25c72e16be 114 float MMA7660FC::read_z()
edodm85 3:df25c72e16be 115 {
edodm85 3:df25c72e16be 116
edodm85 3:df25c72e16be 117 m_i2c.start(); // Start
edodm85 3:df25c72e16be 118 m_i2c.write(0x98); // A write to device 0x98
edodm85 3:df25c72e16be 119 m_i2c.write(OUT_Z); // Register to read
edodm85 3:df25c72e16be 120 m_i2c.start();
edodm85 3:df25c72e16be 121 m_i2c.write(0x99); // Read from device 0x99
edodm85 3:df25c72e16be 122 char z = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 123 m_i2c.stop();
edodm85 3:df25c72e16be 124
edodm85 3:df25c72e16be 125 return (float)z;
edodm85 3:df25c72e16be 126
edodm85 3:df25c72e16be 127 }
edodm85 3:df25c72e16be 128
edodm85 3:df25c72e16be 129
edodm85 0:eb135a8de811 130 // Read from specified MMA7660FC register
edodm85 0:eb135a8de811 131 char MMA7660FC::read_reg(char addr)
edodm85 0:eb135a8de811 132 {
edodm85 0:eb135a8de811 133
edodm85 0:eb135a8de811 134 m_i2c.start(); // Start
edodm85 0:eb135a8de811 135 m_i2c.write(0x98); // A write to device 0x98
edodm85 0:eb135a8de811 136 m_i2c.write(addr); // Register to read
edodm85 3:df25c72e16be 137 m_i2c.start();
edodm85 0:eb135a8de811 138 m_i2c.write(0x99); // Read from device 0x99
edodm85 0:eb135a8de811 139 char c = m_i2c.read(0); // Read the data
edodm85 3:df25c72e16be 140 m_i2c.stop();
edodm85 0:eb135a8de811 141
edodm85 0:eb135a8de811 142 return c;
edodm85 0:eb135a8de811 143
edodm85 0:eb135a8de811 144 }
edodm85 0:eb135a8de811 145
edodm85 0:eb135a8de811 146
edodm85 0:eb135a8de811 147 // Write register (The device must be placed in Standby Mode to change the value of the registers)
edodm85 0:eb135a8de811 148 void MMA7660FC::write_reg(char addr, char data)
edodm85 0:eb135a8de811 149 {
edodm85 0:eb135a8de811 150
edodm85 0:eb135a8de811 151 char cmd[2] = {0, 0};
edodm85 0:eb135a8de811 152
edodm85 0:eb135a8de811 153 cmd[0] = MODE_STATUS;
edodm85 0:eb135a8de811 154 cmd[1] = 0x00; // Standby Mode on
edodm85 0:eb135a8de811 155 m_i2c.write(m_addr, cmd, 2);
edodm85 0:eb135a8de811 156
edodm85 0:eb135a8de811 157 cmd[0] = addr;
edodm85 0:eb135a8de811 158 cmd[1] = data; // New value of the register
edodm85 0:eb135a8de811 159 m_i2c.write(m_addr, cmd, 2);
edodm85 0:eb135a8de811 160
edodm85 0:eb135a8de811 161 cmd[0] = MODE_STATUS;
edodm85 0:eb135a8de811 162 cmd[1] = 0x01; // Active Mode on
edodm85 0:eb135a8de811 163 m_i2c.write(m_addr, cmd, 2);
edodm85 0:eb135a8de811 164
edodm85 0:eb135a8de811 165 }