SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Jancio
#182114
Hi all,

I think I found a bug into the SFE_LSM9DS0 library for Arduino into the setAccelABW routine.

This is the original code:
Code: Select all
void LSM9DS0::setAccelABW(accel_abw abwRate)
{
	// We need to preserve the other bytes in CTRL_REG2_XM. So, first read it:
	uint8_t temp = xmReadByte(CTRL_REG2_XM);
	// Then mask out the accel ABW bits:
	temp &= 0xFF^(0x3 << 7);
	// Then shift in our new ODR bits:
	temp |= (abwRate << 7);
	// And write the new register value back into CTRL_REG2_XM:
	xmWriteByte(CTRL_REG2_XM, temp);
}
In my opinion instead of:
temp &= 0xFF^(0x3 << 7);
temp |= (abwRate << 7);

Should be:
temp &= 0xFF^(0x3 << 6);
temp |= (abwRate << 6);

These lines of codes are masking and then setting the new accelerometer anti-alias filter bandwidth bits. But the original code is setting the wrong ones.

Shifting the "2 digits (0x3)" by 7 position will create a register overflow masking the wrong bits and missing one of the two bits that we want to setup ABW1 and ABW0. The "two digits" should be shifted only by 6 positions in order to get the desired result.

I hope that this will help to avoid some headaches.

Regards,

Maximilian

Check the LSM9DS0's datasheet at page 56 for more details.
https://cdn.sparkfun.com/assets/f/6/1/f/0/LSM9DS0.pdf
User avatar
By DanV
#182115
I can't see how you could be wrong - shifting the lower 2 bits can only be done 6 places before you run into trouble.
Wonder if it's some leftover crap where the author was shifting a 0x01 seven times to set ABW1 and through multiple iterations of coding ended up shifting 0x03 without properly fixing the left shift.

I'd say it is a good catch by you!

Now, just need to propagate that back to the library ...
By Jancio
#182149
Thanks DanV,

I contacted the developer of the library (Jimb0) and he made the adjustments already.

Happy hacking everyone!

Maximilian

PS: Thanks Jimb0