I would like to set the ODR in the sensor but when I add the call setOutputDataRate() with any parameter between 0 and 15 the accelerometer reading never changes. I tried it for different calls to setRange() not still doesn’t work.
Any idea where to start?
There’s a bug in the file SparkFun_Qwiic_KX13X.cpp. I have updated setOutputDataRate(uint8_t rate) to take care of it.
Before the fix, any call to the function caused the sensor to never be ready to be read.
//Address: 0x21, bits[3:0] - default value is 0x06 (50Hz)
//Sets the refresh rate of the accelerometer's data.
// 0.781 * (2 * (n)) derived from pg. 26 of Technical Reference Manual <----incorrect expression
bool QwiicKX13xCore::setOutputDataRate(uint8_t rate){
if( rate < 0 | rate > 15 )
return false;
uint8_t accelState = readAccelState(); // Put it back where we found it.
accelControl(false); // Can't adjust without putting to sleep
KX13X_STATUS_t returnError;
returnError = writeRegister(KX13X_ODCNTL, 0xF0, rate, 0);
if( returnError == KX13X_SUCCESS ) {
accelControl(accelState); // added this to fix the ERROR
return true;
}
else
return false;
// if( returnError == KX13X_SUCCESS ){ // These lines should be removed.
// accelControl(accelState);
// return true;
// }
// else
// return false;
}
In addition, the function readOutputDataRate() was calculating the ODR in Hz incorrectly. It was calculating
0.781 * (2 * (n))
``` instead of ```
0.781 * pow(2, n)
Please check the code below:
// Address:0x21 , bit[3:0]: default value is: 0x06 (50Hz)
// Gets the accelerometer's output data rate.
float QwiicKX13xCore::readOutputDataRate(){
uint8_t tempRegVal;
readRegister(&tempRegVal, KX13X_ODCNTL);
tempRegVal &= 0x0F;
tempRegVal = (float)tempRegVal;
return (0.781 * pow(2, tempRegVal));
}
A.