Hey guys,
I' ve a problem with an accelerometer, really it' s a magneto with a built-in accelerometer for tilt-compensated output but the two devices work in two different i2c address so I think it' s fine... I' m working with this device LSM303DLH, and I wrote this code to get the accelerometer output...
#include <Wire.h>
#define AXEL 0x18
//Funzione di setup
// Accelerometer address: 0011000b 0x18
// Magnetometer address: 0011110b 0x1E
// Registi di setup acceletometro:
// CTRL_REG1_A address: 0x20 parameters: 0x27
// CTRL_REG4_A address: 0x23 parameters: 0x40
//
// Registri di setup magnetometro:
// CRA_REG_M address: 0x00 parameters: 0x14
// MR_REG_M address: 0x02 parameters: 0x00
void lsmsetup(byte address, byte reg, byte setting);
void lsmsetup(byte address, byte reg, byte setting){
Wire.beginTransmission(address);
Wire.send(reg);
Wire.send(setting);
Wire.endTransmission();
}
//Funzione di lettura dei dati da un registro
int reading(int address, byte reg);
int reading(int address, byte reg){
byte value;
Wire.beginTransmission(address);
Wire.send(reg);
Wire.endTransmission();
Wire.requestFrom(address, 1);
while(Wire.available()){
value=Wire.receive();
}return value;
}
byte xlow, ylow, zlow, xhigh, yhigh, zhigh;
int X, Y, Z;
void setup(){
Serial.begin(9600);
Wire.begin();
lsmsetup(AXEL, 0x20, 0x27);
lsmsetup(AXEL, 0x23, 0x40);
lsmsetup(0x1E, 0x00, 0x14);
lsmsetup(0x1E, 0x02, 0x00);
}
void loop(){
xhigh= reading(AXEL, 0x28);
xlow= reading(AXEL, 0x29);
X = (xhigh << 8) + xlow;
yhigh= reading(AXEL, 0x2A);
ylow= reading(AXEL, 0x2B);
Y = ((yhigh << 8) | ylow);
zhigh= reading(AXEL, 0x2C);
zlow= reading(AXEL, 0x2D);
Z = ((zhigh << 8) | zlow);
Serial.print(X);
Serial.print('\t');
Serial.print(Y);
Serial.print('\t');
Serial.print(Z);
Serial.print('\n');
delay(100);
}
/* //Funzione di lettura multipla dei dati da un registro
int multireading(byte address, byte register);
int multireading(byte address, byte register){
int value;
int temp[1];
Wire.beginTransmission(address);
Wire.send(register);
Wire.send(register + 1);
Wire.endTransmission();
Wire.requestFrom(address, 2);
while(Wire.available()){
temp[0]=Wire.receive();
temp[1]=Wire.receive();
}
temp[0] << 8;
temp[0] += temp[1];
value= temp[0];
return value;
} */
I really don' t understand why the values I get are something which doesn' t match with real forces: these are some examples...
1136 368 49040
1136 384 49008
2176 1984 48256
5936 65280 51984
Respectly x, y and z axis; I don' t understand why it doesn' t give me negative numbers: I tried to join the two byte of z axis and it gives me "-something" but the "print" function says something like "51512". It' s strange, isn' t?
Here a quick start guide I followed to setup correctly the accelerometer http://www.sparkfun.com/datasheets/Sensors/Magneto/Tilt%20Compensated%20Compass.pdf
And here the datasheet:
http://www.sparkfun.com/products/10703
It' s my first time approaching with Arduino, but I' ve programmed some stuffs with C, I checked multiple times the code and I can' t find the error, someone can help me? Thank you,
Pentium