Hi,
I have a working BMA180, tested with a GUI of a multicopter flight controller to check the values (multiwii GUI)
I'm trying to write a code to read it in and export values over serial. I used some tutorials from Geeetech :
http://www.geeetech.com/wiki/index.php/BMA180_Triple_Axis_Accelerometer_Breakout
But when i do it, i keep having the same values whenever i move the sensor.
Here is my version of the code. I was hoping you'd give me a hint on what i'm doing wrong. I did similar codes with ITG3205 and i had no problem, although its datasheet was kinda clearer than the BMA180's, so addressing the registers and reading the data is a bit different. Might be why i got confused.
//BMA180 triple axis accelerometer sample code//
//
#include <Wire.h>
#define BMA180 0x40 //address of the accelerometer
#define PWR 0x0D
#define BWTCS 0x20
#define TCOZ 0X30
#define RANGE 0X35
#define DATA 0x02
//
int coords[3] = {0,0,0};
void setup()
{
Serial.begin(115200);
Wire.begin();
Acc_Init();
Serial.println("Sensors have been initialized");
}
//
void loop()
{
if (Serial.available() > 0)
{
Acc_Read(coords);
Serial.print("x=");
Serial.print(coords[0]);
Serial.print("mg");
Serial.print(",y=");
Serial.print(coords[1]);
Serial.print("mg");
Serial.print(",z=");
Serial.print(coords[2]);
Serial.println("mg");
delay(300); // slow down output
}
}
void Acc_Init()
//
{
byte temp[1];
byte temp1;
delay(10);
writeTo(BMA180, PWR, 1<<4);
delay(5);
readFrom(BMA180, BWTCS, 1 ,temp);
temp1=(temp[0]&0x0F) | (0x00 << 4); // set low pass filter to 10Hz (bits value = 0000xxxx)
writeTo(BMA180,TCOZ,temp1);
delay(5);
readFrom(BMA180, TCOZ, 1 ,temp);
temp1=(temp[0]&0xFC) | 0x00; // set mode_config to 0
writeTo(BMA180,TCOZ,temp1);
delay(5);
// range 8g
readFrom(BMA180, RANGE, 1 ,temp);
temp1=(temp[0]&0xF1) | (0x05 << 1);
writeTo(BMA180,RANGE,temp1);
delay(5);
}
//
void Acc_Read(int coordinates[])
{
// read in the 3 axis data, each one is 14 bits
// print the data to terminal
byte result[6];
readFrom(BMA180, DATA, 6, result);
coordinates[0]= (( result[0] | result[1]<<8)>>2) ;
coordinates[1]= (( result[2] | result[3]<<8)>>2);
coordinates[2]= (( result[4] | result[5]<<8)>>2);
}
//
//---------------- Functions--------------------
//Writes val to address register on ACC
void writeTo(int DEVICE, byte address, byte val)
{
Wire.beginTransmission(DEVICE); //start transmission to ACC
Wire.write(address); //send register address
Wire.write(val); //send value to write
Wire.endTransmission(); //end trnsmisson
}
//reads num bytes starting from address register in to buff array
void readFrom(int DEVICE, byte address , int num ,byte buff[])
{
Wire.beginTransmission(DEVICE); //start transmission to ACC
Wire.write(address); //send reguster address
Wire.endTransmission(); //end transmission
Wire.beginTransmission(DEVICE); //start transmission to ACC
Wire.requestFrom(DEVICE,num); //request 6 bits from ACC
int i=0;
while(Wire.available()) //ACC may abnormal
{
buff[i] =Wire.read(); //receive a byte
i++;
}
Wire.endTransmission(); //end transmission
}
The values i get are :
x=-640mg,y=-704mg,z=-768mg
x=-640mg,y=-704mg,z=-768mg
x=-640mg,y=-704mg,z=-768mg
and so on...
whatever the motion.