Skip to main content

Subsection 12.5.4 Part 5: Run It

Before you run accel_test.c, read it. It is the driver’s unit test, and every line it prints tells you something you will need when you are looking for a fault:
/* Shell program to test LSM303AGR driver code
 *
 * ENGS 28
 * Description:     Simple calls to the driver functions for my LSM303AGR accelerometer,
 *                  to test basic functionality.
 *
 * Target device:   Nucleo C031C6, Adafruit LSM303AGR breakout
 * Dependencies:    lsm303agr, i2c.c, uart.c
 */

#include <stdio.h>
#include "ES28.h"
#include "uart.h"
#include "lsm303agr.h"
#include "i2c.h"

#define ACC_FS 4
#define ACC_REGISTERWIDTH 16
#define MILLI 1000

/* CODE */
int main(void) {
    // Setup code (run once) goes here:
    int16_t accel_x = 0, accel_y = 0, accel_z = 0;
    lsm303AccelData_s accel_raw = {0};

    i2c1_init();
    uart2_init();
    printf("LSM303AGR test program \n\r");

    if( lsm303_AccelInit() ) {
        printf("Accelerometer initialized! \n\r");
    }
    else {
        printf("Could not connect to accelerometer \n\r");
    }

    printf("Control Register 1 = 0x%x \n\r", lsm303_AccelRegisterRead(LSM303_CTRL_REG1_A));
    printf("Control Register 4 = 0x%x \n\r", lsm303_AccelRegisterRead(LSM303_CTRL_REG4_A));

    while(1) {
        lsm303_AccelReadRaw(&accel_raw);
        accel_x = (ACC_FS * accel_raw.x * MILLI) >> ACC_REGISTERWIDTH;
        accel_y = (ACC_FS * accel_raw.y * MILLI) >> ACC_REGISTERWIDTH;
        accel_z = (ACC_FS * accel_raw.z * MILLI) >> ACC_REGISTERWIDTH;

        printf("accel_raw_x=0x%x, accel_raw_y=0x%x, accel_raw_z=0x%x \n\r", accel_raw.x, accel_raw.y, accel_raw.z);
        printf("accel_x=%d mg, accel_y=%d mg, accel_z=%d mg \n\n\r", accel_x, accel_y, accel_z);

        delay_ms(2000);
    }

    return 0;       /* never reached */
}
The program checks lsm303_AccelInit()’s return value and prints which way it went. It also reads both control registers back and prints them, so you can check that the values you worked out were actually written. The scaling line is yesterday’s formula in C, with the #defines from yesterday and a right shift by 16 where the formula said divide by 2¹⁶ (the arithmetic behind the shift, including what happens to the large intermediate product, is in Subsection 12.6.4). Each pass through the loop prints the raw readings in hex and the converted milli-g values right underneath. Figure 12.5.13 shows what a successful run looks like.
Figure 12.5.13. CoolTerm’s output from a successful run of accel_test.c: the opening line, the initialization message, the two control-register readbacks, and then a raw and converted line pair every two seconds. (Placeholder; the real screenshot is coming.)

Activity 12.5.5. Stream your first acceleration data.

(a)

Build, run, and open CoolTerm. You should see the LSM303AGR test program line, Accelerometer initialized!, the two control-register readbacks, and then a new reading every two seconds.

(b)

Test it against gravity: flat on the table, z should read approximately +1000 mg. Flip the board over and the sign flips. Rotate the board to move gravity onto x and then y.

(c)

While the board lies flat, pick one printed line and check the z pair: does the raw hex value convert to the milli-g value printed beside it, by yesterday’s formula?
If the program runs but the output is not what you expect, follow these steps to narrow down where the error might be:
  • If Could not connect to accelerometer appears, the WHO_AM_I check did not return 0x33: double-check your wiring against Figure 12.2.4 and try again.
  • If the connection succeeds but a control-register readback does not show the value you wrote into the control register, the write path is suspect, which is the new code you wrote today: check that your lsm303_AccelRegisterWrite() makes a single i2c1_memWrite() call, with the device address, the register address, a count of 1, and a pointer to the data byte.
  • If everything prints and both register readbacks match your initialization, but the acceleration values are nowhere near 1000 mg at rest on any axis, the readings are arriving, and the values with which you initialized the control registers are the suspect: the scaling in accel_test.c assumes ±2 g, so a different FS[1:0] scales every axis by the ratio of the ranges. Check each field of your two control-register values against the datasheet.
  • If none of those three finds it, put the logic analyzer back on the bus as you did on Tuesday and watch the transaction itself.