test
Revision 2:7d8b206eccbf, committed 2019-11-07
- Comitter:
- MuroMoe
- Date:
- Thu Nov 07 09:10:59 2019 +0000
- Parent:
- 1:ffc27e62061d
- Commit message:
- i
Changed in this revision
diff -r ffc27e62061d -r 7d8b206eccbf AE_KXSD9.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AE_KXSD9.cpp Thu Nov 07 09:10:59 2019 +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 ffc27e62061d -r 7d8b206eccbf AE_KXSD9.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AE_KXSD9.h Thu Nov 07 09:10:59 2019 +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
diff -r ffc27e62061d -r 7d8b206eccbf AE_KXSD9.lib --- a/AE_KXSD9.lib Wed Nov 06 07:18:14 2019 +0000 +++ b/AE_KXSD9.lib Thu Nov 07 09:10:59 2019 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/MuroMoe/code/AE_KXSD9/#f6315336fc94 +https://os.mbed.com/users/MuroMoe/code/AE_KXSD9/#b1565a18d53d
diff -r ffc27e62061d -r 7d8b206eccbf SDFileSystem.lib --- a/SDFileSystem.lib Wed Nov 06 07:18:14 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/users/MuroMoe/code/SDFileSystem/#36610d84d988
diff -r ffc27e62061d -r 7d8b206eccbf main.cpp --- a/main.cpp Wed Nov 06 07:18:14 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -#include "mbed.h" -#include "AE_KXSD9.h" -#include "SDFileSystem.h" -#define ADDR_W 0x30 -#define ADDR_R 0x31 - -Serial pc(USBTX, USBRX); // tx, rx -AE_KXSD9 i2c(D14, D15, ADDR_W, ADDR_R); //sda, scl, addr_w, addr_r -SDFileSystem sd(D11, D12, D13, D10, "sd"); -DigitalOut led(LED1); - -int Ready() { - - printf("Hello World!\n"); - printf("\nAnalogIn example\n"); - //mkdir("/sd/mydir", 0777); - - FILE *fp = fopen("/sd/mydir/AE_KXSD9.txt", "w"); - if(fp == NULL) { - error("Could not open file for write\n"); - } - } -int main() { - double x,y,z; - i2c.init(); - while(1){ - i2c.read_xyz(&x,&y,&z); - // pc.printf("\033[2J\033[0;0H");//clear console - pc.printf("x:%lf\r\ny:%lf\r\nz:%lf\r\n\n",x,y,z); - wait(1.0); - - fprintf(fp, "x y z \n"); - fclose(fp); - - //刻み時間 - //wait(1.0); // 1 second - } - }
diff -r ffc27e62061d -r 7d8b206eccbf mbed.bld --- a/mbed.bld Wed Nov 06 07:18:14 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file