cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

LSM303DLM interfacing to the PIC mcro controller

shekar
Associate
Posted on February 12, 2014 at 10:54

I done I2c Communication with the LSM303DLM Module and am setting the normal power mode and ODR@50HZ and enabling xyz axises of Accelerometer and magnetometer and am reading the accelerometer and magnetometer readings but am getting 0's and 1's.How did aget data when sensor is in motion and changing directions.  The following is the code i written to access the LSM registers.

Please any one help me to get dynamic data from the sensor.

#define LSM_SDA PIN_C4

#define LSM_SCL PIN_C3

#use i2c(I2C2)

int8 memxl,memxh,memyl,memyh,memzl,memzh,mxh,mxl,myh,myl,mzh,mzl;

typedef short int i16_t;

  i16_t AXIS_X;

  i16_t AXIS_Y;

  i16_t AXIS_Z;

  i16_t M_AXIS_X;

  i16_t M_AXIS_Y;

  i16_t M_AXIS_Z;

 

void LSM_INIT()

{

 

   i2c_start();  //start

   i2c_write(0x30);      // WR to LSM_A (SA0=0)

   i2c_write(0x20);      // CTRL_REG1_A

   i2c_write(0x27);      // data

   i2c_stop();

   delay_ms(20);

   

   i2c_start();

   i2c_write(0x30);      // WR to LSM_A (SA0=0)

   i2c_write(0x28);      //OUT_X_L_A

   i2c_start();

   i2c_write(0x31);      // RD from OUT_X_L_A

   memxl= i2c_read(0x28); // Read DATA

   i2c_stop();

   

   

   i2c_start();

   i2c_write(0x30);      // WR to LSM

   i2c_write(0x29);      // OUT_X_H_A

   i2c_start();

   i2c_write(0x31);      // RD from OUT_X_H_A

   memxh= i2c_read(0x29); // Read DATA

   i2c_stop();

   

   AXIS_X = (i16_t)( (memxh << ๐Ÿ˜Ž | memxl );

   

   i2c_start();

   i2c_write(0x30);      // WR to LSM

   i2c_write(0x2A);      // OUT_Y_L_A

   i2c_start();

   i2c_write(0x31);      // RD from OUT_Y_L_A

   memyl= i2c_read(0x2A); // Read DATA

   i2c_stop();

   

   i2c_start();

   i2c_write(0x30);      // WR to LSM

   i2c_write(0x2B);      // OUT_Y_H_A

   i2c_start();

   i2c_write(0x31);      // RD from OUT_Y_H_A

   memyh= i2c_read(0x2B); // Read DATA

   i2c_stop();

    AXIS_Y = (i16_t)( (memyh << ๐Ÿ˜Ž | memyl );

   

   i2c_start();

   i2c_write(0x30);      // WR to LSM

   i2c_write(0x2C);      // OUT_Z_L_A

   i2c_start();

   i2c_write(0x31);      // RD from OUT_Z_L_A

   memzl= i2c_read(0x2C); // Read DATA

   i2c_stop();

   

   i2c_start();

   i2c_write(0x30);      // WR to LSM

   i2c_write(0x2D);      // OUT_Z_H_A

   i2c_start();

   i2c_write(0x31);      // RD from OUT_Z_H_A

   memzh= i2c_read(0x2D); // Read DATA

   i2c_stop();  

  AXIS_Z = (i16_t)( (memzh << ๐Ÿ˜Ž | memzl );

 

   i2c_start();

   i2c_write(0x3C);      // WR to LSM

   i2c_write(0x00);      // CRA_REG_M

   i2c_write(0x00);      // data

   i2c_stop();

   

   i2c_start();

   i2c_write(0x3C);      // WR to LSM

   i2c_write(0x02);      // MR_REG_M

   i2c_write(0x00);      // data

   i2c_stop();

   

   i2c_start();

   i2c_write(0x3C);      // WR to LSM-M

   i2c_write(0x03);      // OUT_X_H_M  

   i2c_start();

   i2c_write(0x3D);      // RD from OUT_X_H_M  

   mxh= i2c_read(0x03); // Read DATA

   i2c_stop();

   

   

   i2c_start();

   i2c_write(0x3C);      // WR to LSM

   i2c_write(0x04);      // OUT_X_L_M  

   i2c_start();

   i2c_write(0x3D);      // RD from OUT_X_L_M

   mxl= i2c_read(0x04); // Read DATA

   i2c_stop();

   

   M_AXIS_X = (i16_t)( (mxh << ๐Ÿ˜Ž | mxl );

   

   i2c_start();

   i2c_write(0x3C);      // WR to LSM-M

   i2c_write(0x05);      // OUT_Y_H_M

   i2c_start();

   i2c_write(0x3D);      // RD from OUT_Y_H_M  

   myh= i2c_read(0x05); // Read DATA

   i2c_stop();

   

   

   i2c_start();

   i2c_write(0x3C);      // WR to LSM

   i2c_write(0x06);      // OUT_Y_L_M

   i2c_start();

   i2c_write(0x3D);      // RD from OUT_Y_L_M  

   myl= i2c_read(0x06); // Read DATA

   i2c_stop();

   

   M_AXIS_Y = (i16_t)( (myh << ๐Ÿ˜Ž | myl );

   

   i2c_start();

   i2c_write(0x3C);      // WR to LSM-M

   i2c_write(0x07);      // OUT_Z_H_M

   i2c_start();

   i2c_write(0x3D);      // RD fromOUT_Z_H_M  

   mzh= i2c_read(0x07); // Read DATA

   i2c_stop();

   

   

   i2c_start();

   i2c_write(0x3C);      // WR to LSM

   i2c_write(0x08);      // OUT_Z_L_M

   i2c_start();

   i2c_write(0x3D);      // RD from OUT_Z_L_M  

   mzl= i2c_read(0x08); // Read DATA

   i2c_stop();

   

   M_AXIS_Z = (i16_t)( (mzh << ๐Ÿ˜Ž | mzl );

    fprintf(PC,''AX=%5d AY=%5d  AZ=%5d   MX=%5d MY=%5d  MZ=%5d  '',AXIS_X,AXIS_Y,AXIS_Z,M_AXIS_X,M_AXIS_Y,M_AXIS_Z);

 

   

0 REPLIES 0