LSM303

Have you got the greatest 48 bit multiplier ever conceived? Prove it - post your code here.

Moderator: phalanx

Post Reply
rpellizz
Posts: 2
Joined: Fri Dec 14, 2012 1:54 am

LSM303

Post by rpellizz » Fri Dec 14, 2012 1:59 am

Hi Guys,

I have been dealing with an strange behavior using the LSM303, I am using a pic18F46J50 connected to the LSM303, the magnetometer works great but the accelerometer does not work, it returns FF FF for all axis.


Any help will be great :)

Thanks


Here is my code



// read magnetometer
BYTE readMagnetometer( unsigned char reg)
{
BYTE ret;

StartI2C2();
WriteI2C2( LSM303_SAD_MAG_W );
WriteI2C2( reg );
StartI2C2();
WriteI2C2( LSM303_SAD_MAG_R );
ret = ReadI2C2();
IdleI2C2();
// NotAckI2C2();
StopI2C2();
return ret;
}


BYTE readAccelerometer( unsigned char reg)
{
BYTE ret;

StartI2C2();
WriteI2C2( LSM303_SAD_ACC_W ); //0x30
IdleI2C2();
WriteI2C2( reg );
StartI2C2();
WriteI2C2( LSM303_SAD_ACC_R ); //0x31
ret = ReadI2C2();
IdleI2C2();
NotAckI2C2();
StopI2C2();
return ret;
}


void init_LSM303(void)
{
OpenI2C2( MASTER, SLEW_OFF);

StartI2C2();
WriteI2C2( LSM303_SAD_ACC_W );
IdleI2C2();
WriteI2C2( LSM303_CTRL_REG1_A );// Normal power mode, all axes enabled
IdleI2C2();
WriteI2C2( 0x27 );// Normal power mode, all axes enabled
IdleI2C2();
StopI2C2();

StartI2C2();
WriteI2C2( LSM303_SAD_MAG_W ); // magnetometer addresses the chip
IdleI2C2();
WriteI2C2( LSM303_MR_REG_M ); // magnetometer register
IdleI2C2();
WriteI2C2( 0x00 ); // magnetometer register value Continuous conversion mode
IdleI2C2();
StopI2C2(); // stop condition I2C on bus

}


void main(void)
{
BYTE mxh, mxl, myh, myl, mzh, mzl, tempH, tempL;
unsigned char axh, axl, ayh, ayl, azh, azl;

BoardInit();
ConsoleInit();

init_LSM303();

Printf("\n\r Magnetometer ---------------------- \n\r ");
while(1)
{
axh = readAccelerometer(LSM303_OUT_X_H_A);
axl = readAccelerometer(LSM303_OUT_X_L_A);
ayh = readAccelerometer(LSM303_OUT_Y_H_A);
ayl = readAccelerometer(LSM303_OUT_Y_L_A);
azh = readAccelerometer(LSM303_OUT_Z_H_A);
azl = readAccelerometer(LSM303_OUT_Z_L_A);

mxh = readMagnetometer(LSM303_OUT_X_H_M);
mxl = readMagnetometer(LSM303_OUT_X_L_M);
myh = readMagnetometer(LSM303_OUT_Y_H_M);
myl = readMagnetometer(LSM303_OUT_Y_L_M);
mzh = readMagnetometer(LSM303_OUT_Z_H_M);
mzl = readMagnetometer(LSM303_OUT_Z_L_M);




Printf("\n\rX="); PrintChar(mxl); Printf(","); PrintChar(mxh);
Printf(",Y="); PrintChar(myl); Printf(","); PrintChar(myh);
Printf(",Z="); PrintChar(mzl); Printf(","); PrintChar(mzh);

Printf(" AX="); PrintChar(axl); Printf(","); PrintChar(axh);
Printf(" Y="); PrintChar(ayl); Printf(","); PrintChar(ayh);
Printf(" Z="); PrintChar(azl); Printf(","); PrintChar(azh);

}
}

rpellizz
Posts: 2
Joined: Fri Dec 14, 2012 1:54 am

Re: LSM303

Post by rpellizz » Sun Dec 16, 2012 1:35 pm

Problem Solved :)

I forgot to connect SA0_A to ground.

Hence the code above works great :)


Cheers

Post Reply