LIS3LV02 I2C Accelerometer code

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
adver
Posts: 6
Joined: Tue Jun 28, 2011 3:25 pm

LIS3LV02 I2C Accelerometer code

Post by adver »

Tested on MultiWiiV1_7

// ****************************
// LIS3LV02 I2C Accelerometer
// ****************************
#if defined(LIS3LV02)
#define LIS3A  0x3A // I2C adress: 0x3A (8bit)
static uint8_t rawADC_LIS3LV02[6];

void i2c_ACC_init(){
  i2c_rep_start(LIS3A);    // I2C write direction
  i2c_write(0x20);         // CTRL_REG1
  i2c_write(0xD7);         // 1101 0111 Pwr on, 160Hz

  i2c_rep_start(LIS3A);    // I2C write direction
  i2c_write(0x21);         // CTRL_REG2
  i2c_write(0x50);         // 0100 0000 Littl endian, 12 Bit, Boot

  acc_1G = 256;
  acc_25deg = 108; // = acc_1G * sin(25 deg)
  accPresent = 1;
}

void i2c_ACC_getADC(){
  TWBR = ((16000000L / 400000L) - 16) / 2; // change the I2C clock rate to 400kHz
  i2c_rep_start(LIS3A);     // I2C write direction SAD+W
  i2c_write(0x28+0x80);     // Start multiple read at reg 0x28 (+0x80 multiple flag) SUB
  i2c_rep_start(LIS3A+1);   // I2C read direction => 1 SAD+R
  for(uint8_t i = 0; i < 5; i++) {
    rawADC_LIS3LV02[i]=i2c_readAck();}
  rawADC_LIS3LV02[5]= i2c_readNak();
   
  accADC[ROLL]  =   (rawADC_LIS3LV02[3]<<8 | rawADC_LIS3LV02[2])/4;  // Y_L,Y_H
  accADC[PITCH] =  -(rawADC_LIS3LV02[1]<<8 | rawADC_LIS3LV02[0])/4;  // X_L,X_H
  accADC[YAW]   =  -(rawADC_LIS3LV02[5]<<8 | rawADC_LIS3LV02[4])/4;  // Z_L,Z_H
}
#endif


Alexinparis
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm

Re: LIS3LV02 I2C Accelerometer code

Post by Alexinparis »

thank you !

Alexinparis
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm

Re: LIS3LV02 I2C Accelerometer code

Post by Alexinparis »

adapted to dev version like this:

Code: Select all

#if defined(LIS3LV02)
#define LIS3A  0x3A // I2C adress: 0x3A (8bit)

void i2c_ACC_init(){
  i2c_writeReg(LIS3A ,0x20 ,0xD7 ); // CTRL_REG1   1101 0111 Pwr on, 160Hz
  i2c_writeReg(LIS3A ,0x21 ,0x50 ); // CTRL_REG2   0100 0000 Littl endian, 12 Bit, Boot
  acc_1G = 256;
}

void i2c_ACC_getADC(){
  TWBR = ((16000000L / 400000L) - 16) / 2; // change the I2C clock rate to 400kHz
  i2c_getSixRawADC(0xA4,0x28+0x80);

  ACC_ORIENTATION(  (rawADC[3]<<8 | rawADC[2])/4 ,
                   -(rawADC[1]<<8 | rawADC[0])/4 ,
                   -(rawADC[5]<<8 | rawADC[4])/4);
}
#endif

ziss_dm
Posts: 529
Joined: Tue Mar 08, 2011 5:26 am

Re: LIS3LV02 I2C Accelerometer code

Post by ziss_dm »

Hi Alex,
You probably also need ACC_Common(); :)

regards,
ziss_dm
Attachments
sensors.diff.zip
(558 Bytes) Downloaded 228 times

adver
Posts: 6
Joined: Tue Jun 28, 2011 3:25 pm

Re: LIS3LV02 I2C Accelerometer code

Post by adver »

I think i2c address is wrong ,
Alexinparis wrote:adapted to dev version like this:
i2c_getSixRawADC(0xA4,0x28+0x80);

i2c_getSixRawADC(LIS3A+1,0x28+0x80);

Alexinparis
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm

Re: LIS3LV02 I2C Accelerometer code

Post by Alexinparis »

sure, I was too enthusiast ;)

Post Reply