test

Dependents:   AE_KXSD9-2

Committer:
MuroMoe
Date:
Wed Nov 06 07:15:48 2019 +0000
Revision:
0:f6315336fc94
experiment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MuroMoe 0:f6315336fc94 1 #include "AE_KXSD9.h"
MuroMoe 0:f6315336fc94 2
MuroMoe 0:f6315336fc94 3 //constructor
MuroMoe 0:f6315336fc94 4 AE_KXSD9::AE_KXSD9(PinName sda, PinName scl, int addr1, int addr2)
MuroMoe 0:f6315336fc94 5 :i2c_k(sda, scl),
MuroMoe 0:f6315336fc94 6 addr_w(addr1),
MuroMoe 0:f6315336fc94 7 addr_r(addr2)
MuroMoe 0:f6315336fc94 8 {};
MuroMoe 0:f6315336fc94 9
MuroMoe 0:f6315336fc94 10
MuroMoe 0:f6315336fc94 11 //destructor
MuroMoe 0:f6315336fc94 12 /*
MuroMoe 0:f6315336fc94 13 AE_KXSD9::~AE_KXSD9()
MuroMoe 0:f6315336fc94 14 {
MuroMoe 0:f6315336fc94 15
MuroMoe 0:f6315336fc94 16 }
MuroMoe 0:f6315336fc94 17 */
MuroMoe 0:f6315336fc94 18
MuroMoe 0:f6315336fc94 19 //initialization(default setting)
MuroMoe 0:f6315336fc94 20 void AE_KXSD9::init()
MuroMoe 0:f6315336fc94 21 {
MuroMoe 0:f6315336fc94 22 char cmd_c=0xE1;//Reset Value
MuroMoe 0:f6315336fc94 23 char cmd_b=0x40;//Reset Value
MuroMoe 0:f6315336fc94 24 i2c_k.write(CTRL_REGC, &cmd_c, 1);
MuroMoe 0:f6315336fc94 25 i2c_k.write(CTRL_REGB, &cmd_b, 1);
MuroMoe 0:f6315336fc94 26 }
MuroMoe 0:f6315336fc94 27
MuroMoe 0:f6315336fc94 28 //read each axial acceleration(unit:g)
MuroMoe 0:f6315336fc94 29 void AE_KXSD9::read_xyz(double *x, double *y, double *z)
MuroMoe 0:f6315336fc94 30 {
MuroMoe 0:f6315336fc94 31 const char addr_x_h = XOUT_H;
MuroMoe 0:f6315336fc94 32 char data[6] = {};
MuroMoe 0:f6315336fc94 33 short int acc[3]={};
MuroMoe 0:f6315336fc94 34 i2c_k.write(addr_w, &addr_x_h, 1);
MuroMoe 0:f6315336fc94 35 wait_us(500);
MuroMoe 0:f6315336fc94 36 i2c_k.read(addr_r, data, 6);
MuroMoe 0:f6315336fc94 37 for(int i=0;i<6;i+=2)acc[i/2]=((short int)data[i] << 4) + (short int)data[i+1] - 2048;
MuroMoe 0:f6315336fc94 38
MuroMoe 0:f6315336fc94 39 *x = ((double)acc[0])/SENSITIVITY;
MuroMoe 0:f6315336fc94 40 *y = ((double)acc[1])/SENSITIVITY;
MuroMoe 0:f6315336fc94 41 *z = ((double)acc[2])/SENSITIVITY;
MuroMoe 0:f6315336fc94 42 }
MuroMoe 0:f6315336fc94 43
MuroMoe 0:f6315336fc94 44
MuroMoe 0:f6315336fc94 45 //read the acceleration in x-axis(unit:g)
MuroMoe 0:f6315336fc94 46 double AE_KXSD9::read_x()
MuroMoe 0:f6315336fc94 47 {
MuroMoe 0:f6315336fc94 48 const char addr_x_h = XOUT_H;
MuroMoe 0:f6315336fc94 49 char data[2] = {};
MuroMoe 0:f6315336fc94 50 short int acc;
MuroMoe 0:f6315336fc94 51 i2c_k.write(addr_w, &addr_x_h, 1);
MuroMoe 0:f6315336fc94 52 wait_us(500);
MuroMoe 0:f6315336fc94 53 i2c_k.read(addr_r, data, 2);
MuroMoe 0:f6315336fc94 54 acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
MuroMoe 0:f6315336fc94 55 return (double)acc/SENSITIVITY;
MuroMoe 0:f6315336fc94 56 }
MuroMoe 0:f6315336fc94 57
MuroMoe 0:f6315336fc94 58
MuroMoe 0:f6315336fc94 59 //read the acceleration in y-axis(unit:g)
MuroMoe 0:f6315336fc94 60 double AE_KXSD9::read_y()
MuroMoe 0:f6315336fc94 61 {
MuroMoe 0:f6315336fc94 62 const char addr_y_h = YOUT_H;
MuroMoe 0:f6315336fc94 63 char data[2] = {};
MuroMoe 0:f6315336fc94 64 short int acc;
MuroMoe 0:f6315336fc94 65 i2c_k.write(addr_w, &addr_y_h, 1);
MuroMoe 0:f6315336fc94 66 wait_us(500);
MuroMoe 0:f6315336fc94 67 i2c_k.read(addr_r, data, 2);
MuroMoe 0:f6315336fc94 68 acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
MuroMoe 0:f6315336fc94 69 return (double)acc/SENSITIVITY;
MuroMoe 0:f6315336fc94 70 }
MuroMoe 0:f6315336fc94 71
MuroMoe 0:f6315336fc94 72
MuroMoe 0:f6315336fc94 73 //read the acceleration in z-axis(unit:g)
MuroMoe 0:f6315336fc94 74 double AE_KXSD9::read_z()
MuroMoe 0:f6315336fc94 75 {
MuroMoe 0:f6315336fc94 76
MuroMoe 0:f6315336fc94 77 const char addr_z_h = ZOUT_H;
MuroMoe 0:f6315336fc94 78 char data[2] = {0,0};
MuroMoe 0:f6315336fc94 79 short int acc;
MuroMoe 0:f6315336fc94 80 i2c_k.write(addr_w, &addr_z_h, 1);
MuroMoe 0:f6315336fc94 81 wait_us(500);
MuroMoe 0:f6315336fc94 82 i2c_k.read(addr_r, data, 2);
MuroMoe 0:f6315336fc94 83 acc=((short int)data[0] << 4) + (short int)data[1] - 2048;
MuroMoe 0:f6315336fc94 84 return (double)acc/SENSITIVITY;
MuroMoe 0:f6315336fc94 85 }
MuroMoe 0:f6315336fc94 86
MuroMoe 0:f6315336fc94 87 /*
MuroMoe 0:f6315336fc94 88 //read from AE_KXSD9 register
MuroMoe 0:f6315336fc94 89 char AE_KXSD9::read_reg(char addr)
MuroMoe 0:f6315336fc94 90 {
MuroMoe 0:f6315336fc94 91
MuroMoe 0:f6315336fc94 92 }
MuroMoe 0:f6315336fc94 93 */
MuroMoe 0:f6315336fc94 94
MuroMoe 0:f6315336fc94 95 //write register
MuroMoe 0:f6315336fc94 96 /*
MuroMoe 0:f6315336fc94 97 void AE_KXSD9::write_reg(char addr, char data)
MuroMoe 0:f6315336fc94 98 {
MuroMoe 0:f6315336fc94 99
MuroMoe 0:f6315336fc94 100 }
MuroMoe 0:f6315336fc94 101 */