Library for MMA7660FC Accelerometer device
Dependents: TestCode_MMA7660FC 3D_Accelerometer_Tester RTOS-aap-board-modules embed_Grove_3-Axis_Digital_Accelerometer ... more
MMA7660FC.cpp
00001 // Author: Edoardo De Marchi 00002 /* Copyright (C) 2012 mbed.org, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 00020 00021 #include "MMA7660FC.h" 00022 00023 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}; 00024 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}; 00025 00026 00027 00028 // Connect module at I2C address addr using I2C port pins sda and scl 00029 MMA7660FC::MMA7660FC(PinName sda, PinName scl, int addr) : m_i2c(sda, scl) 00030 { 00031 SPI_W_Address = addr; // Write address 00032 SPI_R_Address = SPI_W_Address | 1; // Read address 00033 } 00034 00035 00036 // Destroys instance 00037 MMA7660FC::~MMA7660FC() 00038 { 00039 } 00040 00041 00042 // Device initialization 00043 void MMA7660FC::init() 00044 { 00045 write_reg(INTSU_STATUS, 0x00); // interrupt disable 00046 write_reg(SR_STATUS, 0x07); // 1 Samples/Second 00047 } 00048 00049 00050 // Reads the tilt angle 00051 void MMA7660FC::read_Tilt(float *x, float *y, float *z) 00052 { 00053 const char Addr_X = OUT_X; 00054 char buf[3] = {0,0,0}; 00055 00056 m_i2c.write(SPI_W_Address, &Addr_X, 1); // Pointer to the OUT_X register 00057 m_i2c.read(SPI_W_Address, buf, 3); // Read register content into buffer with 6bit 00058 00059 // returns the x, y, z coordinates transformed into full degrees 00060 *x = TILT_XY[(int)buf[0]]; 00061 *y = TILT_XY[(int)buf[1]]; 00062 *z = TILT_Z[(int)buf[2]]; 00063 } 00064 00065 00066 // Reads x data 00067 int MMA7660FC::read_x() 00068 { 00069 m_i2c.start(); // Start 00070 m_i2c.write(SPI_W_Address); // A write to device 00071 m_i2c.write(OUT_X); // Register to read 00072 m_i2c.start(); 00073 m_i2c.write(SPI_R_Address); // Read from device 00074 char x = m_i2c.read(0); // Read the data 00075 m_i2c.stop(); 00076 00077 return (int)x; 00078 } 00079 00080 00081 // Reads y data 00082 int MMA7660FC::read_y() 00083 { 00084 m_i2c.start(); // Start 00085 m_i2c.write(SPI_W_Address); // A write to device 00086 m_i2c.write(OUT_Y); // Register to read 00087 m_i2c.start(); 00088 m_i2c.write(SPI_R_Address); // Read from device 00089 char y = m_i2c.read(0); // Read the data 00090 m_i2c.stop(); 00091 00092 return (int)y; 00093 } 00094 00095 00096 // Reads z data 00097 int MMA7660FC::read_z() 00098 { 00099 m_i2c.start(); // Start 00100 m_i2c.write(SPI_W_Address); // A write to device 00101 m_i2c.write(OUT_Z); // Register to read 00102 m_i2c.start(); 00103 m_i2c.write(SPI_R_Address); // Read from device 00104 char z = m_i2c.read(0); // Read the data 00105 m_i2c.stop(); 00106 00107 return (int)z; 00108 } 00109 00110 // Reads MMA7660FC Orientation 00111 char const* MMA7660FC::read_Side() 00112 { 00113 char tiltStatus = read_reg(TILT_STATUS); 00114 00115 if((tiltStatus & 0x03) == 0x01) 00116 return "Front"; 00117 if((tiltStatus & 0x03) == 0x02) 00118 return "Back"; 00119 00120 return "Unknown"; 00121 } 00122 00123 00124 // Reads MMA7660FC Orientation 00125 char const* MMA7660FC::read_Orientation() 00126 { 00127 char tiltStatus = read_reg(TILT_STATUS); 00128 00129 if((tiltStatus & 0x1c) == 0x04) 00130 return "Left"; 00131 if((tiltStatus & 0x1c) == 0x08) 00132 return "Right"; 00133 if((tiltStatus & 0x1c) == 0x14) 00134 return "Down"; 00135 if((tiltStatus & 0x1c) == 0x18) 00136 return "Up"; 00137 00138 return "Unknown"; 00139 } 00140 00141 00142 00143 // Read from specified MMA7660FC register 00144 char MMA7660FC::read_reg(char addr) 00145 { 00146 m_i2c.start(); // Start 00147 m_i2c.write(SPI_W_Address); // A write to device 00148 m_i2c.write(addr); // Register to read 00149 m_i2c.start(); 00150 m_i2c.write(SPI_R_Address); // Read from device 00151 char c = m_i2c.read(0); // Read the data 00152 m_i2c.stop(); 00153 00154 return c; 00155 } 00156 00157 00158 // Write register (The device must be placed in Standby Mode to change the value of the registers) 00159 void MMA7660FC::write_reg(char addr, char data) 00160 { 00161 char cmd[2] = {0, 0}; 00162 00163 cmd[0] = MODE_STATUS; 00164 cmd[1] = 0x00; // Standby Mode on 00165 m_i2c.write(SPI_W_Address, cmd, 2); 00166 00167 cmd[0] = addr; 00168 cmd[1] = data; // New value of the register 00169 m_i2c.write(SPI_W_Address, cmd, 2); 00170 00171 cmd[0] = MODE_STATUS; 00172 cmd[1] = 0x01; // Active Mode on 00173 m_i2c.write(SPI_W_Address, cmd, 2); 00174 } 00175 00176 00177 // check if the address exist on an I2C bus 00178 int MMA7660FC::check() 00179 { 00180 return m_i2c.write(SPI_W_Address, NULL, 0); 00181 }
Generated on Tue Jul 12 2022 18:21:47 by 1.7.2