Ok
Dependencies: mbed_rtos_types Mutex mbed_rtos_storage mbed Semaphore
MMA8451Q.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef MMA8451Q_H 00020 #define MMA8451Q_H 00021 00022 #include "mbed.h" 00023 00024 /** 00025 * MMA8451Q accelerometer example 00026 * 00027 * @code 00028 * #include "mbed.h" 00029 * #include "MMA8451Q.h" 00030 * 00031 * #define MMA8451_I2C_ADDRESS (0x1d<<1) 00032 * 00033 * int main(void) { 00034 * 00035 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); 00036 * PwmOut rled(LED_RED); 00037 * PwmOut gled(LED_GREEN); 00038 * PwmOut bled(LED_BLUE); 00039 * 00040 * while (true) { 00041 * rled = 1.0 - abs(acc.getAccX()); 00042 * gled = 1.0 - abs(acc.getAccY()); 00043 * bled = 1.0 - abs(acc.getAccZ()); 00044 * wait(0.1); 00045 * } 00046 * } 00047 * @endcode 00048 */ 00049 class MMA8451Q 00050 { 00051 public: 00052 /** 00053 * MMA8451Q constructor 00054 * 00055 * @param sda SDA pin 00056 * @param sdl SCL pin 00057 * @param addr addr of the I2C peripheral 00058 */ 00059 MMA8451Q(PinName sda, PinName scl, int addr); 00060 00061 /** 00062 * MMA8451Q destructor 00063 */ 00064 ~MMA8451Q(); 00065 00066 /** 00067 * Get the value of the WHO_AM_I register 00068 * 00069 * @returns WHO_AM_I value 00070 */ 00071 uint8_t getWhoAmI(); 00072 00073 /** 00074 * Get X axis acceleration 00075 * 00076 * @returns X axis acceleration 00077 */ 00078 float getAccX(); 00079 00080 /** 00081 * Get Y axis acceleration 00082 * 00083 * @returns Y axis acceleration 00084 */ 00085 float getAccY(); 00086 00087 /** 00088 * Get Z axis acceleration 00089 * 00090 * @returns Z axis acceleration 00091 */ 00092 float getAccZ(); 00093 00094 /** 00095 * Get XYZ axis acceleration 00096 * 00097 * @param res array where acceleration data will be stored 00098 */ 00099 void getAccAllAxis(float * res); 00100 00101 private: 00102 I2C m_i2c; 00103 int m_addr; 00104 void readRegs(int addr, uint8_t * data, int len); 00105 void writeRegs(uint8_t * data, int len); 00106 int16_t getAccAxis(uint8_t addr); 00107 00108 }; 00109 #define REG_WHO_AM_I 0x0D 00110 #define REG_CTRL_REG_1 0x2A 00111 #define REG_OUT_X_MSB 0x01 00112 #define REG_OUT_Y_MSB 0x03 00113 #define REG_OUT_Z_MSB 0x05 00114 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00115 00116 #define UINT14_MAX 16383 00117 00118 00119 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { 00120 // activate the peripheral 00121 uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; 00122 writeRegs(data, 2); 00123 } 00124 MMA8451Q::~MMA8451Q() { } 00125 00126 uint8_t MMA8451Q::getWhoAmI() { 00127 uint8_t who_am_i = 0; 00128 readRegs(REG_WHO_AM_I, &who_am_i, 1); 00129 return who_am_i; 00130 } 00131 00132 float MMA8451Q::getAccX() { 00133 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); 00134 } 00135 00136 float MMA8451Q::getAccY() { 00137 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); 00138 } 00139 00140 float MMA8451Q::getAccZ() { 00141 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); 00142 } 00143 00144 void MMA8451Q::getAccAllAxis(float * res) { 00145 res[0] = getAccX(); 00146 res[1] = getAccY(); 00147 res[2] = getAccZ(); 00148 } 00149 00150 int16_t MMA8451Q::getAccAxis(uint8_t addr) { 00151 int16_t acc; 00152 uint8_t res[2]; 00153 readRegs(addr, res, 2); 00154 00155 acc = (res[0] << 6) | (res[1] >> 2); 00156 if (acc > UINT14_MAX/2) 00157 acc -= UINT14_MAX; 00158 00159 return acc; 00160 } 00161 00162 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { 00163 char t[1] = {addr}; 00164 m_i2c.write(m_addr, t, 1, true); 00165 m_i2c.read(m_addr, (char *)data, len); 00166 } 00167 00168 void MMA8451Q::writeRegs(uint8_t * data, int len) { 00169 m_i2c.write(m_addr, (char *)data, len); 00170 } 00171 #endif 00172
Generated on Tue Jul 12 2022 22:06:19 by
1.7.2