Double Tap Enable MMA8451Q library

Dependents:   FRDM_MMA8451Q_DTAP_DEMO FRDM_TSI data_logger quadCommand2 ... more

Fork of MMA8451Q by Emilio Monti

Files at this revision

API Documentation at this revision

Comitter:
JoKer
Date:
Sun Mar 10 04:34:03 2013 +0000
Parent:
4:c4d879a39775
Commit message:
Added Double Tap Detection functionality

Changed in this revision

MMA8451Q.cpp Show annotated file Show diff for this revision Revisions of this file
MMA8451Q.h Show annotated file Show diff for this revision Revisions of this file
--- a/MMA8451Q.cpp	Fri Oct 12 11:35:07 2012 +0000
+++ b/MMA8451Q.cpp	Sun Mar 10 04:34:03 2013 +0000
@@ -18,8 +18,21 @@
 
 #include "MMA8451Q.h"
 
+#define INT_SOURCE        0x0C 
 #define REG_WHO_AM_I      0x0D
-#define REG_CTRL_REG_1    0x2A
+#define HP_FILTER_CUTOFF  0x0F 
+#define PULSE_CFG         0x21 
+#define PULSE_SRC         0x22 
+#define PULSE_THSX        0x23 
+#define PULSE_THSY        0x24 
+#define PULSE_THSZ        0x25 
+#define PULSE_TMLT        0x26 
+#define PULSE_LTCY        0x27 
+#define PULSE_WIND        0x28 
+#define REG_CTRL_REG_1    0x2A 
+#define CTRL_REG2         0x2B
+#define CTRL_REG4         0x2D 
+#define CTRL_REG5         0x2E 
 #define REG_OUT_X_MSB     0x01
 #define REG_OUT_Y_MSB     0x03
 #define REG_OUT_Z_MSB     0x05
@@ -41,6 +54,7 @@
 }
 
 float MMA8451Q::getAccX() {
+//divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity)
     return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
 }
 
@@ -70,12 +84,72 @@
     return acc;
 }
 
+void MMA8451Q::setDoubleTap(void){
+//Implemented directly from Freescale's AN4072 
+//Added to MMA8451Q lib
+
+    uint8_t CTRL_REG1_Data;
+//    int adds;
+   uint8_t data[2] = {REG_CTRL_REG_1, 0x08};
+    
+    //400 Hz, Standby Mode
+    writeRegs(data,2);
+    
+    //Enable X, Y and Z Double Pulse with DPA = 0 no double pulse abort    
+    data[0]=PULSE_CFG;data[1]=0x2A;
+    writeRegs(data,2);
+    
+    //SetThreshold 3g on X and Y and 5g on Z
+    //Note: Every step is 0.063g
+    //3 g/0.063g = 48 counts
+    //5g/0.063g = 79 counts
+    data[0]=PULSE_THSX;data[1]=0x30;
+    writeRegs(data,2);//Set X Threshold to 3g 
+    data[0]=PULSE_THSY;data[1]=0x30;
+    writeRegs(data,2);//Set Y Threshold to 3g 
+    data[0]=PULSE_THSZ;data[1]=0x4F;
+    writeRegs(data,2);//Set Z Threshold to 5g
+
+    //Set Time Limit for Tap Detection to 60 ms LP Mode
+    //Note: 400 Hz ODR, Time step is 1.25 ms per step
+    //60 ms/1.25 ms = 48 counts 
+    data[0]=PULSE_TMLT;data[1]=0x30;
+    writeRegs(data,2);//60 ms
+    
+    //Set Latency Time to 200 ms
+    //Note: 400 Hz ODR LPMode, Time step is 2.5 ms per step 00 ms/2.5 ms = 80 counts
+    data[0]=PULSE_LTCY;data[1]=0x50;
+    writeRegs(data,2);//200 ms
+    
+    //Set Time Window for second tap to 300 ms
+    //Note: 400 Hz ODR LP Mode, Time step is 2.5 ms per step
+    //300 ms/2.5 ms = 120 counts
+    data[0]=PULSE_WIND;data[1]=0x78;
+    writeRegs(data,2);//300 ms
+    
+    //Route INT1 to System Interrupt
+    data[0]=CTRL_REG4;data[1]=0x08;
+    writeRegs(data,2);//Enable Pulse Interrupt in System CTRL_REG4
+    data[0]=CTRL_REG5;data[1]=0x08; 
+    writeRegs(data,2);//Route Pulse Interrupt to INT1 hardware Pin CTRL_REG5
+
+    //Set the device to Active Mode
+    readRegs(0x2A,&CTRL_REG1_Data,1);//Read out the contents of the register 
+    CTRL_REG1_Data |= 0x01; //Change the value in the register to Active Mode.
+    data[0]=REG_CTRL_REG_1; 
+    data[1]=CTRL_REG1_Data;
+    writeRegs(data,2);//Write in the updated value to put the device in Active Mode
+}
+
+
 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
     char t[1] = {addr};
     m_i2c.write(m_addr, t, 1, true);
     m_i2c.read(m_addr, (char *)data, len);
 }
 
+
+
 void MMA8451Q::writeRegs(uint8_t * data, int len) {
     m_i2c.write(m_addr, (char *)data, len);
 }
--- a/MMA8451Q.h	Fri Oct 12 11:35:07 2012 +0000
+++ b/MMA8451Q.h	Sun Mar 10 04:34:03 2013 +0000
@@ -97,6 +97,55 @@
    * @param res array where acceleration data will be stored
    */
   void getAccAllAxis(float * res);
+  
+  /** JK
+  * Setup Double Tap detection
+ 
+ 
+Example:
+
+#include "mbed.h"
+#include "MMA8451Q.h"
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+#define ON  0
+#define OFF !ON
+
+//Setup the interrupts for the MMA8451Q
+InterruptIn accInt1(PTA14);
+InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer
+
+uint8_t togstat=0;//Led status
+DigitalOut bled(LED_BLUE);
+
+
+void tapTrue(void){//ISR
+    if(togstat == 0){
+        togstat = 1;
+        bled=ON;
+    } else {
+        togstat = 0;
+        bled=OFF;
+    }
+        
+}
+
+
+int main(void) {
+
+    MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance
+  
+    acc.setDoubleTap();//Setup the MMA8451Q to look for a double Tap
+    accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14
+    
+    while (true) {
+    //Interrupt driven so nothing in main loop
+    }
+}
+
+
+  */
+  void setDoubleTap(void);
 
 private:
   I2C m_i2c;