Skip to main content

Subsection 12.3.4 Part 4: The Data Format

The acceleration readings are put in the data registers of the accelerometer’s memory, at a register address we can look up in the LSM303AGR datasheet, which calls it a sub-address (SUB). Each axis’s reading is 16 bits wide (two bytes), and by default the reading is stored in them left-justified: the valid bits are at the top of the 2-byte word, and zeros fill in below. In low-power mode, for example, there are 8 valid bits, so only the top byte actually carries relevant information. Suppose the two bytes contain the reading 0x4000 (Figure 12.3.22): the top byte is 0x40 = 64. In low-power mode with a full-scale range of ±2 g, each step is 4 g / 2⁸ = 15.625 mg, so this reading corresponds to an acceleration of 64 × 15.625 mg = 1000 mg: one g.
Figure 12.3.22. The reading 0x4000 as it sits in the two data registers, all 16 bits. In low-power mode only the top byte carries valid bits (0x40 = 64), and the low byte is zeros: that is what left-justified means. At ±2 g in low-power mode each step is 15.625 mg, so 64 steps is 1000 mg, one g.
So far we read the data as unsigned, but acceleration has a direction, and the sensor’s output is therefore given as a two’s complement number. We have not needed this encoding until now, so here is a quick review: the top bit of a two’s complement number is worth its usual amount made negative, and every other bit keeps its usual value. The idea is easier to see in 4 bits (Figure 12.3.24): 0b0010 corresponds to a decimal value of 2 regardless of whether or not we are in two’s complement (since the top bit is 0), whereas the number 0b1110 corresponds to −8 + 4 + 2 = −2, because the top bit is worth −8 and the rest are ordinary. A number with its top bit set is therefore negative: the top bit contributes its negative weight, and the remaining bits are read as usual and add to it: 0b1110 is −8 + 0b110 = −8 + 6 = −2. In our 16-bit readings the top bit is bit 15, worth −2¹⁵.
Figure 12.3.24. Two’s complement, in 4 bits and in 16. Left: the number 0b1110, read with its bit weights: the top weight is −8 rather than +8, so the value is −8 + 4 + 2 = −2. Right: in the sensor’s 16-bit readings the same rule makes bit 15 worth −2¹⁵, and every other bit keeps its usual value.
We’ll work the whole conversion once, on the reading 0xC000, step by step. Assume we are in low-power mode at a full-scale range of ±2 g. In binary the reading is 0b1100 0000 0000 0000, and bit 15 is set, so the value is negative. The top byte carries the reading: 0xC0 = 0b1100 0000, whose top bit is worth −128 while the remaining bits are 0b100 0000 = 64, so the value is −128 + 64 = −64. Scaling by the step size gives −64 × 15.625 mg = −1000 mg: minus one g, comfortably inside the ±2000 mg the range allows.
The other two modes work the same way. Figure 12.3.27 shows all three writing their valid bits into the same 16-bit word, with one equation per mode. To convert a raw reading, shift the zero bits off the bottom, then multiply by that mode’s step size. Every one of the three simplifies to the same thing: \(a = 4g \cdot \mathrm{raw} / 2^{16}\text{.}\) This works because the valid bits always start at bit 15, so the shift and the step size always cancel to 2¹⁶. The same reading decoded this way gives the same answer: as a whole 16-bit word, 0xC000 is −32768 + 16384 = −16384, and 4 g × (−16384) / 2¹⁶ = −1000 mg, the number the top-byte route gave. When no mode is stated, this is the route to use: the whole 16-bit word, with the collapsed formula. Two things to keep in mind. The 4 g in this formula is the ±2 g range: choose a different full-scale range and the constant changes with it, so it is not always the same formula. And if we store the raw reading in a signed C data type such as int16_t (an integer, not an unsigned), then the whole two’s complement interpretation is automatically taken care of.
Figure 12.3.27. The three resolution modes write their valid bits into the same 16-bit word, always starting at bit 15: low-power mode fills 8 bits, normal 10, high-resolution 12, with zeros below. Bit 15 is the sign, worth −2¹⁵, so one formula converts the raw word in every mode: \(a = \mathrm{FS} \cdot \mathrm{raw} / 2^{16}\text{,}\) where FS is the full-scale range, 4 g here.
The units of the sensor’s output are g (1 g = 9.8 m/s²), so multiplying by 1000 gives milli-g, and for the ±2 g range the conversion is
a = (4 * ACCEL_raw * 1000) >> 16;              // in mg

// or, with names instead of magic numbers:
#define ACC_FS            4                    // full-scale range, in g
#define ACC_REGISTERWIDTH 16
#define MILLI             1000

a = (ACC_FS * ACCEL_raw * MILLI) >> ACC_REGISTERWIDTH;
The shift right by 16 does the divide by 2¹⁶ (shifting a signed value has one subtlety, which Section 12.6 spells out), and the #define names are the ones tomorrow’s test program, accel_test.c, uses. Note that a here is a signed integer, not an unsigned type, so the sign survives the arithmetic.

Activity 12.3.3. Convert a reading.

(a)

The sensor is in low-power mode on the ±2 g range, and the raw reading is 0xE000. On paper, convert it to milli-g: write out the binary, decide the sign, compute the value, and carry it to mg. Then check your answer for plausibility: is it inside the range?

(b)

If you finish early: the datasheet’s zero-g offset row says a flat, motionless board typically reads ±40 mg off, and its Min and Max columns allow ±80. Suppose your board averages +1032 mg lying flat and −968 mg lying flipped. What is its offset, and how would you correct future readings?
The accelerations along the three axes arrive in six data registers at successive sub-addresses, OUT_X_L_A at 0x28 through OUT_Z_H_A at 0x2D, and you can read all six bytes in one transfer, starting at 0x28, if you set one bit in the register address first. The datasheet explains that bit in §6.1.1 (p. 38), where its “slave” is our target, and “LSb” and “MSb” are the least and most significant bits:
After the start condition (ST) a slave address is sent, once a slave acknowledge (SAK) has been returned, an 8-bit sub-address (SUB) is transmitted: the 7 LSb represent the actual register address while the MSB enables address auto increment. If the MSb of the SUB field is ‘1’, the SUB (register address) is automatically increased to allow multiple data read/writes.
So if we want to read multiple registers successively in one transaction, starting at 0x28 (where the low byte of the x-acceleration is stored), we need to use 0x28 | (1<<7) for the register address from which to start reading. The accelerometer then advances to the next data register after every byte. This is Table 23 of Figure 10.4.4, the multi-byte read: the controller acknowledges each data byte (MAK) so the target serves the next register, and a final not-acknowledge (NMAK) ends the transfer.
lsm303agr.h declares a structure that holds one three-axis reading, and a pointer to a variable of this type is what lsm303_AccelReadRaw() fills in:
typedef struct {
    int16_t x;
    int16_t y;
    int16_t z;
} lsm303AccelData_s;
The six bytes come back low byte first: OUT_X_L_A at 0x28 holds the low byte of x and OUT_X_H_A at 0x29 the high byte. lsm303_AccelReadRaw(), which we’ll hand you, makes the six-byte read and then assembles each pair into one signed 16-bit value:
void lsm303_AccelReadRaw(lsm303AccelData_s *result) {
    uint8_t data[6] = {0};

    i2c1_memRead(LSM303_ADDRESS_ACCEL, LSM303_OUT_X_L_A | (1<<7), 6, &data[0]);

    result->x = ((int16_t) data[1] << 8) | ((int16_t) data[0]);
    result->y = ((int16_t) data[3] << 8) | ((int16_t) data[2]);
    result->z = ((int16_t) data[5] << 8) | ((int16_t) data[4]);
}
The line result->x = ((int16_t) data[1] << 8) | ((int16_t) data[0]); widens each byte, moves the high byte up eight places, ORs them, and stores the result in an int16_t, which is where bit 15 becomes the sign. This is a different use of the shift and OR operators than we have seen so far: here they build a wider signed value out of two array elements, rather than setting a bit inside a register.