Hello,
As part of a work project I am attempting to add the ability to specify the I2C slave address to the H3LIS331DL library that Seed Studio has posted to Github. GitHub - Seeed-Studio/Accelerometer_H3LIS331DL: a library to use Grove & Xadow - 3-Axis Digital Accelerometer(±400g) ST makes demo boards that have the SDA[0] bit broken out to a pin, allowing 2 accelerometers to coexist on the same I2C bus. The demo code works fine for addressing the accelerometer at address 0x18 but the address is hard coded in the header file. My hope was to quickly mod the library so the address is a line argument for all the functions.
I'm a complete neophyte when it comes to C programming. I attempted to add an argument for the address to the basic setup and read data functions but the Arduino environment hangs when I attempt to compile. That seems to point to an issue in the C code since the Arduino environment isn't throwing an error.
Any guidance or suggestions would be extremely appreciated. FYI I removed the original library from the libraries folder in the Arduino folder so I could leave the include statements as is without accidently referencing the unmodified library.
Unfortunately the code is well beyond the 9k charcter limit so I will be attaching the C source and header files. Arduino code included below:
/* get accelerate data of H3LIS331DL
* Auth : lawliet(lawliet.zou@gmail.com)
* version : 0.1
*/
#include <H3LIS331DL.h>
#include <Wire.h>
//please get these value by running H3LIS331DL_AdjVal Sketch.
#define VAL_X_AXIS 203
#define VAL_Y_AXIS 165
#define VAL_Z_AXIS 371
byte addr0 = 0x18;
byte addr1 = 0x19;
H3LIS331DL h3lis;
void setup(){
Serial.begin(9600);
h3lis.init(addr0, H3LIS331DL_ODR_1000Hz, H3LIS331DL_NORMAL, H3LIS331DL_FULLSCALE_8);;
h3lis.importPara(addr0, VAL_X_AXIS, VAL_Y_AXIS, VAL_Z_AXIS);
}
void loop(){
int16_t x,y,z;
h3lis.readXYZ(addr0, &x,&y,&z);
Serial.print("x, y, z = ");
Serial.print(x);
Serial.print("\t");
Serial.print(y);
Serial.print("\t");
Serial.println(z);
double xyz[3];
h3lis.getAcceleration(addr0, xyz);
Serial.print("accelerate of x, y, z = ");
Serial.print(xyz[0]);
Serial.print("g");
Serial.print("\t");
Serial.print(xyz[1]);
Serial.print("g");
Serial.print("\t");
Serial.print(xyz[2]);
Serial.println("g");
delay(1000);
}
H3LIS331DL.cpp (41.7 KB)
H3LIS331DL.h (13.8 KB)