floitsch 03/14/2023 03:22 PMFor reading the actual values you have 2 options:
- read all 6 bytes (2 for each axis) in one go, or
- read two bytes each.
The first is a bit more efficient, but the latter is easier to write.
Here are the constants you need:
ACCEL_XOUT_H ::= 0x3B
ACCEL_XOUT_L ::= 0x3C
ACCEL_YOUT_H ::= 0x3D
ACCEL_YOUT_L ::= 0x3E
ACCEL_ZOUT_H ::= 0x3F
ACCEL_ZOUT_L ::= 0x40
// Read 6 bytes in one go:
accel_data := registers.read_bytes ACCEL_XOUT_H 6
accel_x := accel_data[0] << 8 | accel_data[1]
accel_y := accel_data[2] << 8 | accel_data[3]
accel_z := accel_data[4] << 8 | accel_data[5]
or
// Read each value independently:
accel_x := registers.read_i16_be ACCEL_XOUT_H_
accel_y := registers.read_i16_be ACCEL_YOUT_H_
accel_z := registers.read_i16_be ACCEL_ZOUT_H_
Note that we use read_i16_be
which basically means: "read an integer with 16 bits, in big-endian format". And "big-endian" means that the big part (that is, most significant, or high part) is first.
If you look at the description of these registers (page 29) you can see that the obtained value's meaning depends ot the AFS_SEL that we set in the configuration. With the highest precision (when setting the config-register to 0), the value must be divided by 16384. With the lowest accuracy (but highest range), it must be divided by 2048 (as you do in your code).
You can then start a loop:
while true:
accel_x := registers.read_i16_be ACCEL_XOUT_H_
accel_y := registers.read_i16_be ACCEL_YOUT_H_
accel_z := registers.read_i16_be ACCEL_ZOUT_H_
print "x: $(accel_x / 2048)"
print "y: $(accel_y / 2048)"
print "z: $(accel_z / 2048)"
sleep --ms=1_000