Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MMA8451Q.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 "MMA8451Q.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 MMA8451Q::MMA8451Q(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 MMA8451Q::~MMA8451Q() { } 00049 00050 uint8_t MMA8451Q::getWhoAmI() { 00051 uint8_t who_am_i = 0; 00052 readRegs(REG_WHO_AM_I, &who_am_i, 1); 00053 return who_am_i; 00054 } 00055 00056 float MMA8451Q::getAccX() { 00057 //divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity) 00058 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); 00059 } 00060 00061 float MMA8451Q::getAccY() { 00062 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); 00063 } 00064 00065 float MMA8451Q::getAccZ() { 00066 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); 00067 } 00068 00069 void MMA8451Q::getAccAllAxis(float * res) { 00070 res[0] = getAccX(); 00071 res[1] = getAccY(); 00072 res[2] = getAccZ(); 00073 } 00074 00075 int16_t MMA8451Q::getAccAxis(uint8_t addr) { 00076 int16_t acc; 00077 uint8_t res[2]; 00078 readRegs(addr, res, 2); 00079 00080 acc = (res[0] << 6) | (res[1] >> 2); 00081 if (acc > UINT14_MAX/2) 00082 acc -= UINT14_MAX; 00083 00084 return acc; 00085 } 00086 00087 void MMA8451Q::setDoubleTap(void){ 00088 //Implemented directly from Freescale's AN4072 00089 //Added to MMA8451Q lib 00090 00091 uint8_t CTRL_REG1_Data; 00092 // int adds; 00093 uint8_t data[2] = {REG_CTRL_REG_1, 0x08}; 00094 00095 //400 Hz, Standby Mode 00096 writeRegs(data,2); 00097 00098 //Enable X, Y and Z Double Pulse with DPA = 0 no double pulse abort 00099 data[0]=PULSE_CFG;data[1]=0x2A; 00100 writeRegs(data,2); 00101 00102 //SetThreshold 3g on X and Y and 5g on Z 00103 //Note: Every step is 0.063g 00104 //3 g/0.063g = 48 counts 00105 //5g/0.063g = 79 counts 00106 data[0]=PULSE_THSX;data[1]=0x30; 00107 writeRegs(data,2);//Set X Threshold to 3g 00108 data[0]=PULSE_THSY;data[1]=0x30; 00109 writeRegs(data,2);//Set Y Threshold to 3g 00110 data[0]=PULSE_THSZ;data[1]=0x4F; 00111 writeRegs(data,2);//Set Z Threshold to 5g 00112 00113 //Set Time Limit for Tap Detection to 60 ms LP Mode 00114 //Note: 400 Hz ODR, Time step is 1.25 ms per step 00115 //60 ms/1.25 ms = 48 counts 00116 data[0]=PULSE_TMLT;data[1]=0x30; 00117 writeRegs(data,2);//60 ms 00118 00119 //Set Latency Time to 200 ms 00120 //Note: 400 Hz ODR LPMode, Time step is 2.5 ms per step 00 ms/2.5 ms = 80 counts 00121 data[0]=PULSE_LTCY;data[1]=0x50; 00122 writeRegs(data,2);//200 ms 00123 00124 //Set Time Window for second tap to 300 ms 00125 //Note: 400 Hz ODR LP Mode, Time step is 2.5 ms per step 00126 //300 ms/2.5 ms = 120 counts 00127 data[0]=PULSE_WIND;data[1]=0x78; 00128 writeRegs(data,2);//300 ms 00129 00130 //Route INT1 to System Interrupt 00131 data[0]=CTRL_REG4;data[1]=0x08; 00132 writeRegs(data,2);//Enable Pulse Interrupt in System CTRL_REG4 00133 data[0]=CTRL_REG5;data[1]=0x08; 00134 writeRegs(data,2);//Route Pulse Interrupt to INT1 hardware Pin CTRL_REG5 00135 00136 //Set the device to Active Mode 00137 readRegs(0x2A,&CTRL_REG1_Data,1);//Read out the contents of the register 00138 CTRL_REG1_Data |= 0x01; //Change the value in the register to Active Mode. 00139 data[0]=REG_CTRL_REG_1; 00140 data[1]=CTRL_REG1_Data; 00141 writeRegs(data,2);//Write in the updated value to put the device in Active Mode 00142 } 00143 00144 00145 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { 00146 char t[1] = {addr}; 00147 m_i2c.write(m_addr, t, 1, true); 00148 m_i2c.read(m_addr, (char *)data, len); 00149 } 00150 00151 00152 00153 void MMA8451Q::writeRegs(uint8_t * data, int len) { 00154 m_i2c.write(m_addr, (char *)data, len); 00155 }
Generated on Tue Jul 12 2022 21:36:10 by
1.7.2