I use the LowLevelExalmple as base and i add the interrupt for DataReady.
I would like to obtain higher sampleRates. At present 1 accel measurement is about 5ms (ODR = 208Hz)
when i increase the ODR to 416 or 833 , the interrupt routine goes corrupt.
It looks like the Interrupt is not latched. in the datasheet of the LSM6DS3TR-C they speak about a DRDY_PULSED bit in the DRDY_PULSE_CFG register.
In the driver LSM6DS.h these are not known. The address in the datasheet (0B)refers to a LSM6DS3_ACC_GYRO_ORIENT_CFG_G register in the driver.
Code:
/*****************************************************************************/
// LowLevelExample.ino
// Hardware: Grove - 6-Axis Accelerometer&Gyroscope
// Arduino IDE: Arduino-1.65
// Author: Lambor
// Date: Oct,2015
// Version: v1.0
//
// Modified by:
// Data:
// Description:
//
// by www.seeedstudio.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/*******************************************************************************/
#include "LSM6DS3.h"
#include "Wire.h"
#include "SPI.h"
volatile long interruptCount=0;
uint16_t errorsAndWarnings = 0;
uint8_t readingReg=0;
#define int1Pin PIN_LSM6DS3TR_C_INT1
volatile bool interrupted=false;
volatile long prevInterruptCount = 0; // Interrupt Counter from last loop
//Create instance of LSM6DS3Core
LSM6DS3Core myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A
long millisOld=0;
static int count=0;
static long milllisOld=0;
void setup() {
//Init Serial port
Serial.begin(9600);
while (!Serial);
//Call .beginCore() to configure the IMU
if (myIMU.beginCore() != 0) {
Serial.print("\nDevice Error.\n");
} else {
Serial.print("\nDevice OK.\n");
}
uint8_t dataToWrite = 0; //Temporary variable
//Setup the accelerometer******************************
dataToWrite = 0; //Start Fresh!
dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_100Hz;
dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_8g;
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_208Hz;
//Now, write the patched together data
errorsAndWarnings += myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);
//Set the ODR bit
errorsAndWarnings += myIMU.readRegister(&dataToWrite, LSM6DS3_ACC_GYRO_CTRL4_C);
dataToWrite &= ~((uint8_t)LSM6DS3_ACC_GYRO_BW_SCAL_ODR_ENABLED);
dataToWrite=0;
dataToWrite |= LSM6DS3_ACC_GYRO_INT1_DRDY_XL_ENABLED;
myIMU.writeRegister(LSM6DS3_ACC_GYRO_INT1_CTRL, dataToWrite);
pinMode(int1Pin, INPUT);
attachInterrupt(digitalPinToInterrupt(int1Pin), int1ISR, RISING);
delay(2000);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
setLedRGB(false, false, true); // set blue led
}
void int1ISR()
{
interruptCount++;
}
void loop() {
if (interruptCount > prevInterruptCount) {
long millisNew = millis();
Serial.print(millisNew);
Serial.print(" ");
Serial.print(millisNew-millisOld);
Serial.print(" ");
Serial.print(interruptCount);
millisOld=millisNew;
setLedRGB(false, true, false); // set green only
//Acelerometer axis X
int16_t temp;
if (myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTX_L_XL) != 0) {
errorsAndWarnings++;
}
Serial.print(" X = ");
Serial.print(temp);
if (myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTY_L_XL) != 0) {
errorsAndWarnings++;
}
Serial.print(" Y = ");
Serial.print(temp);
if (myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTZ_L_XL) != 0) {
errorsAndWarnings++;
}
Serial.print(" Z = ");
Serial.println(temp);
prevInterruptCount = interruptCount;
if(errorsAndWarnings>0){
Serial.println(errorsAndWarnings);
delay(1000);
}
}
if (interruptCount >= 100) {
// Trigger System OFF after 5 interrupts
setLedRGB(true,false, false); //
prevInterruptCount=-1000;
interruptCount=0;
Serial.println("100 interrupts hold 2s");
delay(2000);
}
return;
}
void setLedRGB(bool red, bool green, bool blue) {
if (!blue) { digitalWrite(LEDB, HIGH); } else { digitalWrite(LEDB, LOW); }
if (!green) { digitalWrite(LEDG, HIGH); } else { digitalWrite(LEDG, LOW); }
if (!red) { digitalWrite(LEDR, HIGH); } else { digitalWrite(LEDR, LOW); }
}