User Manual

122
if(acc_xyz.y > 32767){ // Set the value of y as no more than 0x7FFF
acc_xyz.y -= 65536;
}
if(acc_xyz.z > 32767){
acc_xyz.z -= 65536;
}
return acc_xyz; // The function ends, return to the acc_xyz struct
}
acc_xyz = adxl345_read_xyz(fd); // Call the function to read the data collected by the
accelerometer module
printf("x: %05d y: %05d z: %05d\n", acc_xyz.x, acc_xyz.y, acc_xyz.z); // Print the
data collected by the accelerometer; %05d means the printed data is a 5-bit one, and the
empty bit will be replaced by 0.
For Python users:
Step 2: Get into the folder of the code
cd /home/pi/SunFounder_Super_Kit_V3.0_for_Raspberry_Pi/Python
Step 3: Run
sudo python 17_ADXL345.py
Code Explanation
class ADXL345(I2C): # Define a class ADXL345and the class inheritance is I2C
def __init__(self, busnum=-1, debug=False): # The initialize function of the
class, which is run when an instance is created of the class
def setRange(self, range): # Read the data format register to preserve bits.
Update the data rate, make sure that the FULL-RES bit is enabled for range scaling
def getRange(self): # Read an 8-bit data from the device register
def setDataRate(self, dataRate): # Note: The LOW_POWER bits are currently
ignored; we always keep the device in 'normal' mode
def getDataRate(self): # get the rate from the register
def read(self): # Read data from the accelerometer
raw = self.accel.readList(self.ADXL345_REG_DATAX0, 6) # Read 6 values from
the register, respectively equal to the high and low bits of the x, y, and z value
print raw
res = []
for i in range(0, 6, 2):
g = raw[i] | (raw[i+1] << 8) # Combine the high 8 bits and low 8 bits
and obtain a measurement value
SunFounder