JP PANG / MMA8491Q

Dependents:   XtrinsicSensorEVK SDFileSystem_HelloWorld5_copy xtrinsic_sensors

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 INT_SOURCE        0x0C 
00022 #define REG_WHO_AM_I      0x0D
00023 #define HP_FILTER_CUTOFF  0x0F 
00024 #define PULSE_CFG         0x21 
00025 #define PULSE_SRC         0x22 
00026 #define PULSE_THSX        0x23 
00027 #define PULSE_THSY        0x24 
00028 #define PULSE_THSZ        0x25 
00029 #define PULSE_TMLT        0x26 
00030 #define PULSE_LTCY        0x27 
00031 #define PULSE_WIND        0x28 
00032 #define REG_CTRL_REG_1    0x2A 
00033 #define CTRL_REG2         0x2B
00034 #define CTRL_REG4         0x2D 
00035 #define CTRL_REG5         0x2E 
00036 #define REG_OUT_X_MSB     0x01
00037 #define REG_OUT_Y_MSB     0x03
00038 #define REG_OUT_Z_MSB     0x05
00039 
00040 #define UINT14_MAX        16383
00041 
00042 MMA8491Q::MMA8491Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
00043     // activate the peripheral
00044     uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
00045     writeRegs(data, 2);
00046 }
00047 /*
00048 MMA8491Q::MMA8491Q(PinName sda, PinName scl, int addr, PinName Xout, PinName Yout, PinName Zout) 
00049     : m_i2c(sda, scl), m_addr(addr), m_xout(Xout), m_yout(Yout), m_zout(Zout) {
00050     // activate the peripheral
00051     uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
00052     writeRegs(data, 2);
00053 }
00054 */
00055 MMA8491Q::~MMA8491Q() { }
00056 
00057 uint8_t MMA8491Q::getWhoAmI() {
00058     uint8_t who_am_i = 0;
00059     readRegs(REG_WHO_AM_I, &who_am_i, 1);
00060     return who_am_i;
00061 }
00062 
00063 float MMA8491Q::getAccX() {
00064 //divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity)
00065     return (float(getAccAxis(REG_OUT_X_MSB))*4.0/4096.0);
00066 }
00067 
00068 float MMA8491Q::getAccY() {
00069     return (float(getAccAxis(REG_OUT_Y_MSB))*4.0/4096.0);
00070 }
00071 
00072 float MMA8491Q::getAccZ() {
00073     return (float(getAccAxis(REG_OUT_Z_MSB))*4.0/4096.0);
00074 }
00075 
00076 void MMA8491Q::getAccAllAxis(float * res) {
00077     res[0] = getAccX();
00078     res[1] = getAccY();
00079     res[2] = getAccZ();
00080 }
00081 
00082 int16_t MMA8491Q::getAccAxis(uint8_t addr) {
00083     int16_t acc;
00084     uint8_t res[2];
00085     readRegs(addr, res, 2);
00086 
00087     acc = (res[0] << 6) | (res[1] >> 2);
00088     if (acc > UINT14_MAX/2)
00089         acc -= UINT14_MAX;
00090 
00091     return acc;
00092 }
00093 
00094 void MMA8491Q::setDoubleTap(void){
00095 //Implemented directly from Freescale's AN4072 
00096 //Added to MMA8491Q lib
00097 
00098     uint8_t CTRL_REG1_Data;
00099 //    int adds;
00100    uint8_t data[2] = {REG_CTRL_REG_1, 0x08};
00101     
00102     //400 Hz, Standby Mode
00103     writeRegs(data,2);
00104     
00105     //Enable X, Y and Z Double Pulse with DPA = 0 no double pulse abort    
00106     data[0]=PULSE_CFG;data[1]=0x2A;
00107     writeRegs(data,2);
00108     
00109     //SetThreshold 3g on X and Y and 5g on Z
00110     //Note: Every step is 0.063g
00111     //3 g/0.063g = 48 counts
00112     //5g/0.063g = 79 counts
00113     data[0]=PULSE_THSX;data[1]=0x30;
00114     writeRegs(data,2);//Set X Threshold to 3g 
00115     data[0]=PULSE_THSY;data[1]=0x30;
00116     writeRegs(data,2);//Set Y Threshold to 3g 
00117     data[0]=PULSE_THSZ;data[1]=0x4F;
00118     writeRegs(data,2);//Set Z Threshold to 5g
00119 
00120     //Set Time Limit for Tap Detection to 60 ms LP Mode
00121     //Note: 400 Hz ODR, Time step is 1.25 ms per step
00122     //60 ms/1.25 ms = 48 counts 
00123     data[0]=PULSE_TMLT;data[1]=0x30;
00124     writeRegs(data,2);//60 ms
00125     
00126     //Set Latency Time to 200 ms
00127     //Note: 400 Hz ODR LPMode, Time step is 2.5 ms per step 00 ms/2.5 ms = 80 counts
00128     data[0]=PULSE_LTCY;data[1]=0x50;
00129     writeRegs(data,2);//200 ms
00130     
00131     //Set Time Window for second tap to 300 ms
00132     //Note: 400 Hz ODR LP Mode, Time step is 2.5 ms per step
00133     //300 ms/2.5 ms = 120 counts
00134     data[0]=PULSE_WIND;data[1]=0x78;
00135     writeRegs(data,2);//300 ms
00136     
00137     //Route INT1 to System Interrupt
00138     data[0]=CTRL_REG4;data[1]=0x08;
00139     writeRegs(data,2);//Enable Pulse Interrupt in System CTRL_REG4
00140     data[0]=CTRL_REG5;data[1]=0x08; 
00141     writeRegs(data,2);//Route Pulse Interrupt to INT1 hardware Pin CTRL_REG5
00142 
00143     //Set the device to Active Mode
00144     readRegs(0x2A,&CTRL_REG1_Data,1);//Read out the contents of the register 
00145     CTRL_REG1_Data |= 0x01; //Change the value in the register to Active Mode.
00146     data[0]=REG_CTRL_REG_1; 
00147     data[1]=CTRL_REG1_Data;
00148     writeRegs(data,2);//Write in the updated value to put the device in Active Mode
00149 }
00150 
00151 
00152 void MMA8491Q::readRegs(int addr, uint8_t * data, int len) {
00153     char t[1] = {addr};
00154     m_i2c.write(m_addr, t, 1, true);
00155     m_i2c.read(m_addr, (char *)data, len);
00156 }
00157 
00158 
00159 
00160 void MMA8491Q::writeRegs(uint8_t * data, int len) {
00161     m_i2c.write(m_addr, (char *)data, len);
00162 }