Hi there,
I’m trying to read accelerometer data from an adxl362 (datasheet here) but I’m experiencing two problems.
- When I set registers, they return different values upon reading.
- The FIFO data does not have the format specified in the datasheet.
Problem #1
Here is some sample code for problem #1. I am setting a handful of registers, and I would expect to retrieve the values I just set.`// Configure
cs ← hardware.pin5;
spi ← hardware.spi189;
spi.configure(0, 4000);
cs.configure(DIGITAL_OUT);
cs.write(1);
function readOneRegister(regAddress) {
cs.write(0);
spi.write(“\x0b”);
spi.write(regAddress);
local regValue = blob(1);
regValue.writeblob(spi.readblob(1));
cs.write(1);
return regValue[0];
}
function writeOneRegister(regAddress, regValue) {
cs.write(0);
spi.write("\\x0a");
spi.write(regAddress);
spi.write(regValue);
cs.write(1);
}
// Configure
writeOneRegister(“\x1f”, “\x52”); // soft reset
imp.sleep(0.1);
local temp = readOneRegister(“\x2d”);
server.log(format(“2d %02x”, temp));
writeOneRegister(“\x2d”, “\x22”); // turn measurement on, ultra low noise
imp.sleep(0.1);
temp = readOneRegister(“\x2d”);
server.log(format(“2d %02x”, temp));
writeOneRegister(“\x2c”, “\x13”);
imp.sleep(0.1);
temp = readOneRegister(“\x2c”);
server.log(format(“2c %02x”, temp));
`
When I run this code, the output varies between these two:
2014-08-30 17:11:37 UTC-7 [Status] Device Booting; 3.37% program storage used 2014-08-30 17:11:37 UTC-7 [Device] 2d 00 2014-08-30 17:11:37 UTC-7 [Device] 2d 20 2014-08-30 17:11:37 UTC-7 [Device] 2c 1f 2014-08-30 17:12:43 UTC-7 [Status] Downloading new code; 3.37% program storage used 2014-08-30 17:12:44 UTC-7 [Device] 2d 00 2014-08-30 17:12:44 UTC-7 [Device] 2d 3f 2014-08-30 17:12:44 UTC-7 [Device] 2c 1f
You can see that the soft reset seems to be doing it’s job, and 2d reads as 00 initially. However 2c always reads as 1f after writing, and 2d flips between 20 and 3f seemingly at random.
Am I doing something wrong in reading or writing? I added pauses to make sure the data has time to be written, but no luck.
Problem #2
According to the datasheet, the FIFO data should be returned as pairs of bytes, with the most significant byte second. After re-ordering those bytes, this is supposed to be the bit pattern:[15-14] Axis identifier (00 = x, 01 = y, 10 = z) [13-12] Sign extension (this should be unnecessary since [0-11] is already two's complement?) [11-0] Two's complement acceleration measurement
If I add this function to the code referenced above and run it, what I get doesn’t match that output.
`// Read X/Y/Z
function readAcc() {
cs.write(0);
spi.write("\\x0d"); // FIFO mode
local s = blob(6);
s.writeblob(hardware.spi189.readblob(6));
server.log(format("raw %02x %02x", s[5], s[4]));
local x = ((s[1] << 28) + (s[0] << 20)) >> 20;
local y = ((s[3] << 28) + (s[2] << 20)) >> 20;
local z = ((s[5] << 28) + (s[4] << 20)) >> 20;
server.log(format("value %i / %i / %i", x, y, z));
cs.write(1);
imp.wakeup(0.5, readAcc);
}`
When I look at the raw output, bits [15-12] are always zero. The following bits do seem to correspond with data numbers.
So question is, am I doing something wrong, or does the output just not match the datasheet?
Thanks guys!