I've was trying all afternoon yesterday to get some data out of my LSM303DLHC using an STM32L152 Nucleo board.
Finally I got all the I2C stuff down, managed to set some registers, verify their contents, etc. so I turned my attention to the data coming out.
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 until I cycle it again. The outputs from the 3 accelerometer axes are always zero.
I've done a read of the 'block update' register and confirmed the IC it set to do continuous updates.
I have verified on a scope that what I'm receiving is correct to the protocol.
I'm using this breakout board: http://www.aliexpress.com/item/1-pcs-GY-511-LSM303DLHC-Module-E-Compass-3-Axis-Accelerometer-3-Axis-Magnetometer-Module-Sensor/1956617486.html
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);
}
}