test

Dependencies:   AE_KXSD9

Committer:
MuroMoe
Date:
Thu Nov 07 09:10:59 2019 +0000
Revision:
2:7d8b206eccbf
i

Who changed what in which revision?

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