Republished Library, to be refined for use with the SparkFun 9DOF in HARP project.

Dependents:   9Dof_unit_testing

Fork of ADXL345 by James Watanabe

Committer:
tylerjw
Date:
Mon Nov 05 18:34:49 2012 +0000
Revision:
8:4cdd4315189f
Parent:
6:5fb29534a6cf
Child:
9:cc0260a2404b
Added calibrate function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Digixx 0:1e4aa22fc1a1 1 /**
gltest26 3:7b83694c7292 2 * @file ADXL345.cpp
tylerjw 8:4cdd4315189f 3 * @author Tyler Weaver
Digixx 0:1e4aa22fc1a1 4 * @author Peter Swanson
Digixx 0:1e4aa22fc1a1 5 * A personal note from me: Jesus Christ has changed my life so much it blows my mind. I say this because
Digixx 0:1e4aa22fc1a1 6 * today, religion is thought of as something that you do or believe and has about as
Digixx 0:1e4aa22fc1a1 7 * little impact on a person as their political stance. But for me, God gives me daily
Digixx 0:1e4aa22fc1a1 8 * strength and has filled my life with the satisfaction that I could never find in any
tylerjw 6:5fb29534a6cf 9 * of the other things that I once looked for it in.
Digixx 0:1e4aa22fc1a1 10 * If your interested, heres verse that changed my life:
Digixx 0:1e4aa22fc1a1 11 * Rom 8:1-3: "Therefore, there is now no condemnation for those who are in Christ Jesus,
Digixx 0:1e4aa22fc1a1 12 * because through Christ Jesus, the law of the Spirit who gives life has set
tylerjw 6:5fb29534a6cf 13 * me free from the law of sin (which brings...) and death. For what the law
Digixx 0:1e4aa22fc1a1 14 * was powerless to do in that it was weakened by the flesh, God did by sending
Digixx 0:1e4aa22fc1a1 15 * His own Son in the likeness of sinful flesh to be a sin offering. And so He
tylerjw 6:5fb29534a6cf 16 * condemned sin in the flesh in order that the righteous requirements of the
Digixx 0:1e4aa22fc1a1 17 * (God's) law might be fully met in us, who live not according to the flesh
Digixx 0:1e4aa22fc1a1 18 * but according to the Spirit."
Digixx 0:1e4aa22fc1a1 19 *
Digixx 0:1e4aa22fc1a1 20 * A special thanks to Ewout van Bekkum for all his patient help in developing this library!
Digixx 0:1e4aa22fc1a1 21 *
Digixx 0:1e4aa22fc1a1 22 * @section LICENSE
Digixx 0:1e4aa22fc1a1 23 *
Digixx 0:1e4aa22fc1a1 24 * Permission is hereby granted, free of charge, to any person obtaining a copy
Digixx 0:1e4aa22fc1a1 25 * of this software and associated documentation files (the "Software"), to deal
Digixx 0:1e4aa22fc1a1 26 * in the Software without restriction, including without limitation the rights
Digixx 0:1e4aa22fc1a1 27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Digixx 0:1e4aa22fc1a1 28 * copies of the Software, and to permit persons to whom the Software is
Digixx 0:1e4aa22fc1a1 29 * furnished to do so, subject to the following conditions:
Digixx 0:1e4aa22fc1a1 30 *
Digixx 0:1e4aa22fc1a1 31 * The above copyright notice and this permission notice shall be included in
Digixx 0:1e4aa22fc1a1 32 * all copies or substantial portions of the Software.
Digixx 0:1e4aa22fc1a1 33 *
Digixx 0:1e4aa22fc1a1 34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Digixx 0:1e4aa22fc1a1 35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Digixx 0:1e4aa22fc1a1 36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Digixx 0:1e4aa22fc1a1 37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Digixx 0:1e4aa22fc1a1 38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Digixx 0:1e4aa22fc1a1 39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Digixx 0:1e4aa22fc1a1 40 * THE SOFTWARE.
Digixx 0:1e4aa22fc1a1 41 *
Digixx 0:1e4aa22fc1a1 42 * @section DESCRIPTION
Digixx 0:1e4aa22fc1a1 43 *
Digixx 0:1e4aa22fc1a1 44 * ADXL345, triple axis, I2C interface, accelerometer.
Digixx 0:1e4aa22fc1a1 45 *
Digixx 0:1e4aa22fc1a1 46 * Datasheet:
Digixx 0:1e4aa22fc1a1 47 *
Digixx 0:1e4aa22fc1a1 48 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
tylerjw 6:5fb29534a6cf 49 */
tylerjw 6:5fb29534a6cf 50
gltest26 3:7b83694c7292 51 /*
Digixx 0:1e4aa22fc1a1 52 * Includes
Digixx 0:1e4aa22fc1a1 53 */
Digixx 0:1e4aa22fc1a1 54 #include "ADXL345.h"
Digixx 0:1e4aa22fc1a1 55
Digixx 0:1e4aa22fc1a1 56 //#include "mbed.h"
Digixx 0:1e4aa22fc1a1 57
tylerjw 6:5fb29534a6cf 58 ADXL345::ADXL345(PinName sda, PinName scl) : i2c_(*(new I2C(sda, scl)))
tylerjw 6:5fb29534a6cf 59 {
gltest26 5:122a504fcfa3 60 myI2c = &i2c_;
gltest26 5:122a504fcfa3 61 }
gltest26 5:122a504fcfa3 62
tylerjw 6:5fb29534a6cf 63 ADXL345::~ADXL345()
tylerjw 6:5fb29534a6cf 64 {
gltest26 5:122a504fcfa3 65 delete myI2c;
gltest26 5:122a504fcfa3 66 }
gltest26 5:122a504fcfa3 67
gltest26 5:122a504fcfa3 68
tylerjw 6:5fb29534a6cf 69 void ADXL345::init()
tylerjw 6:5fb29534a6cf 70 {
tylerjw 6:5fb29534a6cf 71 // initialize the BW data rate
tylerjw 6:5fb29534a6cf 72 setDataRate(ADXL345_6HZ25); // 6.25 Hz
Digixx 0:1e4aa22fc1a1 73
tylerjw 8:4cdd4315189f 74 //Data format (for +-16g) - This is done by setting Bit D3 of the DATA_FORMAT register (Address 0x31)
tylerjw 6:5fb29534a6cf 75 //and writing a value of 0x03 to the range bits (Bit D1 and Bit D0) of the DATA_FORMAT register (Address 0x31).
tylerjw 6:5fb29534a6cf 76 setDataFormatControl(ADXL345_FULL_RES | ADXL345_2G); // full resolution, right justified, 2g range
tylerjw 6:5fb29534a6cf 77
tylerjw 6:5fb29534a6cf 78 // Set Offset - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE.
Digixx 0:1e4aa22fc1a1 79 }
Digixx 0:1e4aa22fc1a1 80
Digixx 0:1e4aa22fc1a1 81
tylerjw 6:5fb29534a6cf 82 char ADXL345::SingleByteRead(char address)
tylerjw 6:5fb29534a6cf 83 {
tylerjw 6:5fb29534a6cf 84 char tx = address;
tylerjw 6:5fb29534a6cf 85 char output;
Digixx 0:1e4aa22fc1a1 86 i2c_.write( ADXL345_WRITE , &tx, 1); //tell it what you want to read
Digixx 0:1e4aa22fc1a1 87 i2c_.read( ADXL345_READ , &output, 1); //tell it where to store the data
Digixx 0:1e4aa22fc1a1 88 return output;
Digixx 0:1e4aa22fc1a1 89 }
Digixx 0:1e4aa22fc1a1 90
Digixx 0:1e4aa22fc1a1 91
Digixx 0:1e4aa22fc1a1 92 /*
Digixx 0:1e4aa22fc1a1 93 ***info on the i2c_.write***
Digixx 0:1e4aa22fc1a1 94 address 8-bit I2C slave address [ addr | 0 ]
Digixx 0:1e4aa22fc1a1 95 data Pointer to the byte-array data to send
Digixx 0:1e4aa22fc1a1 96 length Number of bytes to send
Digixx 0:1e4aa22fc1a1 97 repeated Repeated start, true - do not send stop at end
Digixx 0:1e4aa22fc1a1 98 returns 0 on success (ack), or non-0 on failure (nack)
Digixx 0:1e4aa22fc1a1 99 */
Digixx 0:1e4aa22fc1a1 100
tylerjw 6:5fb29534a6cf 101 int ADXL345::SingleByteWrite(char address, char data)
tylerjw 6:5fb29534a6cf 102 {
tylerjw 6:5fb29534a6cf 103 int ack = 0;
tylerjw 6:5fb29534a6cf 104 char tx[2];
tylerjw 6:5fb29534a6cf 105 tx[0] = address;
tylerjw 6:5fb29534a6cf 106 tx[1] = data;
tylerjw 6:5fb29534a6cf 107 return ack | i2c_.write( ADXL345_WRITE , tx, 2);
Digixx 0:1e4aa22fc1a1 108 }
Digixx 0:1e4aa22fc1a1 109
Digixx 0:1e4aa22fc1a1 110
tylerjw 6:5fb29534a6cf 111 void ADXL345::multiByteRead(char address, char* output, int size)
tylerjw 6:5fb29534a6cf 112 {
Digixx 0:1e4aa22fc1a1 113 i2c_.write( ADXL345_WRITE, &address, 1); //tell it where to read from
Digixx 0:1e4aa22fc1a1 114 i2c_.read( ADXL345_READ , output, size); //tell it where to store the data read
Digixx 0:1e4aa22fc1a1 115 }
Digixx 0:1e4aa22fc1a1 116
Digixx 0:1e4aa22fc1a1 117
tylerjw 6:5fb29534a6cf 118 int ADXL345::multiByteWrite(char address, char* ptr_data, int size)
tylerjw 6:5fb29534a6cf 119 {
tylerjw 6:5fb29534a6cf 120 int ack;
tylerjw 6:5fb29534a6cf 121
tylerjw 6:5fb29534a6cf 122 ack = i2c_.write( ADXL345_WRITE, &address, 1); //tell it where to write to
tylerjw 6:5fb29534a6cf 123 return ack | i2c_.write( ADXL345_READ, ptr_data, size); //tell it what data to write
Digixx 0:1e4aa22fc1a1 124 }
Digixx 0:1e4aa22fc1a1 125
Digixx 0:1e4aa22fc1a1 126
tylerjw 8:4cdd4315189f 127 void ADXL345::getOutput(int16_t* readings)
tylerjw 6:5fb29534a6cf 128 {
tylerjw 6:5fb29534a6cf 129 char buffer[6];
Digixx 0:1e4aa22fc1a1 130 multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
tylerjw 6:5fb29534a6cf 131
gltest26 3:7b83694c7292 132 readings[0] = wordExtend(&buffer[0]);
gltest26 3:7b83694c7292 133 readings[1] = wordExtend(&buffer[2]);
gltest26 3:7b83694c7292 134 readings[2] = wordExtend(&buffer[4]);
Digixx 0:1e4aa22fc1a1 135 }
Digixx 0:1e4aa22fc1a1 136
tylerjw 6:5fb29534a6cf 137 char ADXL345::getDeviceID()
tylerjw 6:5fb29534a6cf 138 {
Digixx 0:1e4aa22fc1a1 139 return SingleByteRead(ADXL345_DEVID_REG);
tylerjw 6:5fb29534a6cf 140 }
Digixx 0:1e4aa22fc1a1 141 //
tylerjw 6:5fb29534a6cf 142 int ADXL345::setPowerMode(char mode)
tylerjw 6:5fb29534a6cf 143 {
Digixx 0:1e4aa22fc1a1 144 //Get the current register contents, so we don't clobber the rate value.
Digixx 0:1e4aa22fc1a1 145 char registerContents = (mode << 4) | SingleByteRead(ADXL345_BW_RATE_REG);
Digixx 0:1e4aa22fc1a1 146
tylerjw 6:5fb29534a6cf 147 return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents);
Digixx 0:1e4aa22fc1a1 148 }
Digixx 0:1e4aa22fc1a1 149
tylerjw 8:4cdd4315189f 150 char ADXL345::getBwRateReg()
tylerjw 8:4cdd4315189f 151 {
tylerjw 8:4cdd4315189f 152 return SingleByteRead(ADXL345_BW_RATE_REG);
tylerjw 8:4cdd4315189f 153 }
tylerjw 8:4cdd4315189f 154
tylerjw 8:4cdd4315189f 155 int ADXL345::setBwRateReg(char reg)
tylerjw 8:4cdd4315189f 156 {
tylerjw 8:4cdd4315189f 157 return SingleByteWrite(ADXL345_BW_RATE_REG, reg);
tylerjw 8:4cdd4315189f 158 }
tylerjw 8:4cdd4315189f 159
tylerjw 6:5fb29534a6cf 160 char ADXL345::getPowerControl()
tylerjw 6:5fb29534a6cf 161 {
Digixx 0:1e4aa22fc1a1 162 return SingleByteRead(ADXL345_POWER_CTL_REG);
Digixx 0:1e4aa22fc1a1 163 }
Digixx 0:1e4aa22fc1a1 164
tylerjw 6:5fb29534a6cf 165 int ADXL345::setPowerControl(char settings)
tylerjw 6:5fb29534a6cf 166 {
Digixx 0:1e4aa22fc1a1 167 return SingleByteWrite(ADXL345_POWER_CTL_REG, settings);
Digixx 0:1e4aa22fc1a1 168
Digixx 0:1e4aa22fc1a1 169 }
Digixx 0:1e4aa22fc1a1 170
tylerjw 6:5fb29534a6cf 171 char ADXL345::getDataFormatControl(void)
tylerjw 6:5fb29534a6cf 172 {
Digixx 0:1e4aa22fc1a1 173 return SingleByteRead(ADXL345_DATA_FORMAT_REG);
Digixx 0:1e4aa22fc1a1 174 }
Digixx 0:1e4aa22fc1a1 175
tylerjw 6:5fb29534a6cf 176 int ADXL345::setDataFormatControl(char settings)
tylerjw 6:5fb29534a6cf 177 {
tylerjw 6:5fb29534a6cf 178 return SingleByteWrite(ADXL345_DATA_FORMAT_REG, settings);
Digixx 0:1e4aa22fc1a1 179 }
Digixx 0:1e4aa22fc1a1 180
tylerjw 6:5fb29534a6cf 181 int ADXL345::setDataFormatControl(char settings, char mask, char *prev)
tylerjw 6:5fb29534a6cf 182 {
gltest26 4:8046894b947e 183 char old = SingleByteRead(ADXL345_DATA_FORMAT_REG);
gltest26 4:8046894b947e 184 if(prev)
gltest26 4:8046894b947e 185 *prev = old;
gltest26 4:8046894b947e 186 return SingleByteWrite(ADXL345_DATA_FORMAT_REG, (old | (settings & mask)) & (settings | ~mask));
gltest26 4:8046894b947e 187 }
gltest26 4:8046894b947e 188
tylerjw 6:5fb29534a6cf 189 int ADXL345::setDataRate(char rate)
tylerjw 6:5fb29534a6cf 190 {
Digixx 0:1e4aa22fc1a1 191 //Get the current register contents, so we don't clobber the power bit.
Digixx 0:1e4aa22fc1a1 192 char registerContents = SingleByteRead(ADXL345_BW_RATE_REG);
Digixx 0:1e4aa22fc1a1 193
Digixx 0:1e4aa22fc1a1 194 registerContents &= 0x10;
Digixx 0:1e4aa22fc1a1 195 registerContents |= rate;
Digixx 0:1e4aa22fc1a1 196
Digixx 0:1e4aa22fc1a1 197 return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents);
Digixx 0:1e4aa22fc1a1 198 }
Digixx 0:1e4aa22fc1a1 199
Digixx 0:1e4aa22fc1a1 200
tylerjw 6:5fb29534a6cf 201 char ADXL345::getOffset(char axis)
tylerjw 6:5fb29534a6cf 202 {
Digixx 0:1e4aa22fc1a1 203 char address = 0;
Digixx 0:1e4aa22fc1a1 204
Digixx 0:1e4aa22fc1a1 205 if (axis == ADXL345_X) {
Digixx 0:1e4aa22fc1a1 206 address = ADXL345_OFSX_REG;
Digixx 0:1e4aa22fc1a1 207 } else if (axis == ADXL345_Y) {
Digixx 0:1e4aa22fc1a1 208 address = ADXL345_OFSY_REG;
Digixx 0:1e4aa22fc1a1 209 } else if (axis == ADXL345_Z) {
Digixx 0:1e4aa22fc1a1 210 address = ADXL345_OFSZ_REG;
Digixx 0:1e4aa22fc1a1 211 }
Digixx 0:1e4aa22fc1a1 212
tylerjw 6:5fb29534a6cf 213 return SingleByteRead(address);
Digixx 0:1e4aa22fc1a1 214 }
Digixx 0:1e4aa22fc1a1 215
tylerjw 6:5fb29534a6cf 216 int ADXL345::setOffset(char axis, char offset)
tylerjw 6:5fb29534a6cf 217 {
Digixx 0:1e4aa22fc1a1 218 char address = 0;
Digixx 0:1e4aa22fc1a1 219
Digixx 0:1e4aa22fc1a1 220 if (axis == ADXL345_X) {
Digixx 0:1e4aa22fc1a1 221 address = ADXL345_OFSX_REG;
Digixx 0:1e4aa22fc1a1 222 } else if (axis == ADXL345_Y) {
Digixx 0:1e4aa22fc1a1 223 address = ADXL345_OFSY_REG;
Digixx 0:1e4aa22fc1a1 224 } else if (axis == ADXL345_Z) {
Digixx 0:1e4aa22fc1a1 225 address = ADXL345_OFSZ_REG;
Digixx 0:1e4aa22fc1a1 226 }
Digixx 0:1e4aa22fc1a1 227
tylerjw 6:5fb29534a6cf 228 return SingleByteWrite(address, offset);
Digixx 0:1e4aa22fc1a1 229 }
Digixx 0:1e4aa22fc1a1 230
Digixx 0:1e4aa22fc1a1 231
tylerjw 6:5fb29534a6cf 232 char ADXL345::getFifoControl(void)
tylerjw 6:5fb29534a6cf 233 {
tylerjw 6:5fb29534a6cf 234 return SingleByteRead(ADXL345_FIFO_CTL);
tylerjw 6:5fb29534a6cf 235 }
tylerjw 6:5fb29534a6cf 236
tylerjw 6:5fb29534a6cf 237 int ADXL345::setFifoControl(char settings)
tylerjw 6:5fb29534a6cf 238 {
tylerjw 6:5fb29534a6cf 239 return SingleByteWrite(ADXL345_FIFO_STATUS, settings);
tylerjw 6:5fb29534a6cf 240 }
Digixx 0:1e4aa22fc1a1 241
tylerjw 6:5fb29534a6cf 242 char ADXL345::getFifoStatus(void)
tylerjw 6:5fb29534a6cf 243 {
tylerjw 6:5fb29534a6cf 244 return SingleByteRead(ADXL345_FIFO_STATUS);
tylerjw 6:5fb29534a6cf 245 }
tylerjw 6:5fb29534a6cf 246
tylerjw 6:5fb29534a6cf 247 char ADXL345::getTapThreshold(void)
tylerjw 6:5fb29534a6cf 248 {
tylerjw 6:5fb29534a6cf 249 return SingleByteRead(ADXL345_THRESH_TAP_REG);
tylerjw 6:5fb29534a6cf 250 }
tylerjw 6:5fb29534a6cf 251
tylerjw 6:5fb29534a6cf 252 int ADXL345::setTapThreshold(char threshold)
tylerjw 6:5fb29534a6cf 253 {
tylerjw 6:5fb29534a6cf 254 return SingleByteWrite(ADXL345_THRESH_TAP_REG, threshold);
tylerjw 6:5fb29534a6cf 255 }
tylerjw 6:5fb29534a6cf 256
tylerjw 6:5fb29534a6cf 257 float ADXL345::getTapDuration(void)
tylerjw 6:5fb29534a6cf 258 {
Digixx 0:1e4aa22fc1a1 259 return (float)SingleByteRead(ADXL345_DUR_REG)*625;
Digixx 0:1e4aa22fc1a1 260 }
Digixx 0:1e4aa22fc1a1 261
tylerjw 6:5fb29534a6cf 262 int ADXL345::setTapDuration(short int duration_us)
tylerjw 6:5fb29534a6cf 263 {
Digixx 0:1e4aa22fc1a1 264 short int tapDuration = duration_us / 625;
Digixx 0:1e4aa22fc1a1 265 char tapChar[2];
tylerjw 6:5fb29534a6cf 266 tapChar[0] = (tapDuration & 0x00FF);
tylerjw 6:5fb29534a6cf 267 tapChar[1] = (tapDuration >> 8) & 0x00FF;
Digixx 0:1e4aa22fc1a1 268 return multiByteWrite(ADXL345_DUR_REG, tapChar, 2);
Digixx 0:1e4aa22fc1a1 269 }
Digixx 0:1e4aa22fc1a1 270
tylerjw 6:5fb29534a6cf 271 float ADXL345::getTapLatency(void)
tylerjw 6:5fb29534a6cf 272 {
Digixx 0:1e4aa22fc1a1 273 return (float)SingleByteRead(ADXL345_LATENT_REG)*1.25;
Digixx 0:1e4aa22fc1a1 274 }
Digixx 0:1e4aa22fc1a1 275
tylerjw 6:5fb29534a6cf 276 int ADXL345::setTapLatency(short int latency_ms)
tylerjw 6:5fb29534a6cf 277 {
Digixx 0:1e4aa22fc1a1 278 latency_ms = latency_ms / 1.25;
Digixx 0:1e4aa22fc1a1 279 char latChar[2];
tylerjw 6:5fb29534a6cf 280 latChar[0] = (latency_ms & 0x00FF);
tylerjw 6:5fb29534a6cf 281 latChar[1] = (latency_ms << 8) & 0xFF00;
Digixx 0:1e4aa22fc1a1 282 return multiByteWrite(ADXL345_LATENT_REG, latChar, 2);
Digixx 0:1e4aa22fc1a1 283 }
Digixx 0:1e4aa22fc1a1 284
tylerjw 6:5fb29534a6cf 285 float ADXL345::getWindowTime(void)
tylerjw 6:5fb29534a6cf 286 {
Digixx 0:1e4aa22fc1a1 287 return (float)SingleByteRead(ADXL345_WINDOW_REG)*1.25;
Digixx 0:1e4aa22fc1a1 288 }
Digixx 0:1e4aa22fc1a1 289
tylerjw 6:5fb29534a6cf 290 int ADXL345::setWindowTime(short int window_ms)
tylerjw 6:5fb29534a6cf 291 {
Digixx 0:1e4aa22fc1a1 292 window_ms = window_ms / 1.25;
Digixx 0:1e4aa22fc1a1 293 char windowChar[2];
Digixx 0:1e4aa22fc1a1 294 windowChar[0] = (window_ms & 0x00FF);
Digixx 0:1e4aa22fc1a1 295 windowChar[1] = ((window_ms << 8) & 0xFF00);
tylerjw 6:5fb29534a6cf 296 return multiByteWrite(ADXL345_WINDOW_REG, windowChar, 2);
Digixx 0:1e4aa22fc1a1 297 }
Digixx 0:1e4aa22fc1a1 298
tylerjw 6:5fb29534a6cf 299 char ADXL345::getActivityThreshold(void)
tylerjw 6:5fb29534a6cf 300 {
Digixx 0:1e4aa22fc1a1 301 return SingleByteRead(ADXL345_THRESH_ACT_REG);
Digixx 0:1e4aa22fc1a1 302 }
Digixx 0:1e4aa22fc1a1 303
tylerjw 6:5fb29534a6cf 304 int ADXL345::setActivityThreshold(char threshold)
tylerjw 6:5fb29534a6cf 305 {
Digixx 0:1e4aa22fc1a1 306 return SingleByteWrite(ADXL345_THRESH_ACT_REG, threshold);
Digixx 0:1e4aa22fc1a1 307 }
Digixx 0:1e4aa22fc1a1 308
tylerjw 6:5fb29534a6cf 309 char ADXL345::getInactivityThreshold(void)
tylerjw 6:5fb29534a6cf 310 {
Digixx 0:1e4aa22fc1a1 311 return SingleByteRead(ADXL345_THRESH_INACT_REG);
Digixx 0:1e4aa22fc1a1 312 }
Digixx 0:1e4aa22fc1a1 313
Digixx 0:1e4aa22fc1a1 314 //int FUNCTION(short int * ptr_Output)
Digixx 0:1e4aa22fc1a1 315 //short int FUNCTION ()
Digixx 0:1e4aa22fc1a1 316
tylerjw 6:5fb29534a6cf 317 int ADXL345::setInactivityThreshold(char threshold)
tylerjw 6:5fb29534a6cf 318 {
Digixx 0:1e4aa22fc1a1 319 return SingleByteWrite(ADXL345_THRESH_INACT_REG, threshold);
Digixx 0:1e4aa22fc1a1 320 }
Digixx 0:1e4aa22fc1a1 321
tylerjw 6:5fb29534a6cf 322 char ADXL345::getTimeInactivity(void)
tylerjw 6:5fb29534a6cf 323 {
Digixx 0:1e4aa22fc1a1 324 return SingleByteRead(ADXL345_TIME_INACT_REG);
Digixx 0:1e4aa22fc1a1 325 }
Digixx 0:1e4aa22fc1a1 326
tylerjw 6:5fb29534a6cf 327 int ADXL345::setTimeInactivity(char timeInactivity)
tylerjw 6:5fb29534a6cf 328 {
Digixx 0:1e4aa22fc1a1 329 return SingleByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
Digixx 0:1e4aa22fc1a1 330 }
Digixx 0:1e4aa22fc1a1 331
tylerjw 6:5fb29534a6cf 332 char ADXL345::getActivityInactivityControl(void)
tylerjw 6:5fb29534a6cf 333 {
Digixx 0:1e4aa22fc1a1 334 return SingleByteRead(ADXL345_ACT_INACT_CTL_REG);
Digixx 0:1e4aa22fc1a1 335 }
Digixx 0:1e4aa22fc1a1 336
tylerjw 6:5fb29534a6cf 337 int ADXL345::setActivityInactivityControl(char settings)
tylerjw 6:5fb29534a6cf 338 {
Digixx 0:1e4aa22fc1a1 339 return SingleByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
Digixx 0:1e4aa22fc1a1 340 }
Digixx 0:1e4aa22fc1a1 341
tylerjw 6:5fb29534a6cf 342 char ADXL345::getFreefallThreshold(void)
tylerjw 6:5fb29534a6cf 343 {
Digixx 0:1e4aa22fc1a1 344 return SingleByteRead(ADXL345_THRESH_FF_REG);
Digixx 0:1e4aa22fc1a1 345 }
Digixx 0:1e4aa22fc1a1 346
tylerjw 6:5fb29534a6cf 347 int ADXL345::setFreefallThreshold(char threshold)
tylerjw 6:5fb29534a6cf 348 {
tylerjw 6:5fb29534a6cf 349 return SingleByteWrite(ADXL345_THRESH_FF_REG, threshold);
Digixx 0:1e4aa22fc1a1 350 }
Digixx 0:1e4aa22fc1a1 351
tylerjw 6:5fb29534a6cf 352 char ADXL345::getFreefallTime(void)
tylerjw 6:5fb29534a6cf 353 {
Digixx 0:1e4aa22fc1a1 354 return SingleByteRead(ADXL345_TIME_FF_REG)*5;
Digixx 0:1e4aa22fc1a1 355 }
Digixx 0:1e4aa22fc1a1 356
tylerjw 6:5fb29534a6cf 357 int ADXL345::setFreefallTime(short int freefallTime_ms)
tylerjw 6:5fb29534a6cf 358 {
tylerjw 6:5fb29534a6cf 359 freefallTime_ms = freefallTime_ms / 5;
tylerjw 6:5fb29534a6cf 360 char fallChar[2];
tylerjw 6:5fb29534a6cf 361 fallChar[0] = (freefallTime_ms & 0x00FF);
tylerjw 6:5fb29534a6cf 362 fallChar[1] = (freefallTime_ms << 8) & 0xFF00;
tylerjw 6:5fb29534a6cf 363
Digixx 0:1e4aa22fc1a1 364 return multiByteWrite(ADXL345_TIME_FF_REG, fallChar, 2);
Digixx 0:1e4aa22fc1a1 365 }
Digixx 0:1e4aa22fc1a1 366
tylerjw 6:5fb29534a6cf 367 char ADXL345::getTapAxisControl(void)
tylerjw 6:5fb29534a6cf 368 {
Digixx 0:1e4aa22fc1a1 369 return SingleByteRead(ADXL345_TAP_AXES_REG);
Digixx 0:1e4aa22fc1a1 370 }
Digixx 0:1e4aa22fc1a1 371
tylerjw 6:5fb29534a6cf 372 int ADXL345::setTapAxisControl(char settings)
tylerjw 6:5fb29534a6cf 373 {
tylerjw 6:5fb29534a6cf 374 return SingleByteWrite(ADXL345_TAP_AXES_REG, settings);
Digixx 0:1e4aa22fc1a1 375 }
Digixx 0:1e4aa22fc1a1 376
tylerjw 6:5fb29534a6cf 377 char ADXL345::getTapSource(void)
tylerjw 6:5fb29534a6cf 378 {
Digixx 0:1e4aa22fc1a1 379 return SingleByteRead(ADXL345_ACT_TAP_STATUS_REG);
Digixx 0:1e4aa22fc1a1 380 }
Digixx 0:1e4aa22fc1a1 381
tylerjw 6:5fb29534a6cf 382 char ADXL345::getInterruptEnableControl(void)
tylerjw 6:5fb29534a6cf 383 {
Digixx 0:1e4aa22fc1a1 384 return SingleByteRead(ADXL345_INT_ENABLE_REG);
Digixx 0:1e4aa22fc1a1 385 }
Digixx 0:1e4aa22fc1a1 386
tylerjw 6:5fb29534a6cf 387 int ADXL345::setInterruptEnableControl(char settings)
tylerjw 6:5fb29534a6cf 388 {
tylerjw 6:5fb29534a6cf 389 return SingleByteWrite(ADXL345_INT_ENABLE_REG, settings);
Digixx 0:1e4aa22fc1a1 390 }
Digixx 0:1e4aa22fc1a1 391
tylerjw 6:5fb29534a6cf 392 char ADXL345::getInterruptMappingControl(void)
tylerjw 6:5fb29534a6cf 393 {
Digixx 0:1e4aa22fc1a1 394 return SingleByteRead(ADXL345_INT_MAP_REG);
Digixx 0:1e4aa22fc1a1 395 }
Digixx 0:1e4aa22fc1a1 396
tylerjw 6:5fb29534a6cf 397 int ADXL345::setInterruptMappingControl(char settings)
tylerjw 6:5fb29534a6cf 398 {
Digixx 0:1e4aa22fc1a1 399 return SingleByteWrite(ADXL345_INT_MAP_REG, settings);
Digixx 0:1e4aa22fc1a1 400 }
Digixx 0:1e4aa22fc1a1 401
tylerjw 6:5fb29534a6cf 402 char ADXL345::getInterruptSource(void)
tylerjw 6:5fb29534a6cf 403 {
Digixx 0:1e4aa22fc1a1 404 return SingleByteRead(ADXL345_INT_SOURCE_REG);
tylerjw 8:4cdd4315189f 405 }
tylerjw 8:4cdd4315189f 406
tylerjw 8:4cdd4315189f 407 void ADXL345::sample100avg(float period, int16_t buffer[][3], int16_t *avg, Timer* t)
tylerjw 8:4cdd4315189f 408 {
tylerjw 8:4cdd4315189f 409 double start_time;
tylerjw 8:4cdd4315189f 410
tylerjw 8:4cdd4315189f 411 for(int sample = 0; sample < 100; sample++) {
tylerjw 8:4cdd4315189f 412 start_time = t->read();
tylerjw 8:4cdd4315189f 413
tylerjw 8:4cdd4315189f 414 getOutput(buffer[sample]);
tylerjw 8:4cdd4315189f 415
tylerjw 8:4cdd4315189f 416 wait(period - (start_time - t->read()));
tylerjw 8:4cdd4315189f 417 }
tylerjw 8:4cdd4315189f 418
tylerjw 8:4cdd4315189f 419 for(int axis = 0; axis < 3; axis++) {
tylerjw 8:4cdd4315189f 420 double average = 0.0;
tylerjw 8:4cdd4315189f 421 for(int sample = 0; sample < 100; sample++)
tylerjw 8:4cdd4315189f 422 average += buffer[sample][axis];
tylerjw 8:4cdd4315189f 423 average /= 100.0;
tylerjw 8:4cdd4315189f 424 avg[axis] = static_cast<int16_t>(average);
tylerjw 8:4cdd4315189f 425 }
tylerjw 8:4cdd4315189f 426 }
tylerjw 8:4cdd4315189f 427
tylerjw 8:4cdd4315189f 428 void ADXL345::calibrate(Timer* t, bool store_output, Serial *pc)
tylerjw 8:4cdd4315189f 429 {
tylerjw 8:4cdd4315189f 430 int16_t data[100][3]; // {x,y,z}, data
tylerjw 8:4cdd4315189f 431 int16_t data_avg[3];
tylerjw 8:4cdd4315189f 432 int8_t calibration_offset[3];
tylerjw 8:4cdd4315189f 433
tylerjw 8:4cdd4315189f 434 float period = 0.01; // period of sample rate
tylerjw 8:4cdd4315189f 435
tylerjw 8:4cdd4315189f 436 // wait 11.1ms
tylerjw 8:4cdd4315189f 437 wait(0.0111);
tylerjw 8:4cdd4315189f 438
tylerjw 8:4cdd4315189f 439 pc->puts("Reading old register states... ");
tylerjw 8:4cdd4315189f 440 // read current register states
tylerjw 8:4cdd4315189f 441 char bw_rate = getBwRateReg();
tylerjw 8:4cdd4315189f 442 char power_control = getPowerControl();
tylerjw 8:4cdd4315189f 443 char data_format = getDataFormatControl();
tylerjw 8:4cdd4315189f 444
tylerjw 8:4cdd4315189f 445 pc->puts("Done!\r\nSetting new register states... ");
tylerjw 8:4cdd4315189f 446 // initalize command sequence
tylerjw 8:4cdd4315189f 447 setDataFormatControl((ADXL345_16G | ADXL345_FULL_RES));
tylerjw 8:4cdd4315189f 448 setBwRateReg(ADXL345_100HZ); // 100Hz data rate
tylerjw 8:4cdd4315189f 449 setPowerControl(0x08); // start measurement
tylerjw 8:4cdd4315189f 450
tylerjw 8:4cdd4315189f 451 // wait 1.1ms
tylerjw 8:4cdd4315189f 452 wait(0.0111);
tylerjw 8:4cdd4315189f 453 pc->puts("Done!\r\nSampling... ");
tylerjw 8:4cdd4315189f 454 //take 100 data points and average (100Hz)
tylerjw 8:4cdd4315189f 455 sample100avg(period, data, data_avg, t);
tylerjw 8:4cdd4315189f 456 pc->puts("Done!\r\nCalculating offset values... ");
tylerjw 8:4cdd4315189f 457 // calculate calibration value
tylerjw 8:4cdd4315189f 458 calibration_offset[0] = -1 * (data_avg[0] / 4); // x
tylerjw 8:4cdd4315189f 459 calibration_offset[1] = -1 * (data_avg[1] / 4); // y
tylerjw 8:4cdd4315189f 460 calibration_offset[2] = -1 * ((data_avg[2] - 256) / 4); // z
tylerjw 8:4cdd4315189f 461
tylerjw 8:4cdd4315189f 462 if(store_output) {
tylerjw 8:4cdd4315189f 463 pc->puts("Done!\r\nStoring output to file... ");
tylerjw 8:4cdd4315189f 464 LocalFileSystem local("local");
tylerjw 8:4cdd4315189f 465 FILE *fp = fopen("/local/OFF_CAL.csv", "w"); // write
tylerjw 8:4cdd4315189f 466 fprintf(fp, "ADXL345 Calibration offsets\r\nx,%d\r\ny,%d\r\nz,%d\r\n\r\n", calibration_offset[0], calibration_offset[1], calibration_offset[2]);
tylerjw 8:4cdd4315189f 467
tylerjw 8:4cdd4315189f 468 fputs("Raw Data:\r\nX,Y,Z\r\n", fp);
tylerjw 8:4cdd4315189f 469 for(int sample = 0; sample < 100; sample++)
tylerjw 8:4cdd4315189f 470 fprintf(fp, "%d,%d,%d\r\n",data[sample][0],data[sample][1],data[sample][2]);
tylerjw 8:4cdd4315189f 471 fclose(fp);
tylerjw 8:4cdd4315189f 472 }
tylerjw 8:4cdd4315189f 473 pc->puts("Done!\r\nSetting the offset registers... ");
tylerjw 8:4cdd4315189f 474 // update offset registers
tylerjw 8:4cdd4315189f 475 for(char axis = 0x00; axis < 0x03; axis++)
tylerjw 8:4cdd4315189f 476 setOffset(axis,calibration_offset[axis]);
tylerjw 8:4cdd4315189f 477 pc->puts("Done!\r\nReturning registers to original state... ");
tylerjw 8:4cdd4315189f 478 // return control registers to original state
tylerjw 8:4cdd4315189f 479 setDataFormatControl(data_format);
tylerjw 8:4cdd4315189f 480 setBwRateReg(bw_rate);
tylerjw 8:4cdd4315189f 481 setPowerControl(power_control);
tylerjw 8:4cdd4315189f 482 pc->puts("Done!\r\n");
tylerjw 8:4cdd4315189f 483 }
tylerjw 8:4cdd4315189f 484
tylerjw 8:4cdd4315189f 485 void ADXL345::calibrate(Timer* t, bool store_output)
tylerjw 8:4cdd4315189f 486 {
tylerjw 8:4cdd4315189f 487 int16_t data[100][3]; // {x,y,z}, data
tylerjw 8:4cdd4315189f 488 int16_t data_avg[3];
tylerjw 8:4cdd4315189f 489 int8_t calibration_offset[3];
tylerjw 8:4cdd4315189f 490
tylerjw 8:4cdd4315189f 491 float period = 0.01; // period of sample rate
tylerjw 8:4cdd4315189f 492
tylerjw 8:4cdd4315189f 493 // wait 11.1ms
tylerjw 8:4cdd4315189f 494 wait(0.0111);
tylerjw 8:4cdd4315189f 495
tylerjw 8:4cdd4315189f 496 // read current register states
tylerjw 8:4cdd4315189f 497 char bw_rate = getBwRateReg();
tylerjw 8:4cdd4315189f 498 char power_control = getPowerControl();
tylerjw 8:4cdd4315189f 499 char data_format = getDataFormatControl();
tylerjw 8:4cdd4315189f 500
tylerjw 8:4cdd4315189f 501 // initalize command sequence
tylerjw 8:4cdd4315189f 502 setDataFormatControl((ADXL345_16G | ADXL345_FULL_RES));
tylerjw 8:4cdd4315189f 503 setBwRateReg(ADXL345_100HZ); // 100Hz data rate
tylerjw 8:4cdd4315189f 504 setPowerControl(0x08); // start measurement
tylerjw 8:4cdd4315189f 505
tylerjw 8:4cdd4315189f 506 // wait 1.1ms
tylerjw 8:4cdd4315189f 507 wait(0.0111);
tylerjw 8:4cdd4315189f 508 //take 100 data points and average (100Hz)
tylerjw 8:4cdd4315189f 509 sample100avg(period, data, data_avg, t);
tylerjw 8:4cdd4315189f 510 // calculate calibration value
tylerjw 8:4cdd4315189f 511 calibration_offset[0] = -1 * (data_avg[0] / 4); // x
tylerjw 8:4cdd4315189f 512 calibration_offset[1] = -1 * (data_avg[1] / 4); // y
tylerjw 8:4cdd4315189f 513 calibration_offset[2] = -1 * ((data_avg[2] - 256) / 4); // z
tylerjw 8:4cdd4315189f 514
tylerjw 8:4cdd4315189f 515 if(store_output) {
tylerjw 8:4cdd4315189f 516 LocalFileSystem local("local");
tylerjw 8:4cdd4315189f 517 FILE *fp = fopen("/local/OFF_CAL.csv", "w"); // write
tylerjw 8:4cdd4315189f 518 fprintf(fp, "ADXL345 Calibration offsets\r\nx,%d\r\ny,%d\r\nz,%d\r\n\r\n", calibration_offset[0], calibration_offset[1], calibration_offset[2]);
tylerjw 8:4cdd4315189f 519
tylerjw 8:4cdd4315189f 520 fputs("Raw Data:\r\nX,Y,Z\r\n", fp);
tylerjw 8:4cdd4315189f 521 for(int sample = 0; sample < 100; sample++)
tylerjw 8:4cdd4315189f 522 fprintf(fp, "%d,%d,%d\r\n",data[sample][0],data[sample][1],data[sample][2]);
tylerjw 8:4cdd4315189f 523 fclose(fp);
tylerjw 8:4cdd4315189f 524 }
tylerjw 8:4cdd4315189f 525 // update offset registers
tylerjw 8:4cdd4315189f 526 for(char axis = 0x00; axis < 0x03; axis++)
tylerjw 8:4cdd4315189f 527 setOffset(axis,calibration_offset[axis]);
tylerjw 8:4cdd4315189f 528 // return control registers to original state
tylerjw 8:4cdd4315189f 529 setDataFormatControl(data_format);
tylerjw 8:4cdd4315189f 530 setBwRateReg(bw_rate);
tylerjw 8:4cdd4315189f 531 setPowerControl(power_control);
tylerjw 6:5fb29534a6cf 532 }