AnsweredAssumed Answered

Reading data from LSM303AGR

Question asked by Ester Verlato on Jun 22, 2017
Latest reply on Sep 4, 2017 by Miroslav B

Hi, I'm new here and I am new at working on mems.

I have a LPCXpresso1549 evaluation board and a Steval-MKI172v1 mems that implements a lsm303agr chip. 

I'm working on mbed platform and I need to read values from accelerometer but I tried a lot and cannot read anything.

 

I connect pins for SCL and SDA, 3V3 to the VDD pin, the GND, and two digital pins to the CS and CS_A (chip select on lsm303agr). 

I don't know what address my lsm303 has and I can't understand what address I need to write on registers

 

 

My code is:

 

#include "mbed.h"

 

#define ACC 0x00

#define CTRL_REG1_A 0x20
#define CTRL_REG2_A 0x21
#define CTRL_REG3_A 0x22
#define CTRL_REG4_A 0x23
#define CTRL_REG5_A 0x24

#define OUT_X_L_A 0x28
#define OUT_X_H_A 0x29
#define OUT_Y_L_A 0x2A
#define OUT_Y_H_A 0x2B
#define OUT_Z_L_A 0x2C
#define OUT_Z_H_A 0x2D

 

typedef struct Plane {
      float x, y, z;
} Plane;

 

Plane a;
//Plane a_max;
//Plane a_min;

 

Serial pc(USBTX, USBRX); // tx, rx

 

I2C i2c(P0_23, P0_22);
DigitalOut chip_sel_a(P0_27);
DigitalOut chip_sel_m(P0_28);

 

void LSM303_write (int data, int reg_address) {

 

   char out[2] = { reg_address, data };
   i2c.write( ACC, out, 2 );
}

 

int main() {

 

//setup iniziale
chip_sel_a = 1;
chip_sel_m = 0;

 

LSM303_write(0x57, CTRL_REG1_A);
LSM303_write(0x81, CTRL_REG4_A); 
LSM303_write(0x00, CTRL_REG2_A);
LSM303_write(0x00, CTRL_REG3_A);
LSM303_write(0x00, CTRL_REG5_A);

 

   while(1) {

 

      char data[1] = { OUT_X_L_A | (1<<7)}; //mette un 1 in testa all'indirizzo per fare read multipla
      char out[6] = {0,0,0,0,0,0};
      i2c.write( ACC, data,1);
      i2c.read( ACC, out, 6);

 

      a.x = short( (((short)out[1]) << 8) | out[0] );
      a.y = short( (((short)out[3]) << 8) | out[2] );
      a.z = short( (((short)out[5]) << 8) | out[4] );

 

      pc.printf("asse x: %f\n", a.x);
      pc.printf("asse y: %f\n", a.y);
      pc.printf("asse z: %f\n\n", a.z);


      wait(2);
      }
   }   

 

 

Could someone help me?  

Outcomes