// ****************************
// 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
LIS3LV02 I2C Accelerometer code
LIS3LV02 I2C Accelerometer code
Tested on MultiWiiV1_7
-
- Posts: 1630
- Joined: Wed Jan 19, 2011 9:07 pm
Re: LIS3LV02 I2C Accelerometer code
thank you !
-
- Posts: 1630
- Joined: Wed Jan 19, 2011 9:07 pm
Re: LIS3LV02 I2C Accelerometer code
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
Re: LIS3LV02 I2C Accelerometer code
Hi Alex,
You probably also need ACC_Common();
regards,
ziss_dm
You probably also need ACC_Common();
regards,
ziss_dm
- Attachments
-
- sensors.diff.zip
- (558 Bytes) Downloaded 239 times
Re: LIS3LV02 I2C Accelerometer code
I think i2c address is wrong ,
i2c_getSixRawADC(LIS3A+1,0x28+0x80);
Alexinparis wrote:adapted to dev version like this:
i2c_getSixRawADC(0xA4,0x28+0x80);
i2c_getSixRawADC(LIS3A+1,0x28+0x80);
-
- Posts: 1630
- Joined: Wed Jan 19, 2011 9:07 pm
Re: LIS3LV02 I2C Accelerometer code
sure, I was too enthusiast