AnsweredAssumed Answered

Recommended LIS2DH Setup

Question asked by Ted Mawson on Nov 1, 2017
Latest reply on Feb 9, 2018 by M Poppitz

I have an LIS2DH device in my design and it's working - sort of.  I was given some LIS3DH C driver code which I'm using under the advice that the LIS2DH is register-compatible with the LIS3DH.

So my requirement is to take 12-bit X,Y,Z readings ten times per second with a timing loop in my code making other sensor readings interleaved with the accelerometer ones and saving the data to flash memory.  The setup code I'm running is listed at the bottom of this post.  My timing code sets up a flag called takeAccelSample and, if that's true (which it is 10 times per second), I take a reading using the code also below.

 

What I observe is that my LIS2DH starts off taking readings and all seems good but, after 3 or 4 thousand samples have been taken, the data turns to junk like 0,0,4 continuously where normal data is 148,-204,12,-148,-212,24, and, occasionally the readings will be -1,-1,-1 or 0,0,0 for the whole dataset like as if the device wan't initialized properly.

 

I'm sure that I haven't set up the registers correctly and I'm struggling to understand how to configure the LIS2DH registers to do what I want, I don't think I want FIFO because I'm unable to guarantee that the loop timing of my code will match the LIS2DH taking 10 times per second under its own timing loop and I need the data I read to be time-aligned with the other sensor readings.  I do have the LIS2DH INT1 pin connected to my microcontroller so I could use that if it helps but I haven't used it yet.

 

I think it should be possible to ask for a sample as I start my run and  then in 0.1 seconds that sample should be ready and I can read and ask for another without having to wait for the result.  Can someone please give me advice on how to configure the LIS2DH to read the samples on demand?

 

Thanks in advance 

 

// +++++++++++++++ Take Accelerometer Samples ++++++++++++++++++++++++++++++
// TM - Take Accelerometer Samples
if (takeAccelSample) { // this will be true if recording mode is active
response = LIS3DH_GetAccAxesRaw(&data); // TM sample Raw Accel data over SPI
if(response==1){
// TM - capture data values
rawAccel[0][accelIndx] = data.AXIS_X >> 6;
rawAccel[1][accelIndx] = data.AXIS_Y >> 6;
rawAccel[2][accelIndx] = data.AXIS_Z >> 6;
accelIndx++; // we have to increment the accellerometer index once after capturing the samples
takeAccelSample = 0; // clear flag now that sample has been taken
}
} // end of take accelerometer samples

 

 

// +++++++++++++++ Accelerometer Setup ++++++++++++++++++++++++++++++
void accelerometerSetup()
{
// Set Output Data Rate (ODR) - CTRL_REG1
response = LIS3DH_SetODR(LIS3DH_ODR_10Hz);
// sprintf(strMsg1,"T response = %u\r\n", response); SCB_1_UartPutString(strMsg1);
if(response==1){
if (verBose) {
sprintf(strMsg1,"\n\rSET_ODR_OK \n\r"); SCB_1_UartPutString(strMsg1);
}
}
// Set High Pass Filter (HPF) Mode - CTRL_REG2
response = LIS3DH_SetHPFMode(LIS3DH_HPM_NORMAL_MODE);
if(response==1){
if (verBose) {
sprintf(strMsg1,"\n\rSET_HPF_OK \n\r"); SCB_1_UartPutString(strMsg1);
}
}
// Set Interrupt Mode
response = LIS3DH_SetInt1Pin(LIS3DH_CLICK_ON_PIN_INT1_DISABLE | LIS3DH_I1_INT1_ON_PIN_INT1_ENABLE |
LIS3DH_I1_INT2_ON_PIN_INT1_DISABLE | LIS3DH_I1_DRDY1_ON_INT1_ENABLE | LIS3DH_I1_DRDY2_ON_INT1_DISABLE |
LIS3DH_WTM_ON_INT1_DISABLE | LIS3DH_INT1_OVERRUN_DISABLE );
if(response==1){
if (verBose) {
sprintf(strMsg1,"\n\rSET_Interrupts_OK \n\r"); SCB_1_UartPutString(strMsg1);
}
}
// Set FIFO watermark level
response = LIS3DH_SetWaterMark(1); // Set watermark to 1
if(response==1){
if (verBose) {
sprintf(strMsg1,"\n\rSET_FIFO_WtrMk_OK \n\r"); SCB_1_UartPutString(strMsg1);
}
}
// Set FIFO mode - CTRL_REG? also enables FIFO? (TM thinks it does)
response = LIS3DH_FIFOModeEnable(LIS3DH_FIFO_DISABLE); // (LIS3DH_FIFO_MODE);
if(response==1){
if (verBose) {
sprintf(strMsg1,"\n\rSET_FIFO_Mode_OK \n\r"); SCB_1_UartPutString(strMsg1);
}
}
//
response = LIS3DH_SetBLE(LIS3DH_BLE_LSB); // _LSB or _MSB ??? TM pretty sure it's LSB
if(response==1){
if (verBose) {
sprintf(strMsg1,"\n\rSET_BLE_Mode_OK \n\r"); SCB_1_UartPutString(strMsg1);
}
}

// Set mode
response = LIS3DH_SetMode(LIS3DH_LOW_POWER);
if(response==1){
if (verBose) {
sprintf(strMsg1,"SET_MODE_OK \n\r"); SCB_1_UartPutString(strMsg1);
sprintf(strMsg1,"M response = %u\r\n", response); SCB_1_UartPutString(strMsg1);
}
}
// Set Fullscale to be +- 2g
response = LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);
if(response==1){
if (verBose) {
sprintf(strMsg1,"SET_FULLSCALE_OK \n\r"); SCB_1_UartPutString(strMsg1);
}
}
// Set axis enable
//set axis Enable
response = LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);
if(response==1){
if (verBose) {
sprintf(strMsg1,"SET_AXIS_OK \n\r"); SCB_1_UartPutString(strMsg1);
}
}
}

Outcomes