Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ADXL345.cpp
00001 #include "ADXL345.h" 00002 00003 ADXL345::ADXL345(PinName sda, PinName scl) : I2C_Sensor(sda, scl, ADXL345_I2C_ADDRESS) 00004 { 00005 // Set Offset - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE. 00006 writeRegister(ADXL345_OFSX_REG, 0xFA); // to get these offsets just lie your sensor down on the table always the axis pointing down to earth has 200+ and the others should have more or less 0 00007 writeRegister(ADXL345_OFSY_REG, 0xFE); 00008 writeRegister(ADXL345_OFSZ_REG, 0x0A); 00009 00010 writeRegister(ADXL345_BW_RATE_REG, 0x0F); // 3200Hz BW-Rate 00011 writeRegister(ADXL345_DATA_FORMAT_REG, 0x0B); // set data format to full resolution and +-16g 00012 writeRegister(ADXL345_POWER_CTL_REG, 0x08); // set mode 00013 } 00014 00015 void ADXL345::read(){ 00016 readraw(); 00017 for (int i = 0; i < 3; i++) 00018 data[i] = raw[i] - offset[i]; // TODO: didnt care about units 00019 } 00020 00021 void ADXL345::readraw(){ 00022 char buffer[6]; 00023 readMultiRegister(ADXL345_DATAX0_REG, buffer, 6); 00024 00025 raw[0] = (short) ((int)buffer[1] << 8 | (int)buffer[0]); 00026 raw[1] = (short) ((int)buffer[3] << 8 | (int)buffer[2]); 00027 raw[2] = (short) ((int)buffer[5] << 8 | (int)buffer[4]); 00028 } 00029 00030 void ADXL345::calibrate(int times, float separation_time) 00031 { 00032 // calibrate sensor with an average of count samples (result of calibration stored in offset[]) 00033 float calib[3] = {0,0,0}; // temporary array for the sum of calibration measurement 00034 00035 for (int i = 0; i < times; i++) { // read 'times' times the data in a very short time 00036 readraw(); 00037 for (int j = 0; j < 3; j++) 00038 calib[j] += raw[j]; 00039 wait(separation_time); 00040 } 00041 00042 for (int i = 0; i < 2; i++) 00043 offset[i] = calib[i]/times; // take the average of the calibration measurements 00044 }
Generated on Tue Jul 12 2022 20:54:01 by
1.7.2