Hi folks,
I am new to both i2c and interfacing with accelerometers. I feel that my hardware set up is A ok, and I have at one point gotten ONLY the x-axis to work, however for some reason I can't get any of them to work now.
I know that I am successfully accessing the register addresses on the bma180 accelerometer device as it is returning "3" from the chip_id method (which I believe is the correct id) however, when following the same format to access the other registers for the x, y, z the values I receive are 0.
My goal is to simply read the x, y, z output from the device.
Code:
#include <Wire.h>
#define DEVICE 0x40
#define CHIP_ID 0x00
#define R_ACC_X_LSB 0x02
#define R_ACC_X_MSB 0x03
#define R_ACC_Y_LSB 0x04
#define R_ACC_Y_MSB 0x05
#define R_ACC_Z_LSB 0x06
#define R_ACC_Z_MSB 0x07
#define R_CTRL_REG0 0x0D
#define R_BW_TCS 0x20
#define R_CUST_DATA1 0x2C
#define R_OFFSET_LSB1 0x35
int temp, result, error, id;
void setup()
{
Wire.begin();
Serial.begin(9600);
initializeBMA180();
}
void loop()
{
// get values from accelerometer and print them out
int x = getAccel(0x03);
int y = getAccel(0x05);
int z = getAccel(0x07);
Serial.print(" x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.println(z);
delay(100);
}
void initializeBMA180()
{
checkID();
enableWrite();
setFilter();
setRange();
}
int checkID()
{
// connect to address 0x00 and determine chip id
Wire.beginTransmission(DEVICE);
Wire.send(0x00);
error = Wire.endTransmission();
Wire.requestFrom(DEVICE, 1);
while(Wire.available()) {
id = Wire.receive();
}
Serial.println("\n \n");
Serial.print("Chip ID: ");
Serial.print(id);
Serial.print(" Result: ");
Serial.println(error);
return id;
}
void enableWrite()
{
//Connect to the ctrl_reg0 register and set the ee_w bit to enable writing
Wire.beginTransmission(DEVICE);
Wire.send(0x0D);
Wire.send(B0001);
result = Wire.endTransmission();
Serial.print("Enable Write Result: ");
Serial.println(result);
delay(10);
}
void setFilter()
{
// Connect to the bw_tcs register and set the filtering level to 10hz
Wire.beginTransmission(DEVICE);
Wire.send(0x20);
Wire.send(B00001000);
result = Wire.endTransmission();
Serial.print("Set Filter Result: ");
Serial.println(result);
delay(10);
}
void setRange()
{
Wire.beginTransmission(DEVICE);
Wire.send(0x35);
Wire.send(B0100);
result = Wire.endTransmission();
Serial.print("Set Range Result: ");
Serial.println(result);
delay(10);
}
int getAccel(byte accel_data)
{
int data = getRegister(accel_data);
return data;
}
int getRegister(byte registerAddr)
{
// pass in address of x, y, and z to determine their value
Wire.beginTransmission(DEVICE); // start transmission
Wire.send(registerAddr); // request address
Wire.endTransmission(); // queue bytes
Wire.requestFrom(DEVICE,1); // request numbytes from queue
while(Wire.available() == 0); // wait for bytes to arrive
int temp = Wire.receive(); // receive bytes
return temp;
}
Setup:
Thank you!