test
Revision 1:b1565a18d53d, committed 2019-11-07
- Comitter:
- MuroMoe
- Date:
- Thu Nov 07 09:10:54 2019 +0000
- Parent:
- 0:f6315336fc94
- Commit message:
- i
Changed in this revision
AE_KXSD9.cpp | Show diff for this revision Revisions of this file |
AE_KXSD9.h | Show diff for this revision Revisions of this file |
diff -r f6315336fc94 -r b1565a18d53d AE_KXSD9.cpp --- a/AE_KXSD9.cpp Wed Nov 06 07:15:48 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -#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 f6315336fc94 -r b1565a18d53d AE_KXSD9.h --- a/AE_KXSD9.h Wed Nov 06 07:15:48 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -#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