Darrell Crooks / Mbed 2 deprecated MMA8491Q

Dependencies:   mbed

Fork of MMA8491Q by JP PANG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA8491Q.cpp Source File

MMA8491Q.cpp

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 #include "MMA8491Q.h"
00020 
00021 #define REG_STATUS        0x00
00022 #define REG_OUT_X_MSB     0x01
00023 #define REG_OUT_Y_MSB     0x03
00024 #define REG_OUT_Z_MSB     0x05
00025 
00026 #define XDR               0x01  //X data ready flag position
00027 #define YDR               0x02  //Y data ready flag position
00028 #define ZDR               0x04  //Z data ready flag position
00029 #define ZYXDR             0x08  //X, Y, Z data ready flag position
00030 
00031 #define UINT14_MAX        16383 //maximum count for unsigned 14-bit integer
00032 
00033 MMA8491Q::MMA8491Q(PinName sda, PinName scl, int addr, PinName en) : m_i2c(sda, scl), m_addr(addr), m_en(en) {
00034   
00035 }
00036 
00037 MMA8491Q::MMA8491Q(PinName sda, PinName scl, int addr, PinName en, PinName Xout, PinName Yout, PinName Zout) 
00038                 : m_i2c(sda, scl), m_addr(addr), m_en(en), m_xout(Xout), m_yout(Yout), m_zout(Zout) {
00039     
00040 }
00041 
00042 
00043 MMA8491Q::~MMA8491Q() { }
00044 
00045 float MMA8491Q::getAccX() {
00046     uint8_t res[2];
00047     uint8_t status;
00048     float acc = 0;
00049     
00050     toggleEN();
00051     
00052     status = isDataAvailable(XDR);
00053     if(status){
00054         readRegs(REG_OUT_X_MSB, res, 2);    //get X readings
00055         acc = intAccToFloat(res); 
00056         
00057    }
00058    return acc;
00059 }    
00060 
00061 
00062 float MMA8491Q::getAccY() {
00063     uint8_t res[2];
00064     uint8_t status;
00065     float acc = 0;
00066     
00067     toggleEN();
00068     
00069     status = isDataAvailable(YDR);
00070     if(status){
00071         readRegs(REG_OUT_Y_MSB, res, 2);    //get Y readings
00072         acc = intAccToFloat(res); 
00073         
00074    }
00075    return acc;
00076 }
00077 
00078 
00079 float MMA8491Q::getAccZ() {
00080     uint8_t res[2];
00081     uint8_t status;
00082     float acc = 0;
00083     
00084     toggleEN();
00085     
00086     status = isDataAvailable(ZDR);
00087     if(status){
00088         readRegs(REG_OUT_Z_MSB, res, 2);    //get Z readings
00089         acc = intAccToFloat(res); 
00090         
00091    }
00092    return acc;
00093 }
00094 
00095 
00096 uint8_t MMA8491Q::getAccAllAxis(float * acc) {
00097     uint8_t res[6];
00098     uint8_t status;
00099 
00100     toggleEN();
00101     
00102     status = isDataAvailable(ZYXDR);
00103     if(status){
00104         readRegs(REG_OUT_X_MSB, res, 6);    //get X,Y,Z readings
00105         for(int8_t i=0; i<3; i++){
00106             acc[i] = intAccToFloat(&res[(i*2)]); 
00107         }
00108    }
00109    return status;
00110 }
00111 
00112 
00113 uint8_t MMA8491Q::isDataAvailable( uint8_t mask){
00114     unsigned char status;   
00115     readRegs( REG_STATUS, &status, 1);
00116     status &= mask;    //check for requested Data Ready flag
00117     return status;    
00118 }
00119 
00120 
00121 //convert 14-bit 2's compliment (mg) number from sensor to float scaled to 1g
00122 float MMA8491Q::intAccToFloat(uint8_t * data) {
00123     int16_t acc = 0;
00124     
00125     acc = (data[0] << 6) | (data[1] >> 2);
00126     if (acc > UINT14_MAX/2)     //MSB of 14-bit value is 1 or negative
00127         acc -= UINT14_MAX;      //extend sign to 16 bits
00128         
00129     return acc/1000.0;
00130  }
00131  
00132  
00133  void MMA8491Q::toggleEN(void){
00134     m_en = 0;             //cycle cycle enable low-high 
00135     wait(0.001);
00136     m_en = 1;             //start MMA8491Q reading
00137     wait(0.001);                //wait 1 ms for reading
00138 
00139  }
00140  
00141  
00142 void MMA8491Q::readRegs(int addr, uint8_t * data, int len) {
00143     char t[1] = {addr};
00144     m_i2c.write(m_addr, t, 1, true);
00145     m_i2c.read(m_addr, (char *)data, len);
00146 }
00147 
00148 
00149 void MMA8491Q::writeRegs(uint8_t * data, int len) {
00150     m_i2c.write(m_addr, (char *)data, len);
00151 }