Library for MMA7660FC Accelerometer device

Dependents:   TestCode_MMA7660FC 3D_Accelerometer_Tester RTOS-aap-board-modules embed_Grove_3-Axis_Digital_Accelerometer ... more

Files at this revision

API Documentation at this revision

Comitter:
edodm85
Date:
Sat Jun 30 10:08:19 2012 +0000
Child:
1:6e7a2df4f149
Commit message:
1

Changed in this revision

MMA7660FC.cpp Show annotated file Show diff for this revision Revisions of this file
MMA7660FC.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA7660FC.cpp	Sat Jun 30 10:08:19 2012 +0000
@@ -0,0 +1,114 @@
+// Author: Edoardo De Marchi
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+ 
+ 
+#include "MMA7660FC.h"
+
+#define OUT_X 0x00              // [6:0] are Read Only 6-bit output value X (XOUT[5] is 0 if the g direction is positive, 1 is negative)
+#define OUT_Y 0x01              // [6:0] are Read Only 6-bit output value Y (YOUT[5] is 0 if the g direction is positive, 1 is negative)
+#define OUT_Z 0x02              // [6:0] are Read Only 6-bit output value Z (ZOUT[5] is 0 if the g direction is positive, 1 is negative)
+#define TILT_STATUS 0x03        // Tilt Status (Read only)
+#define SRST_STATUS 0x04        // Sample Rate Status Register (Read only)
+#define SPCNT_STATUS 0x05       // Sleep Count Register (Read/Write)
+#define INTSU_STATUS 0x06       // Interrupt Setup Register
+#define MODE_STATUS 0x07        // Mode Register (Read/Write)
+#define SR_STATUS 0x08          // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
+#define PDET_STATUS 0x09        // Tap/Pulse Detection Register (Read/Write)
+#define PD_STATUS 0xA           // Tap/Pulse Debounce Count Register (Read/Write)
+
+
+
+        // Connect module at I2C address addr using I2C port pins sda and scl
+MMA7660FC::MMA7660FC(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
+{
+
+}
+
+
+        // Destroys instance
+MMA7660FC::~MMA7660FC()
+{
+
+}
+
+
+        // Device initialization
+void MMA7660FC::init()
+{
+    
+    write_reg(INTSU_STATUS, 0x10);      // automatic interrupt after every measurement
+    write_reg(SR_STATUS, 0x00);         // 120 Samples/Second
+    write_reg(MODE_STATUS, 0x01);       // Active Mode
+    
+}
+
+
+        // Reads X, Y, Z data
+void MMA7660FC::read_g(int *x, int *y, int *z)
+{
+
+    const char Addr_X = OUT_X;
+    char buf[3] = {0,0,0};
+    
+    m_i2c.write(m_addr, &Addr_X, 1);         // Pointer to the OUT_X register
+    m_i2c.read(m_addr, buf, 3);              // Read register content into buffer with 6bit
+    
+    *x = buf[0];
+    *y = buf[1];
+    *z = buf[2];      
+  
+}
+
+
+        // Read from specified MMA7660FC register
+char MMA7660FC::read_reg(char addr)
+{
+    
+    m_i2c.start();                  // Start
+    m_i2c.write(0x98);              // A write to device 0x98
+    m_i2c.write(addr);              // Register to read
+    m_i2c.start();                  // Need to send start condition here
+    m_i2c.write(0x99);              // Read from device 0x99
+    char c = m_i2c.read(0);         // Read the data
+    m_i2c.stop();                   // Stop
+ 
+    return c;
+    
+}
+
+
+        // Write register (The device must be placed in Standby Mode to change the value of the registers) 
+void MMA7660FC::write_reg(char addr, char data)
+{
+
+    char cmd[2] = {0, 0};
+    
+    cmd[0] = MODE_STATUS;
+    cmd[1] = 0x00;                      // Standby Mode on
+    m_i2c.write(m_addr, cmd, 2);
+  
+    cmd[0] = addr;
+    cmd[1] = data;                      // New value of the register
+    m_i2c.write(m_addr, cmd, 2); 
+      
+    cmd[0] = MODE_STATUS;
+    cmd[1] = 0x01;                      // Active Mode on
+    m_i2c.write(m_addr, cmd, 2);
+                  
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA7660FC.h	Sat Jun 30 10:08:19 2012 +0000
@@ -0,0 +1,105 @@
+// Author: Edoardo De Marchi 
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+ 
+ 
+#ifndef MBED_MMA7660FC_H
+#define MBED_MMA7660FC_H
+ 
+#include "mbed.h"
+ 
+/** Accelerometer MMA7660FC class 
+ *
+ * Example:
+ * @code
+ * 
+ * #include "mbed.h"
+ * #include "MMA7660FC.h"
+ * 
+ * #define ADDR_MMA7660 0x98
+ * 
+ * MMA7660FC Acc(p28, p27, ADDR_MMA7660);
+ * serial pc(USBTX, USBRX);
+ *
+ * int main() 
+ * {
+ *   Acc.init(); 
+ *      while(1)
+ *      {
+ *          int x=0, y=0, z=0;
+ *          Acc.read_g(&x, &y, &z);
+ *          pc.printf("x: \n", x);
+ *          pc.printf("x: \n", y);
+ *          pc.printf("x: \n", z);
+ *          wait(1);       
+ *      }
+ * }
+ * @endcode
+ */ 
+class MMA7660FC         
+{        
+    public:
+        
+        
+       /** Create an MMA7660FC object connected to the specified I2C object
+        *
+        * @param sda I2C data port
+        * @param scl I2C clock port
+        * @param addr The address of the MMA7660FC
+        */ 
+      MMA7660FC(PinName sda, PinName scl, int addr);
+       
+       /** Destroys an MMA7660FC object
+        *
+        */
+      ~MMA7660FC();
+      
+       /** Initialization of device MMA7660FC
+        *
+        */
+      void init();
+    
+       /** Read the x,y,z axes acceleration
+        *
+        * @param *x Value of X acceleration
+        * @param *y Value of Y acceleration
+        * @param *z Value of Z acceleration
+        */
+      void read_g(int *x, int *y, int *z);
+            
+        /** Read from specified MMA7660FC register
+         *
+         * @param addr The internal registeraddress of the MMA7660FC
+         * @returns The value of the register
+         */
+      char read_reg(char addr);
+        
+        /** Write to specified MMA7660FC register
+        *
+        * @param addr The internal registeraddress of the MMA7660FC
+        * @param data New value of the register
+        */    
+      void write_reg(char addr, char data); 
+      
+   
+    private:
+      I2C m_i2c;
+      int m_addr;   
+};
+
+#endif 
\ No newline at end of file