8 years, 11 months ago.

LSM303 frozen data

I've been trying all afternoon to get some data out of my LSM303 using an STM32L152 Nucleo board.

Finally I got all the I2C stuff down, managed to set some registers, verify their contents, etc. so I turn my attention to the data.

No matter what I do it seems, the data is stuck at the same values. When I power cycle the sensor I get changes in the magnetometer values, but then they stay the same. The outputs from the 3 accelerometer axes are always zero.

Any idea anyone? What am I doing wrong here?

#include "mbed.h"

#define ACC_ADDR 0x32
#define MAG_ADDR 0x3C

// ACC registers //
#define CTRL_REG1_A 0x20

#define ACC_X_L 0x28
#define ACC_X_H 0x29
#define ACC_Z_L 0x2C
#define ACC_Z_H 0x2D

// MAG registers //

#define CRA_REG_M 0x00 

#define MAG_X_H 0x03
#define MAG_X_L 0x04

#define MAG_TEMP_H 0x31 
#define MAG_TEMP_L 0x32

//------------------------------------
// Hyperterminal configuration
// 9600 bauds, 8-bit data, no parity
//------------------------------------

Serial pc(SERIAL_TX, SERIAL_RX);

DigitalOut led(LED1);
InterruptIn btn(USER_BUTTON);
I2C i2c(PB_9, PB_8);



int main()
{
 
    char data_write[2];
    char data_read[6] = {0,0};
 
    /* Turn on temp sensor */
    data_write[0] = CRA_REG_M ;
    data_write[1] = 0x90;
    int status = i2c.write(MAG_ADDR, data_write, 2, 0);
    if (status != 0) { // Error
        while (1) {
            led = !led;
            wait(0.2);
        }
    }
 
    int i = 0;
    int bytes = 6;
    int j;
    
    while (1) {


        // Read acceleromter data registers
        data_write[0] = ACC_X_L;
        i2c.write(ACC_ADDR, data_write, 1, 1); // no stop
        i2c.read(ACC_ADDR, data_read, bytes, 0);
 
        printf("\f");
        // Display result
        for(j=0;j<bytes;j++){
            printf("d: %x\n", data_read[j]);
        }
        printf("---- (%d)", i++);        
        
        led = 1;
        
        
        wait(0.1);

    }
 
}

Just did a quick check on the 'block update' register in case I messed something up there. It's definitely set as continuous update (default)

posted by Craig E 27 May 2015

1 Answer

8 years, 11 months ago.

Hi Craig

Page 25 of the ST Document for the LSM303DLHC Section 7.1.1 CTRL_REG1_A (20h) this is the control reg for the Accelerometer

By default this is all set to zero and hence accel. is in a powered down mode with all axis on

You probably need to set to 0x97 to get fastest rate and switch on all axis to read

You have defined this reg in your code but I don't see where you have set it to any value

Hope this helps

Regards

Martin