Library for AE-KXSD9(3 axis accelerometer Module KXSD9-2050)

Dependents:   LPC1768_AE_KXSD9

Files at this revision

API Documentation at this revision

Comitter:
Naoto_111
Date:
Fri May 16 14:17:37 2014 +0000
Parent:
0:809f34a09f98
Commit message:
first release; some functions unimplemented

Changed in this revision

AE_KXSD9.cpp Show annotated file Show diff for this revision Revisions of this file
AE_KXSD9.h Show annotated file Show diff for this revision Revisions of this file
KXSD9_2050.cpp Show diff for this revision Revisions of this file
KXSD9_2050.h Show diff for this revision Revisions of this file
diff -r 809f34a09f98 -r 4dc1a0ac0cf1 AE_KXSD9.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AE_KXSD9.cpp	Fri May 16 14:17:37 2014 +0000
@@ -0,0 +1,101 @@
+#include "AE_KXSD9.h"
+
+//constructor
+AE_KXSD9::AE_KXSD9(PinName sda, PinName scl, int addr1, int addr2)
+    :i2c_k(sda, scl),
+     addr_w(addr1),
+     addr_r(addr2)
+{};
+
+
+//destructor
+/*
+AE_KXSD9::~AE_KXSD9()
+{
+    
+}
+*/
+
+//initialization(default setting)
+void AE_KXSD9::init()
+{
+    char cmd_c=0xE1;//Reset Value
+    char cmd_b=0x40;//Reset Value
+    i2c_k.write(CTRL_REGC, &cmd_c, 1);
+    i2c_k.write(CTRL_REGB, &cmd_b, 1);
+}
+
+//read each axial acceleration(unit:g)
+void AE_KXSD9::read_xyz(double *x, double *y, double *z)
+{
+    const char addr_x_h = XOUT_H;
+    char data[6] = {};
+    short int acc[3]={};
+    i2c_k.write(addr_w, &addr_x_h, 1);
+    wait_us(500);
+    i2c_k.read(addr_r, data, 6);
+    for(int i=0;i<6;i+=2)acc[i/2]=((short int)data[i] << 4) + (short int)data[i+1] - 2048;
+    
+    *x = ((double)acc[0])/SENSITIVITY;
+    *y = ((double)acc[1])/SENSITIVITY;
+    *z = ((double)acc[2])/SENSITIVITY; 
+}
+
+
+//read the acceleration in x-axis(unit:g)
+double AE_KXSD9::read_x()
+{
+    const char addr_x_h = XOUT_H;
+    char data[2] = {};
+    short int acc;
+    i2c_k.write(addr_w, &addr_x_h, 1); 
+    wait_us(500);
+    i2c_k.read(addr_r, data, 2);
+    acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
+    return (double)acc/SENSITIVITY;  
+}
+
+
+//read the acceleration in y-axis(unit:g)
+double AE_KXSD9::read_y()
+{
+    const char addr_y_h = YOUT_H;
+    char data[2] = {};
+    short int acc;
+    i2c_k.write(addr_w, &addr_y_h, 1);
+    wait_us(500);
+    i2c_k.read(addr_r, data, 2);
+    acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
+    return (double)acc/SENSITIVITY;  
+}
+
+
+//read the acceleration in z-axis(unit:g)
+double AE_KXSD9::read_z()
+{
+
+    const char addr_z_h = ZOUT_H;
+    char data[2] = {0,0};
+    short int acc;
+    i2c_k.write(addr_w, &addr_z_h, 1);
+    wait_us(500);
+    i2c_k.read(addr_r, data, 2);
+    acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
+    return (double)acc/SENSITIVITY;  
+}
+
+/*
+//read from AE_KXSD9 register
+char AE_KXSD9::read_reg(char addr)
+{
+    
+}
+*/
+
+//write register
+/*
+void AE_KXSD9::write_reg(char addr, char data)
+{
+               
+}
+*/
\ No newline at end of file
diff -r 809f34a09f98 -r 4dc1a0ac0cf1 AE_KXSD9.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AE_KXSD9.h	Fri May 16 14:17:37 2014 +0000
@@ -0,0 +1,38 @@
+#ifndef AE_KXSD9_H
+#define AE_KXSD9_H
+ 
+#include "mbed.h"
+
+class AE_KXSD9        
+{        
+    public:
+        AE_KXSD9(PinName sda, PinName scl, int addr1, int addr2);//constructor
+        //~AE_KXSD9();//destructor(unimplemented)
+        void init();//initialization(required)
+        void read_xyz(double *x, double *y, double *z);//read each axial acceleration(unit:g)
+        double read_x();//read the acceleration in x-axis(unit:g)
+        double read_y();//read the acceleration in y-axis(unit:g)
+        double read_z();//read the acceleration in z-axis(unit:g)
+        //char read_reg(char addr);//read from AE_KXSD9 register(unimplemented)
+        //void write_reg(char addr, char data); //write register(unimplemented)
+        enum{
+            XOUT_H=0x00,    //the highest 8 bits of the acceleration in x-axis
+            XOUT_L=0x01,    //the lowest 4 bits of the acceleration in x-axis
+            YOUT_H=0x02,    //the highest 8 bits of the acceleration in y-axis
+            YOUT_L=0x03,    //the lowest 4 bits of the acceleration in y-axis
+            ZOUT_H=0x04,   //the highest 8 bits of the acceleration in z-axis
+            ZOUT_L=0x05,    //the lowest 4 bits of the acceleration in z-axis
+            AUXOUT_H=0x06,    
+            AUXOUT_L=0x07,    
+            RESET_WRITE=0x0A,    
+            CTRL_REGC=0x0C,    
+            CTRL_REGB=0x0D,    
+            CTRL_REGA=0x0E,  
+            SENSITIVITY=819
+        };
+    private:
+      I2C i2c_k;
+      int addr_w, addr_r;
+};
+
+#endif 
\ No newline at end of file
diff -r 809f34a09f98 -r 4dc1a0ac0cf1 KXSD9_2050.cpp
--- a/KXSD9_2050.cpp	Fri May 16 14:10:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,101 +0,0 @@
-#include "KXSD9_2050.h"
-
-//constructor
-KXSD9_2050::KXSD9_2050(PinName sda, PinName scl, int addr1, int addr2)
-    :i2c_k(sda, scl),
-     addr_w(addr1),
-     addr_r(addr2)
-{};
-
-
-//destructor
-/*
-KXSD9_2050::~KXSD9_2050()
-{
-    
-}
-*/
-
-//initialization(default setting)
-void KXSD9_2050::init()
-{
-    char cmd_c=0xE1;//Reset Value
-    char cmd_b=0x40;//Reset Value
-    i2c_k.write(CTRL_REGC, &cmd_c, 1);
-    i2c_k.write(CTRL_REGB, &cmd_b, 1);
-}
-
-//read each axial acceleration(unit:g)
-void KXSD9_2050::read_xyz(double *x, double *y, double *z)
-{
-    const char addr_x_h = XOUT_H;
-    char data[6] = {};
-    short int acc[3]={};
-    i2c_k.write(addr_w, &addr_x_h, 1);
-    wait_us(500);
-    i2c_k.read(addr_r, data, 6);
-    for(int i=0;i<6;i+=2)acc[i/2]=((short int)data[i] << 4) + (short int)data[i+1] - 2048;
-    
-    *x = ((double)acc[0])/SENSITIVITY;
-    *y = ((double)acc[1])/SENSITIVITY;
-    *z = ((double)acc[2])/SENSITIVITY; 
-}
-
-
-//read the acceleration in x-axis(unit:g)
-double KXSD9_2050::read_x()
-{
-    const char addr_x_h = XOUT_H;
-    char data[2] = {};
-    short int acc;
-    i2c_k.write(addr_w, &addr_x_h, 1); 
-    wait_us(500);
-    i2c_k.read(addr_r, data, 2);
-    acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
-    return (double)acc/SENSITIVITY;  
-}
-
-
-//read the acceleration in y-axis(unit:g)
-double KXSD9_2050::read_y()
-{
-    const char addr_y_h = YOUT_H;
-    char data[2] = {};
-    short int acc;
-    i2c_k.write(addr_w, &addr_y_h, 1);
-    wait_us(500);
-    i2c_k.read(addr_r, data, 2);
-    acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
-    return (double)acc/SENSITIVITY;  
-}
-
-
-//read the acceleration in z-axis(unit:g)
-double KXSD9_2050::read_z()
-{
-
-    const char addr_z_h = ZOUT_H;
-    char data[2] = {0,0};
-    short int acc;
-    i2c_k.write(addr_w, &addr_z_h, 1);
-    wait_us(500);
-    i2c_k.read(addr_r, data, 2);
-    acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
-    return (double)acc/SENSITIVITY;  
-}
-
-/*
-//read from KXSD9_2050 register
-char KXSD9_2050::read_reg(char addr)
-{
-    
-}
-*/
-
-//write register
-/*
-void KXSD9_2050::write_reg(char addr, char data)
-{
-               
-}
-*/
\ No newline at end of file
diff -r 809f34a09f98 -r 4dc1a0ac0cf1 KXSD9_2050.h
--- a/KXSD9_2050.h	Fri May 16 14:10:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-#ifndef KXSD9_2050_H
-#define KXSD9_2050_H
- 
-#include "mbed.h"
-
-class KXSD9_2050        
-{        
-    public:
-        KXSD9_2050(PinName sda, PinName scl, int addr1, int addr2);//constructor
-        //~KXSD9_2050();//destructor(unimplemented)
-        void init();//initialization(required)
-        void read_xyz(double *x, double *y, double *z);//read each axial acceleration(unit:g)
-        double read_x();//read the acceleration in x-axis(unit:g)
-        double read_y();//read the acceleration in y-axis(unit:g)
-        double read_z();//read the acceleration in z-axis(unit:g)
-        //char read_reg(char addr);//read from KXSD9_2050 register(unimplemented)
-        //void write_reg(char addr, char data); //write register(unimplemented)
-        enum{
-            XOUT_H=0x00,    //the highest 8 bits of the acceleration in x-axis
-            XOUT_L=0x01,    //the lowest 4 bits of the acceleration in x-axis
-            YOUT_H=0x02,    //the highest 8 bits of the acceleration in y-axis
-            YOUT_L=0x03,    //the lowest 4 bits of the acceleration in y-axis
-            ZOUT_H=0x04,   //the highest 8 bits of the acceleration in z-axis
-            ZOUT_L=0x05,    //the lowest 4 bits of the acceleration in z-axis
-            AUXOUT_H=0x06,    
-            AUXOUT_L=0x07,    
-            RESET_WRITE=0x0A,    
-            CTRL_REGC=0x0C,    
-            CTRL_REGB=0x0D,    
-            CTRL_REGA=0x0E,  
-            SENSITIVITY=819
-        };
-    private:
-      I2C i2c_k;
-      int addr_w, addr_r;
-};
-
-#endif 
\ No newline at end of file