add FIFO functions.

Dependents:   test_ADXL375_i2c

Committer:
Sigma884
Date:
Wed Nov 14 09:12:38 2018 +0000
Revision:
0:46fcf349bdca
Child:
1:91bcb4ada70c
3???????ADXL375???????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sigma884 0:46fcf349bdca 1 #include "ADXL375_i2c.h"
Sigma884 0:46fcf349bdca 2
Sigma884 0:46fcf349bdca 3 ADXL375_i2c :: ADXL375_i2c(I2C &userI2C, ALT_ADDRESS LOW_HIGH){
Sigma884 0:46fcf349bdca 4 if(LOW_HIGH == ALT_ADDRESS_LOW){
Sigma884 0:46fcf349bdca 5 ADXL375_I2C_READ = ADXL375_I2C_LOW_READ;
Sigma884 0:46fcf349bdca 6 ADXL375_I2C_WRITE = ADXL375_I2C_LOW_WRITE;
Sigma884 0:46fcf349bdca 7 ADXL375_I2C_ADDRESS = ADXL375_I2C_LOW_ADDRESS;
Sigma884 0:46fcf349bdca 8 }
Sigma884 0:46fcf349bdca 9 else if(LOW_HIGH == ALT_ADDRESS_HIGH){
Sigma884 0:46fcf349bdca 10 ADXL375_I2C_READ = ADXL375_I2C_HIGH_READ;
Sigma884 0:46fcf349bdca 11 ADXL375_I2C_WRITE = ADXL375_I2C_HIGH_WRITE;
Sigma884 0:46fcf349bdca 12 ADXL375_I2C_ADDRESS = ADXL375_I2C_HIGH_ADDRESS;
Sigma884 0:46fcf349bdca 13 }
Sigma884 0:46fcf349bdca 14
Sigma884 0:46fcf349bdca 15 i2c = &userI2C;
Sigma884 0:46fcf349bdca 16 i2c -> frequency(400000);
Sigma884 0:46fcf349bdca 17
Sigma884 0:46fcf349bdca 18 char tx[2];
Sigma884 0:46fcf349bdca 19 tx[0] = ADXL375_BW_RATE_REG;
Sigma884 0:46fcf349bdca 20 tx[1] = ADXL375_3200HZ;
Sigma884 0:46fcf349bdca 21 i2c -> write(ADXL375_I2C_WRITE, tx, 2);
Sigma884 0:46fcf349bdca 22
Sigma884 0:46fcf349bdca 23 char rx[2];
Sigma884 0:46fcf349bdca 24 rx[0] = ADXL375_DATA_FORMAT_REG;
Sigma884 0:46fcf349bdca 25 rx[1] = 0x0B;
Sigma884 0:46fcf349bdca 26 i2c -> write(ADXL375_I2C_WRITE, rx, 2);
Sigma884 0:46fcf349bdca 27
Sigma884 0:46fcf349bdca 28 char x[2];
Sigma884 0:46fcf349bdca 29 x[0] = ADXL375_OFSX_REG ;
Sigma884 0:46fcf349bdca 30 x[1] = 0xFE;
Sigma884 0:46fcf349bdca 31 i2c -> write(ADXL375_I2C_WRITE, x, 2);
Sigma884 0:46fcf349bdca 32 char y[2];
Sigma884 0:46fcf349bdca 33 y[0] = ADXL375_OFSY_REG ;
Sigma884 0:46fcf349bdca 34 y[1] = 0xFE;
Sigma884 0:46fcf349bdca 35 i2c -> write(ADXL375_I2C_WRITE, y, 2);
Sigma884 0:46fcf349bdca 36 char z[2];
Sigma884 0:46fcf349bdca 37 z[0] = ADXL375_OFSZ_REG ;
Sigma884 0:46fcf349bdca 38 z[1] = 0x00;
Sigma884 0:46fcf349bdca 39 i2c -> write(ADXL375_I2C_WRITE, z, 2);
Sigma884 0:46fcf349bdca 40
Sigma884 0:46fcf349bdca 41 singleByteWrite(ADXL375_POWER_CTL_REG, 0x08);
Sigma884 0:46fcf349bdca 42
Sigma884 0:46fcf349bdca 43 offset_x = offset_y = offset_z = 0.0f;
Sigma884 0:46fcf349bdca 44 }
Sigma884 0:46fcf349bdca 45
Sigma884 0:46fcf349bdca 46 char ADXL375_i2c :: singleByteRead(char address){
Sigma884 0:46fcf349bdca 47 char tx = address;
Sigma884 0:46fcf349bdca 48 char output;
Sigma884 0:46fcf349bdca 49 i2c -> write(ADXL375_I2C_WRITE, &tx, 1);
Sigma884 0:46fcf349bdca 50 i2c -> read(ADXL375_I2C_READ, &output, 1);
Sigma884 0:46fcf349bdca 51 return output;
Sigma884 0:46fcf349bdca 52 }
Sigma884 0:46fcf349bdca 53
Sigma884 0:46fcf349bdca 54 void ADXL375_i2c :: singleByteWrite(char address, char data){
Sigma884 0:46fcf349bdca 55 char tx[2];
Sigma884 0:46fcf349bdca 56 tx[0] = address;
Sigma884 0:46fcf349bdca 57 tx[1] = data;
Sigma884 0:46fcf349bdca 58 i2c -> write(ADXL375_I2C_WRITE, tx, 2);
Sigma884 0:46fcf349bdca 59 }
Sigma884 0:46fcf349bdca 60
Sigma884 0:46fcf349bdca 61 void ADXL375_i2c :: multiByteRead(char address, char* output, int size){
Sigma884 0:46fcf349bdca 62 i2c -> write(ADXL375_I2C_WRITE, &address, 1);
Sigma884 0:46fcf349bdca 63 i2c -> read(ADXL375_I2C_READ, output, size);
Sigma884 0:46fcf349bdca 64 }
Sigma884 0:46fcf349bdca 65
Sigma884 0:46fcf349bdca 66 void ADXL375_i2c :: getOutput(float* output){
Sigma884 0:46fcf349bdca 67 char buffer[6];
Sigma884 0:46fcf349bdca 68 short int data[3];
Sigma884 0:46fcf349bdca 69 multiByteRead(ADXL375_DATAX0_REG, buffer, 6);
Sigma884 0:46fcf349bdca 70 data[0] = (int)buffer[1] << 8 | (int)buffer[0];
Sigma884 0:46fcf349bdca 71 data[1] = (int)buffer[3] << 8 | (int)buffer[2];
Sigma884 0:46fcf349bdca 72 data[2] = (int)buffer[5] << 8 | (int)buffer[4];
Sigma884 0:46fcf349bdca 73
Sigma884 0:46fcf349bdca 74 output[0] = (float)data[0] / 20.0f - offset_x;
Sigma884 0:46fcf349bdca 75 output[1] = (float)data[1] / 20.0f - offset_y;
Sigma884 0:46fcf349bdca 76 output[2] = (float)data[2] / 20.0f - offset_z;
Sigma884 0:46fcf349bdca 77 }
Sigma884 0:46fcf349bdca 78
Sigma884 0:46fcf349bdca 79 int ADXL375_i2c :: whoAmI(){
Sigma884 0:46fcf349bdca 80 char who_am_i = singleByteRead(ADXL375_DEVID_REG);
Sigma884 0:46fcf349bdca 81 if(who_am_i == 0xE5){
Sigma884 0:46fcf349bdca 82 return 1;
Sigma884 0:46fcf349bdca 83 }
Sigma884 0:46fcf349bdca 84 else{
Sigma884 0:46fcf349bdca 85 return -1;
Sigma884 0:46fcf349bdca 86 }
Sigma884 0:46fcf349bdca 87 }
Sigma884 0:46fcf349bdca 88
Sigma884 0:46fcf349bdca 89 void ADXL375_i2c :: offset(float x, float y, float z){
Sigma884 0:46fcf349bdca 90 offset_x = x;
Sigma884 0:46fcf349bdca 91 offset_y = y;
Sigma884 0:46fcf349bdca 92 offset_z = z;
Sigma884 0:46fcf349bdca 93 }
Sigma884 0:46fcf349bdca 94
Sigma884 0:46fcf349bdca 95 void ADXL375_i2c :: setDataRate(char rate){
Sigma884 0:46fcf349bdca 96 char buff = singleByteRead(ADXL375_BW_RATE_REG);
Sigma884 0:46fcf349bdca 97 buff &= 0x10;
Sigma884 0:46fcf349bdca 98 buff |= rate;
Sigma884 0:46fcf349bdca 99 singleByteWrite(ADXL375_BW_RATE_REG, buff);
Sigma884 0:46fcf349bdca 100 }