Just received the LSM303DLH breakout board today. Hooked it up to my dsPIC and I’m not able to read anything from the magnetometer. It’s not clear if the configuration commands are being read at all. When querying the data registers it returns 0xFF for each. I’m powering the board at 3.3V. Set to normal operation at 75Hz output. I’m attempting to read the registers at 50Hz. The DRDY pin is at 1.8V, so it seems like something may be happening. I’ve scoped the I2C lines and the address and register bytes look clean, the pins then stay high. Code is below for reference…
Also, I’m able to communicate with the accelerometer just fine. The accelerometer output is a bit odd in that the results are shifted 4 bits from what I expect. At ±8g resolution the 1g values are about 4096 instead of 256… Has anyone else seen this? It’s not indicated in the datasheet, but is easy enough to deal with.
Best,
Geoff
Configuration:
#define MAGREAD 0x3D
#define MAGWRITE 0x3C
//////////////////////////////////////
// Configure LSM303DLH Magnetometer //
//////////////////////////////////////
void ConfigureMag(void) {
StartI2C();
IdleI2C();
MasterWriteI2C(MAGWRITE);
IdleI2C();
MasterWriteI2C(0x00); // Set pointer to configuration register
IdleI2C();
MasterWriteI2C(0b00011000); // Set to 75 Hz data ouput rate and normal configuration
IdleI2C();
StopI2C();
IdleI2C();
StartI2C();
IdleI2C();
MasterWriteI2C(MAGWRITE);
IdleI2C();
MasterWriteI2C(0x01); // Set pointer to configuration B register
IdleI2C();
MasterWriteI2C(0b10000000); // Set minimum gain (+- 4.0 Gauss range)
IdleI2C();
StopI2C();
IdleI2C();
StartI2C();
IdleI2C();
MasterWriteI2C(MAGWRITE);
IdleI2C();
MasterWriteI2C(0x02); // Mode register
IdleI2C();
MasterWriteI2C(0b00000000); // Continuous conversion mode
IdleI2C();
StopI2C();
IdleI2C();
}
Read (called at 50Hz):
/////////////////////////////////
// Read LSM303DLH magnetometer //
/////////////////////////////////
void ReadMag(StateVector *svptr) {
static unsigned char xH;
static unsigned char xL;
static unsigned char yH;
static unsigned char yL;
static unsigned char zH;
static unsigned char zL;
StartI2C();
IdleI2C();
MasterWriteI2C(MAGWRITE); // Write address
IdleI2C();
MasterWriteI2C(0x03); // First data register
IdleI2C();
RestartI2C();
IdleI2C();
MasterWriteI2C(MAGREAD); // Read address
IdleI2C();
xH = MasterReadI2C(); // x-axis high byte
AckI2C();
IdleI2C();
xL = MasterReadI2C(); // x-axis low byte
AckI2C();
IdleI2C();
yH = MasterReadI2C(); // y-axis high byte
AckI2C();
IdleI2C();
yL = MasterReadI2C(); // y-axis low byte
AckI2C();
IdleI2C();
zH = MasterReadI2C(); // z-axis high byte
AckI2C();
IdleI2C();
zL = MasterReadI2C(); // z-axis low byte
NotAckI2C();
IdleI2C();
StopI2C();
IdleI2C();
svptr->magX = xL|(xH << 8); // x-axis value
svptr->magY = yL|(yH << 8); // y-axis value
svptr->magZ = zL|(zH << 8); // z-axis value