library for read and configuration the sensor FXOS8700CQ

Dependents:   1-K64F_with_5_acel

Fork of FXOS8700CQ by Thomas Murphy

Files at this revision

API Documentation at this revision

Comitter:
vinajarr
Date:
Thu Jan 18 07:51:57 2018 +0000
Parent:
6:75494f28f57f
Commit message:
implement interrupt;

Changed in this revision

FXOS8700CQ.cpp Show annotated file Show diff for this revision Revisions of this file
FXOS8700CQ.h Show annotated file Show diff for this revision Revisions of this file
diff -r 75494f28f57f -r 26db88bf6b70 FXOS8700CQ.cpp
--- a/FXOS8700CQ.cpp	Mon Dec 05 09:24:27 2016 +0000
+++ b/FXOS8700CQ.cpp	Thu Jan 18 07:51:57 2018 +0000
@@ -4,10 +4,9 @@
 uint8_t raw[FXOS8700CQ_READ_LEN]; // Buffer for reading out stored data
 
 // Construct class and its contents
-FXOS8700CQ::FXOS8700CQ(PinName sda, PinName scl, int addr) : dev_i2c(sda, scl), dev_addr(addr)
+FXOS8700CQ::FXOS8700CQ(I2C* puntero, int addr) : dev_i2c(puntero), dev_addr(addr)
 {
     // Initialization of the FXOS8700CQ
-    dev_i2c.frequency(200000); // Use maximum I2C frequency
     uint8_t data[6] = {0, 0, 0, 0, 0, 0}; // to write over I2C: device register, up to 5 bytes data
 
     // TODO: verify WHOAMI?
@@ -20,18 +19,17 @@
     // Now that the device is in standby, configure registers at will
 
     // Setup for write-though for CTRL_REG series
-    // Keep data[0] as FXOS8700CQ_CTRL_REG1
-    data[1] =
-        FXOS8700CQ_CTRL_REG1_ASLP_RATE2(1) | // 0b01 gives sleep rate of 12.5Hz
-        FXOS8700CQ_CTRL_REG1_DR3(1); // 0x001 gives ODR of 400Hz/200Hz hybrid
-
+    
+     //FXOS8700CQ_CTRL_REG1
+    data[1] = 0b00001100; //data 400Hz ODR Active mode and Low noise
+    
     // FXOS8700CQ_CTRL_REG2;
     data[2] =
-        FXOS8700CQ_CTRL_REG2_SMODS2(3) | // 0b11 gives low power sleep oversampling mode
-        FXOS8700CQ_CTRL_REG2_MODS2(1); // 0b01 gives low noise, low power oversampling mode
+        FXOS8700CQ_CTRL_REG2_SMODS2(2) | // 0b10 High Resolution
+        FXOS8700CQ_CTRL_REG2_MODS2(2); // 0b10 
 
     // No configuration changes from default 0x00 in CTRL_REG3
-    // Interrupts will be active low, their outputs in push-pull mode
+    // Interrupts will be active low, their outputs in push pull mode
     data[3] = 0x00;
 
     // FXOS8700CQ_CTRL_REG4;
@@ -54,14 +52,9 @@
 
     // Setup for write-through for M_CTRL_REG series
     data[0] = FXOS8700CQ_M_CTRL_REG1;
-    data[1] =
-        FXOS8700CQ_M_CTRL_REG1_M_ACAL | // set automatic calibration
-        FXOS8700CQ_M_CTRL_REG1_MO_OS3(7) | // use maximum magnetic oversampling
-        FXOS8700CQ_M_CTRL_REG1_M_HMS2(3); // enable hybrid sampling (both sensors)
-
+    data[1] = FXOS8700CQ_M_CTRL_REG1_M_HMS2(0); // only acelerometer
     // FXOS8700CQ_M_CTRL_REG2
-    data[2] =
-        FXOS8700CQ_M_CTRL_REG2_HYB_AUTOINC_MODE;
+    data[2] =0x00; 
 
     // FXOS8700CQ_M_CTRL_REG3
     data[3] =
@@ -153,15 +146,15 @@
         return 1;
     }
 
-    read_regs(FXOS8700CQ_M_OUT_X_MSB, raw, FXOS8700CQ_READ_LEN);
+    read_regs(FXOS8700CQ_OUT_X_MSB, raw, 6);
 
     // Pull out 16-bit, 2's complement magnetometer data
 
 
     // Pull out 14-bit, 2's complement, right-justified accelerometer data
-    accel_data->x = (raw[6] << 8) | raw[7];
-    accel_data->y = (raw[8] << 8) | raw[9];
-    accel_data->z = (raw[10] << 8) | raw[11];
+    accel_data->x = (raw[0] << 6) | raw[1]>>2;
+    accel_data->y = (raw[2] << 6) | raw[3]>>2;
+    accel_data->z = (raw[4] << 6) | raw[5]>>2;
 
     // Have to apply corrections to make the int16_t correct
     if(accel_data->x > UINT14_MAX/2) {
@@ -200,16 +193,16 @@
 
 // Private methods
 
-// Excepting the call to dev_i2c.frequency() in the constructor,
+// Excepting the call to dev_i2c->frequency() in the constructor,
 // the use of the mbed I2C class is restricted to these methods
 void FXOS8700CQ::read_regs(int reg_addr, uint8_t* data, int len)
 {
     char t[1] = {reg_addr};
-    dev_i2c.write(dev_addr, t, 1, true);
-    dev_i2c.read(dev_addr, (char *)data, len);
+    dev_i2c->write(dev_addr, t, 1, true);
+    dev_i2c->read(dev_addr, (char *)data, len);
 }
 
 void FXOS8700CQ::write_regs(uint8_t* data, int len)
 {
-    dev_i2c.write(dev_addr, (char*)data, len);
+    dev_i2c->write(dev_addr, (char*)data, len);
 }
diff -r 75494f28f57f -r 26db88bf6b70 FXOS8700CQ.h
--- a/FXOS8700CQ.h	Mon Dec 05 09:24:27 2016 +0000
+++ b/FXOS8700CQ.h	Thu Jan 18 07:51:57 2018 +0000
@@ -124,7 +124,7 @@
     * @param sdl SCL pin
     * @param addr address of the I2C peripheral in (7-bit << 1) form
     */
-    FXOS8700CQ(PinName sda, PinName scl, int addr);
+    FXOS8700CQ(I2C * puntero, int addr);
 
     /**
     * FXOS8700CQ destructor
@@ -162,15 +162,15 @@
     */
     uint8_t get_accel_scale(void);
 
-
+    void read_regs(int reg_addr, uint8_t* data, int len);
 
 private:
-    I2C dev_i2c; // instance of the mbed I2C class
+    I2C *dev_i2c; // instance of the mbed I2C class
     uint8_t dev_addr; // Device I2C address, in (7-bit << 1) form
     bool enabled; // keep track of enable bit of device
 
     // I2C helper methods
-    void read_regs(int reg_addr, uint8_t* data, int len);
+    
     void write_regs(uint8_t* data, int len);
 
 };