3軸加速度センサADXL375のI2Cライブラリ MPU9250では取れないような大きな加速度が取得できます データは粗目です
Dependents: Hybrid_IZU2019 SWAN_IZU2019_v1 Shock_Alt_Logger_v2_1 ADXL375_Test
Revision 0:46fcf349bdca, committed 2018-11-14
- Comitter:
- Sigma884
- Date:
- Wed Nov 14 09:12:38 2018 +0000
- Commit message:
- 3???????ADXL375???????????
Changed in this revision
ADXL375_i2c.cpp | Show annotated file Show diff for this revision Revisions of this file |
ADXL375_i2c.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 46fcf349bdca ADXL375_i2c.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ADXL375_i2c.cpp Wed Nov 14 09:12:38 2018 +0000 @@ -0,0 +1,100 @@ +#include "ADXL375_i2c.h" + +ADXL375_i2c :: ADXL375_i2c(I2C &userI2C, ALT_ADDRESS LOW_HIGH){ + if(LOW_HIGH == ALT_ADDRESS_LOW){ + ADXL375_I2C_READ = ADXL375_I2C_LOW_READ; + ADXL375_I2C_WRITE = ADXL375_I2C_LOW_WRITE; + ADXL375_I2C_ADDRESS = ADXL375_I2C_LOW_ADDRESS; + } + else if(LOW_HIGH == ALT_ADDRESS_HIGH){ + ADXL375_I2C_READ = ADXL375_I2C_HIGH_READ; + ADXL375_I2C_WRITE = ADXL375_I2C_HIGH_WRITE; + ADXL375_I2C_ADDRESS = ADXL375_I2C_HIGH_ADDRESS; + } + + i2c = &userI2C; + i2c -> frequency(400000); + + char tx[2]; + tx[0] = ADXL375_BW_RATE_REG; + tx[1] = ADXL375_3200HZ; + i2c -> write(ADXL375_I2C_WRITE, tx, 2); + + char rx[2]; + rx[0] = ADXL375_DATA_FORMAT_REG; + rx[1] = 0x0B; + i2c -> write(ADXL375_I2C_WRITE, rx, 2); + + char x[2]; + x[0] = ADXL375_OFSX_REG ; + x[1] = 0xFE; + i2c -> write(ADXL375_I2C_WRITE, x, 2); + char y[2]; + y[0] = ADXL375_OFSY_REG ; + y[1] = 0xFE; + i2c -> write(ADXL375_I2C_WRITE, y, 2); + char z[2]; + z[0] = ADXL375_OFSZ_REG ; + z[1] = 0x00; + i2c -> write(ADXL375_I2C_WRITE, z, 2); + + singleByteWrite(ADXL375_POWER_CTL_REG, 0x08); + + offset_x = offset_y = offset_z = 0.0f; +} + +char ADXL375_i2c :: singleByteRead(char address){ + char tx = address; + char output; + i2c -> write(ADXL375_I2C_WRITE, &tx, 1); + i2c -> read(ADXL375_I2C_READ, &output, 1); + return output; +} + +void ADXL375_i2c :: singleByteWrite(char address, char data){ + char tx[2]; + tx[0] = address; + tx[1] = data; + i2c -> write(ADXL375_I2C_WRITE, tx, 2); +} + +void ADXL375_i2c :: multiByteRead(char address, char* output, int size){ + i2c -> write(ADXL375_I2C_WRITE, &address, 1); + i2c -> read(ADXL375_I2C_READ, output, size); +} + +void ADXL375_i2c :: getOutput(float* output){ + char buffer[6]; + short int data[3]; + multiByteRead(ADXL375_DATAX0_REG, buffer, 6); + data[0] = (int)buffer[1] << 8 | (int)buffer[0]; + data[1] = (int)buffer[3] << 8 | (int)buffer[2]; + data[2] = (int)buffer[5] << 8 | (int)buffer[4]; + + output[0] = (float)data[0] / 20.0f - offset_x; + output[1] = (float)data[1] / 20.0f - offset_y; + output[2] = (float)data[2] / 20.0f - offset_z; +} + +int ADXL375_i2c :: whoAmI(){ + char who_am_i = singleByteRead(ADXL375_DEVID_REG); + if(who_am_i == 0xE5){ + return 1; + } + else{ + return -1; + } +} + +void ADXL375_i2c :: offset(float x, float y, float z){ + offset_x = x; + offset_y = y; + offset_z = z; +} + +void ADXL375_i2c :: setDataRate(char rate){ + char buff = singleByteRead(ADXL375_BW_RATE_REG); + buff &= 0x10; + buff |= rate; + singleByteWrite(ADXL375_BW_RATE_REG, buff); +} \ No newline at end of file
diff -r 000000000000 -r 46fcf349bdca ADXL375_i2c.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ADXL375_i2c.h Wed Nov 14 09:12:38 2018 +0000 @@ -0,0 +1,101 @@ +#ifndef ADXL375_I2C_H +#define ADXL375_I2C_H + +#include "mbed.h" + +//Registers. +#define ADXL375_DEVID_REG 0x00 +#define ADXL375_THRESH_TAP_REG 0x1D +#define ADXL375_OFSX_REG 0x1E +#define ADXL375_OFSY_REG 0x1F +#define ADXL375_OFSZ_REG 0x20 +#define ADXL375_DUR_REG 0x21 +#define ADXL375_LATENT_REG 0x22 +#define ADXL375_WINDOW_REG 0x23 +#define ADXL375_THRESH_ACT_REG 0x24 +#define ADXL375_THRESH_INACT_REG 0x25 +#define ADXL375_TIME_INACT_REG 0x26 +#define ADXL375_ACT_INACT_CTL_REG 0x27 +#define ADXL375_THRESH_FF_REG 0x28 +#define ADXL375_TIME_FF_REG 0x29 +#define ADXL375_TAP_AXES_REG 0x2A +#define ADXL375_ACT_TAP_STATUS_REG 0x2B +#define ADXL375_BW_RATE_REG 0x2C +#define ADXL375_POWER_CTL_REG 0x2D +#define ADXL375_INT_ENABLE_REG 0x2E +#define ADXL375_INT_MAP_REG 0x2F +#define ADXL375_INT_SOURCE_REG 0x30 +#define ADXL375_DATA_FORMAT_REG 0x31 +#define ADXL375_DATAX0_REG 0x32 +#define ADXL375_DATAX1_REG 0x33 +#define ADXL375_DATAY0_REG 0x34 +#define ADXL375_DATAY1_REG 0x35 +#define ADXL375_DATAZ0_REG 0x36 +#define ADXL375_DATAZ1_REG 0x37 +#define ADXL375_FIFO_CTL 0x38 +#define ADXL375_FIFO_STATUS 0x39 + +//Data rate codes. +#define ADXL375_3200HZ 0x0F +#define ADXL375_1600HZ 0x0E +#define ADXL375_800HZ 0x0D +#define ADXL375_400HZ 0x0C +#define ADXL375_200HZ 0x0B +#define ADXL375_100HZ 0x0A +#define ADXL375_50HZ 0x09 +#define ADXL375_25HZ 0x08 +#define ADXL375_12HZ5 0x07 +#define ADXL375_6HZ25 0x06 + +// when ALT ADDRESS pin is LOW +#define ADXL375_I2C_LOW_READ 0xA7 +#define ADXL375_I2C_LOW_WRITE 0xA6 +#define ADXL375_I2C_LOW_ADDRESS 0x53 + +//when ALT ADDRESS pin is HIGH +#define ADXL375_I2C_HIGH_READ 0x3B +#define ADXL375_I2C_HIGH_WRITE 0x3A +#define ADXL375_I2C_HIGH_ADDRESS 0x1D + +#define ADXL375_X 0x00 +#define ADXL375_Y 0x01 +#define ADXL375_Z 0x02 + +// modes +#define MeasurementMode 0x08 +class ADXL375_i2c +{ + +public: + + typedef enum{ + ALT_ADDRESS_LOW, + ALT_ADDRESS_HIGH + }ALT_ADDRESS; + + ADXL375_i2c(I2C &userI2C, ALT_ADDRESS LOW_HIGH); + + void getOutput(float* output); + + int whoAmI(); + + void offset(float x, float y, float z); + + void setDataRate(char rate); + +private: + + I2C *i2c; + char ADXL375_I2C_READ; + char ADXL375_I2C_WRITE; + char ADXL375_I2C_ADDRESS; + + float offset_x, offset_y, offset_z; + + char singleByteRead(char address); + void singleByteWrite(char address, char data); + void multiByteRead(char startAddress, char* ptr_output, int size); + +}; + +#endif \ No newline at end of file