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

LIS3LV02 I2C Accelerometer code

Postby adver » Tue Jun 28, 2011 3:32 pm

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

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

Re: LIS3LV02 I2C Accelerometer code

Postby Alexinparis » Tue Jun 28, 2011 11:53 pm

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

Re: LIS3LV02 I2C Accelerometer code

Postby Alexinparis » Wed Jun 29, 2011 12:02 am

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
Alexinparis
 
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm

Re: LIS3LV02 I2C Accelerometer code

Postby ziss_dm » Wed Jun 29, 2011 2:12 am

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

regards,
ziss_dm
Attachments
sensors.diff.zip
(558 Bytes) Downloaded 132 times
ziss_dm
 
Posts: 529
Joined: Tue Mar 08, 2011 5:26 am

Re: LIS3LV02 I2C Accelerometer code

Postby adver » Wed Jun 29, 2011 11:16 am

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);
adver
 
Posts: 6
Joined: Tue Jun 28, 2011 3:25 pm

Re: LIS3LV02 I2C Accelerometer code

Postby Alexinparis » Wed Jun 29, 2011 10:36 pm

sure, I was too enthusiast ;)
Alexinparis
 
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm


Return to Software development

Who is online

Users browsing this forum: No registered users and 13 guests