Skip to content

Instantly share code, notes, and snippets.

@giljr
Created September 3, 2017 18:30
Show Gist options
  • Save giljr/717866d5c1eb582b964b62124414aa13 to your computer and use it in GitHub Desktop.
Save giljr/717866d5c1eb582b964b62124414aa13 to your computer and use it in GitHub Desktop.
#include <Wire.h> // I2C library
//-------- REGISTER MAP for ADXL345 chip -----//
#define ACC (0x53) // ADXL345 ACC address
#define POWER_CTL (0x2D) // Power-saving features control
#define DATA_FORMAT (0x31) // Data format control
#define BW_RATE (0x2C) // Data rate and power mode control
#define DATAX0 (0x32) // First address for axis x
#define A_TO_READ (6) // num of bytes we are going to read each time
// two bytes for each axis
//-- Initialization - Turning on the ADXL345 ---//
/*by default the device is in +-2g range reading*/
void initAcc() {
/*Register 0x2D—POWER_CTL (Read/Write)
[D7] [D6] [D5] [D4] [D3] [D2] [D1 D0]
0 0 Link AUTO_SLEEP Measure* Sleep Wakeup */
writeTo(ACC, POWER_CTL, 0B00001000); // 1 places the part into measurement mode
/*Register 0x31—DATA_FORMAT (Read/Write)
[D7] [D6] [D5] [D4] [D3] [D2] [D1 D0]
SELF_TEST SPI INT_INVERT 0 FULL_RES* Justify Range** */
writeTo(ACC, DATA_FORMAT, 0B00001011); // +-16g range, right-justified, full resolution
/*Register 0x2C—BW_RATE (Read/Write)
[D7] [D6] [D5] [D4] [D3 D2 D1 D0]
0 0 0 LOW_POWER Rate*** */
writeTo(ACC, BW_RATE, 0B00101100); // 1100 -> ODR = 400Hz, 200 bandwidth = ODR/2
// (ODR = Output Data Rate); Table 7 & 8 DS
// same outputting data at each 10 ms (T = 1/ F)
}
//-------- Returns all data Function ---------//
void getAccelerometerData(int * result) {
int regAddress = DATAX0; // first axis-acceleration-data register on the ADXL345
byte buff[A_TO_READ]; // simultaneous reading as recommended by the data sheet
readFrom(ACC, regAddress, A_TO_READ, buff); // read the acceleration data from the ADXL345
// each axis reading comes in 10 bit resolution
// ie 2 bytes. Least Significat Byte first!!
//thus we are converting both bytes in to one int
result[0] = (((int)buff[1]) << 8) | buff[0];
result[1] = (((int)buff[3])<< 8) | buff[2];
result[2] = (((int)buff[5]) << 8) | buff[4];
}
//---------------- Set Up -------------------//
void setup()
{
Serial.begin(9600);
Wire.begin();
initAcc();
}
//---------------- Loop -------------------//
void loop()
{
int hx,hy,hz;
int acc[3];
getAccelerometerData(acc);
hx = acc[0];
hy = acc[1];
hz = acc[2];
Serial.print(" X=");
Serial.print(hx);
//Serial.print(hx/256); //in g
Serial.print(",");
Serial.print(" Y=");
Serial.print(hy);
//Serial.print(hy/256);//in g
Serial.print(",");
Serial.print(" Z=");
Serial.println(hz);
//Serial.println(hz/256);//in g
delay(50);
}
//---------------- 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 transmission
}
// reads num bytes starting from address
// register on ACC in to buff array
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
Wire.beginTransmission(DEVICE); // start transmission to ACC
Wire.write(address); // sends address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(DEVICE); // start transmission to ACC
Wire.requestFrom(DEVICE, num); // request 6 bytes from ACC
int i = 0;
while(Wire.available()) // ACC may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); // end transmission
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment