Basic component library for the FXAS21000 gyro from Freescale

Dependents:   Hello_FXAS21000 Multi-Sensor Freescale_Multi-Sensor_Shield FRDM-STBC-AGM01 ... more

Files at this revision

API Documentation at this revision

Comitter:
JimCarver
Date:
Sat Apr 19 00:10:49 2014 +0000
Child:
1:88a036a417bf
Commit message:
Improved version, added getWhoAmI() and readXYXraw()

Changed in this revision

FXAS21000.cpp Show annotated file Show diff for this revision Revisions of this file
FXAS21000.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXAS21000.cpp	Sat Apr 19 00:10:49 2014 +0000
@@ -0,0 +1,73 @@
+ #include "FXAS21000.h"
+ 
+FXAS21000::FXAS21000(PinName sda, PinName scl) : _i2c(sda, scl) {
+ 
+    begin();
+}
+              
+void FXAS21000::RegRead( char reg, char * d, int len)
+{
+    char cmd[1];
+    cmd[0] = reg;
+char i2c_addr = FXAS21000_SLAVE_ADDR;
+_i2c.write( i2c_addr, cmd, 1, true);
+_i2c.read ( i2c_addr, d, len);
+}
+
+void FXAS21000::begin(void)
+{
+    char data[2];
+    // write 0000 1000 = 0x08 to gyro control register 1 to place FXAS21000 into
+    // standby
+    // [7-1] = 0000 000
+    // [0]: active=0
+    data[0] = FXAS21000_CTRL_REG1;
+    data[1] = 0x08;
+    _i2c.write( FXAS21000_SLAVE_ADDR, data, 2);
+    
+    // write 0001 1011 to CRTL_REG0 register
+
+    data[0] = FXAS21000_CTRL_REG0;
+    data[1] = 0x1B;
+    _i2c.write( FXAS21000_SLAVE_ADDR, data, 2);
+
+    // write 0000 1001 to gyro control register 1
+
+    data[0] = FXAS21000_CTRL_REG1;
+    data[1] = 0x0A;
+    _i2c.write( FXAS21000_SLAVE_ADDR, data, 2);
+}
+
+char FXAS21000::getWhoAmI(void)
+{
+    char d;
+    RegRead( FXAS21000_WHOAMI, &d, 1);
+    return(d);
+}
+
+void FXAS21000::ReadXYZ(float * a)
+{
+    char d[7];
+    int16_t t[6];
+
+    RegRead( FXAS21000_STATUS, d, 7);
+    t[0] = ((d[1] * 256) + ((unsigned short) d[2]));
+    t[1] = ((d[3] * 256) + ((unsigned short) d[4]));
+    t[2] = ((d[5] * 256) + ((unsigned short) d[6]));
+    //printf("%X\r\n", (int) d[0]);
+
+    a[0] = (float) t[0] * 0.003125;
+    a[1] = (float) t[1] * 0.003125;
+    a[2] = (float) t[2] * 0.003125;
+  
+}
+
+void FXAS21000::ReadXYZraw(int16_t * t)
+{
+    char d[7];
+
+    RegRead( FXAS21000_STATUS, d, 7);
+    t[0] = ((d[1] * 256) + ((unsigned short) d[2]));
+    t[1] = ((d[3] * 256) + ((unsigned short) d[4]));
+    t[2] = ((d[5] * 256) + ((unsigned short) d[6]));
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXAS21000.h	Sat Apr 19 00:10:49 2014 +0000
@@ -0,0 +1,69 @@
+#ifndef FXAS21000_H
+#define FXAS21000_H
+
+#include "mbed.h"
+
+
+// MMA8652 Slave Address
+#define FXAS21000_SLAVE_ADDR 0x40
+
+// MMA8652 internal register addresses
+#define FXAS21000_STATUS 0x00
+#define FXAS21000_WHOAMI 0x0C
+#define FXAS21000_XYZ_DATA_CFG 0x0E
+#define FXAS21000_CTRL_REG0 0x0D
+#define FXAS21000_CTRL_REG1 0x13
+#define FXAS21000_WHOAMI_VAL 0xD1
+
+class FXAS21000
+{
+public:
+    /**
+    * FXAS21000 constructor
+    *
+    * @param sda SDA pin
+    * @param sdl SCL pin
+    * 
+    */
+    FXAS21000(PinName sda, PinName scl);
+ 
+    
+    /**
+    * Get the Gyro values
+    * Result is floating point degrees / second
+    *
+    * @param floating point array where the results will be placed
+    */
+    void ReadXYZ(float * a);
+
+    
+    /**
+    * Get the Gyro values
+    * Result is signed 16 bit value
+    *
+    * @param int16_t point array where the results will be placed
+    */
+    void ReadXYZraw(int16_t * t);
+
+   
+    /**
+    * Get the value of the WHO_AM_I register
+    *
+    * @returns DEVICE_ID value == 0xD1
+    */
+    //char getWhoAmI();
+    char getWhoAmI(void);
+    
+private:
+
+    I2C _i2c;
+    /** Set the device in active mode
+    */
+    void begin( void);
+    
+    void RegRead( char reg, char * d, int len);
+
+
+};
+
+#endif