ADXL345 triple axis, digital accelerometer library.

Dependents:   AntiTheftGPS

Committer:
Owen
Date:
Thu Jan 20 20:11:51 2011 +0000
Revision:
0:745335502422
Based on ADXL345 from Jose R Padron (which is modified from Aaron Berk\s original). Fixes a bug in setFifoControl.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Owen 0:745335502422 1 /**
Owen 0:745335502422 2 * @author Aaron Berk
Owen 0:745335502422 3 *
Owen 0:745335502422 4 * @section LICENSE
Owen 0:745335502422 5 *
Owen 0:745335502422 6 * Copyright (c) 2010 ARM Limited
Owen 0:745335502422 7 *
Owen 0:745335502422 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
Owen 0:745335502422 9 * of this software and associated documentation files (the "Software"), to deal
Owen 0:745335502422 10 * in the Software without restriction, including without limitation the rights
Owen 0:745335502422 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Owen 0:745335502422 12 * copies of the Software, and to permit persons to whom the Software is
Owen 0:745335502422 13 * furnished to do so, subject to the following conditions:
Owen 0:745335502422 14 *
Owen 0:745335502422 15 * The above copyright notice and this permission notice shall be included in
Owen 0:745335502422 16 * all copies or substantial portions of the Software.
Owen 0:745335502422 17 *
Owen 0:745335502422 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Owen 0:745335502422 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Owen 0:745335502422 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Owen 0:745335502422 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Owen 0:745335502422 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Owen 0:745335502422 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Owen 0:745335502422 24 * THE SOFTWARE.
Owen 0:745335502422 25 *
Owen 0:745335502422 26 * @section DESCRIPTION
Owen 0:745335502422 27 *
Owen 0:745335502422 28 * ADXL345, triple axis, digital interface, accelerometer.
Owen 0:745335502422 29 *
Owen 0:745335502422 30 * Datasheet:
Owen 0:745335502422 31 *
Owen 0:745335502422 32 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
Owen 0:745335502422 33 */
Owen 0:745335502422 34
Owen 0:745335502422 35 /**
Owen 0:745335502422 36 * Includes
Owen 0:745335502422 37 */
Owen 0:745335502422 38 #include "ADXL345.h"
Owen 0:745335502422 39
Owen 0:745335502422 40 ADXL345::ADXL345(PinName mosi,
Owen 0:745335502422 41 PinName miso,
Owen 0:745335502422 42 PinName sck,
Owen 0:745335502422 43 PinName cs) : spi_(mosi, miso, sck), nCS_(cs) {
Owen 0:745335502422 44
Owen 0:745335502422 45 //1.5MHz, allowing us to use the fastest data rates without problems with the FIFO (must be less than 1.6MHz)
Owen 0:745335502422 46 spi_.frequency(1500000);
Owen 0:745335502422 47 spi_.format(8,3);
Owen 0:745335502422 48
Owen 0:745335502422 49 nCS_ = 1;
Owen 0:745335502422 50
Owen 0:745335502422 51 wait_us(500);
Owen 0:745335502422 52
Owen 0:745335502422 53 }
Owen 0:745335502422 54
Owen 0:745335502422 55 int ADXL345::getDevId(void) {
Owen 0:745335502422 56
Owen 0:745335502422 57 return oneByteRead(ADXL345_DEVID_REG);
Owen 0:745335502422 58
Owen 0:745335502422 59 }
Owen 0:745335502422 60
Owen 0:745335502422 61 int ADXL345::getTapThreshold(void) {
Owen 0:745335502422 62
Owen 0:745335502422 63 return oneByteRead(ADXL345_THRESH_TAP_REG);
Owen 0:745335502422 64
Owen 0:745335502422 65 }
Owen 0:745335502422 66
Owen 0:745335502422 67 void ADXL345::setTapThreshold(int threshold) {
Owen 0:745335502422 68
Owen 0:745335502422 69 oneByteWrite(ADXL345_THRESH_TAP_REG, threshold);
Owen 0:745335502422 70
Owen 0:745335502422 71 }
Owen 0:745335502422 72
Owen 0:745335502422 73 int ADXL345::getOffset(int axis) {
Owen 0:745335502422 74
Owen 0:745335502422 75 int address = 0;
Owen 0:745335502422 76
Owen 0:745335502422 77 if (axis == ADXL345_X) {
Owen 0:745335502422 78 address = ADXL345_OFSX_REG;
Owen 0:745335502422 79 } else if (axis == ADXL345_Y) {
Owen 0:745335502422 80 address = ADXL345_OFSY_REG;
Owen 0:745335502422 81 } else if (axis == ADXL345_Z) {
Owen 0:745335502422 82 address = ADXL345_OFSZ_REG;
Owen 0:745335502422 83 }
Owen 0:745335502422 84
Owen 0:745335502422 85 return oneByteRead(address);
Owen 0:745335502422 86
Owen 0:745335502422 87 }
Owen 0:745335502422 88
Owen 0:745335502422 89 void ADXL345::setOffset(int axis, char offset) {
Owen 0:745335502422 90
Owen 0:745335502422 91 int address = 0;
Owen 0:745335502422 92
Owen 0:745335502422 93 if (axis == ADXL345_X) {
Owen 0:745335502422 94 address = ADXL345_OFSX_REG;
Owen 0:745335502422 95 } else if (axis == ADXL345_Y) {
Owen 0:745335502422 96 address = ADXL345_OFSY_REG;
Owen 0:745335502422 97 } else if (axis == ADXL345_Z) {
Owen 0:745335502422 98 address = ADXL345_OFSZ_REG;
Owen 0:745335502422 99 }
Owen 0:745335502422 100
Owen 0:745335502422 101 return oneByteWrite(address, offset);
Owen 0:745335502422 102
Owen 0:745335502422 103 }
Owen 0:745335502422 104
Owen 0:745335502422 105 int ADXL345::getTapDuration(void) {
Owen 0:745335502422 106
Owen 0:745335502422 107 return oneByteRead(ADXL345_DUR_REG)*625;
Owen 0:745335502422 108
Owen 0:745335502422 109 }
Owen 0:745335502422 110
Owen 0:745335502422 111 void ADXL345::setTapDuration(int duration_us) {
Owen 0:745335502422 112
Owen 0:745335502422 113 int tapDuration = duration_us / 625;
Owen 0:745335502422 114
Owen 0:745335502422 115 oneByteWrite(ADXL345_DUR_REG, tapDuration);
Owen 0:745335502422 116
Owen 0:745335502422 117 }
Owen 0:745335502422 118
Owen 0:745335502422 119 float ADXL345::getTapLatency(void) {
Owen 0:745335502422 120
Owen 0:745335502422 121 return oneByteRead(ADXL345_LATENT_REG)*1.25;
Owen 0:745335502422 122
Owen 0:745335502422 123 }
Owen 0:745335502422 124
Owen 0:745335502422 125 void ADXL345::setTapLatency(int latency_ms) {
Owen 0:745335502422 126
Owen 0:745335502422 127 int tapLatency = latency_ms / 1.25;
Owen 0:745335502422 128
Owen 0:745335502422 129 oneByteWrite(ADXL345_LATENT_REG, tapLatency);
Owen 0:745335502422 130
Owen 0:745335502422 131 }
Owen 0:745335502422 132
Owen 0:745335502422 133 float ADXL345::getWindowTime(void) {
Owen 0:745335502422 134
Owen 0:745335502422 135 return oneByteRead(ADXL345_WINDOW_REG)*1.25;
Owen 0:745335502422 136
Owen 0:745335502422 137 }
Owen 0:745335502422 138
Owen 0:745335502422 139 void ADXL345::setWindowTime(int window_ms) {
Owen 0:745335502422 140
Owen 0:745335502422 141 int windowTime = window_ms / 1.25;
Owen 0:745335502422 142
Owen 0:745335502422 143 oneByteWrite(ADXL345_WINDOW_REG, windowTime);
Owen 0:745335502422 144
Owen 0:745335502422 145 }
Owen 0:745335502422 146
Owen 0:745335502422 147 int ADXL345::getActivityThreshold(void) {
Owen 0:745335502422 148
Owen 0:745335502422 149 return oneByteRead(ADXL345_THRESH_ACT_REG);
Owen 0:745335502422 150
Owen 0:745335502422 151 }
Owen 0:745335502422 152
Owen 0:745335502422 153 void ADXL345::setActivityThreshold(int threshold) {
Owen 0:745335502422 154
Owen 0:745335502422 155 oneByteWrite(ADXL345_THRESH_ACT_REG, threshold);
Owen 0:745335502422 156
Owen 0:745335502422 157 }
Owen 0:745335502422 158
Owen 0:745335502422 159 int ADXL345::getInactivityThreshold(void) {
Owen 0:745335502422 160
Owen 0:745335502422 161 return oneByteRead(ADXL345_THRESH_INACT_REG);
Owen 0:745335502422 162
Owen 0:745335502422 163 }
Owen 0:745335502422 164
Owen 0:745335502422 165 void ADXL345::setInactivityThreshold(int threshold) {
Owen 0:745335502422 166
Owen 0:745335502422 167 return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold);
Owen 0:745335502422 168
Owen 0:745335502422 169 }
Owen 0:745335502422 170
Owen 0:745335502422 171 int ADXL345::getTimeInactivity(void) {
Owen 0:745335502422 172
Owen 0:745335502422 173 return oneByteRead(ADXL345_TIME_INACT_REG);
Owen 0:745335502422 174
Owen 0:745335502422 175 }
Owen 0:745335502422 176
Owen 0:745335502422 177 void ADXL345::setTimeInactivity(int timeInactivity) {
Owen 0:745335502422 178
Owen 0:745335502422 179 oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
Owen 0:745335502422 180
Owen 0:745335502422 181 }
Owen 0:745335502422 182
Owen 0:745335502422 183 int ADXL345::getActivityInactivityControl(void) {
Owen 0:745335502422 184
Owen 0:745335502422 185 return oneByteRead(ADXL345_ACT_INACT_CTL_REG);
Owen 0:745335502422 186
Owen 0:745335502422 187 }
Owen 0:745335502422 188
Owen 0:745335502422 189 void ADXL345::setActivityInactivityControl(int settings) {
Owen 0:745335502422 190
Owen 0:745335502422 191 oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
Owen 0:745335502422 192
Owen 0:745335502422 193 }
Owen 0:745335502422 194
Owen 0:745335502422 195 int ADXL345::getFreefallThreshold(void) {
Owen 0:745335502422 196
Owen 0:745335502422 197 return oneByteRead(ADXL345_THRESH_FF_REG);
Owen 0:745335502422 198
Owen 0:745335502422 199 }
Owen 0:745335502422 200
Owen 0:745335502422 201 void ADXL345::setFreefallThreshold(int threshold) {
Owen 0:745335502422 202
Owen 0:745335502422 203 oneByteWrite(ADXL345_THRESH_FF_REG, threshold);
Owen 0:745335502422 204
Owen 0:745335502422 205 }
Owen 0:745335502422 206
Owen 0:745335502422 207 int ADXL345::getFreefallTime(void) {
Owen 0:745335502422 208
Owen 0:745335502422 209 return oneByteRead(ADXL345_TIME_FF_REG)*5;
Owen 0:745335502422 210
Owen 0:745335502422 211 }
Owen 0:745335502422 212
Owen 0:745335502422 213 void ADXL345::setFreefallTime(int freefallTime_ms) {
Owen 0:745335502422 214
Owen 0:745335502422 215 int freefallTime = freefallTime_ms / 5;
Owen 0:745335502422 216
Owen 0:745335502422 217 oneByteWrite(ADXL345_TIME_FF_REG, freefallTime);
Owen 0:745335502422 218
Owen 0:745335502422 219 }
Owen 0:745335502422 220
Owen 0:745335502422 221 int ADXL345::getTapAxisControl(void) {
Owen 0:745335502422 222
Owen 0:745335502422 223 return oneByteRead(ADXL345_TAP_AXES_REG);
Owen 0:745335502422 224
Owen 0:745335502422 225 }
Owen 0:745335502422 226
Owen 0:745335502422 227 void ADXL345::setTapAxisControl(int settings) {
Owen 0:745335502422 228
Owen 0:745335502422 229 oneByteWrite(ADXL345_TAP_AXES_REG, settings);
Owen 0:745335502422 230
Owen 0:745335502422 231 }
Owen 0:745335502422 232
Owen 0:745335502422 233 int ADXL345::getTapSource(void) {
Owen 0:745335502422 234
Owen 0:745335502422 235 return oneByteRead(ADXL345_ACT_TAP_STATUS_REG);
Owen 0:745335502422 236
Owen 0:745335502422 237 }
Owen 0:745335502422 238
Owen 0:745335502422 239 void ADXL345::setPowerMode(char mode) {
Owen 0:745335502422 240
Owen 0:745335502422 241 //Get the current register contents, so we don't clobber the rate value.
Owen 0:745335502422 242 char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
Owen 0:745335502422 243
Owen 0:745335502422 244 registerContents = (mode << 4) | registerContents;
Owen 0:745335502422 245
Owen 0:745335502422 246 oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
Owen 0:745335502422 247
Owen 0:745335502422 248 }
Owen 0:745335502422 249
Owen 0:745335502422 250 int ADXL345::getPowerControl(void) {
Owen 0:745335502422 251
Owen 0:745335502422 252 return oneByteRead(ADXL345_POWER_CTL_REG);
Owen 0:745335502422 253
Owen 0:745335502422 254 }
Owen 0:745335502422 255
Owen 0:745335502422 256 void ADXL345::setPowerControl(int settings) {
Owen 0:745335502422 257
Owen 0:745335502422 258 oneByteWrite(ADXL345_POWER_CTL_REG, settings);
Owen 0:745335502422 259
Owen 0:745335502422 260 }
Owen 0:745335502422 261
Owen 0:745335502422 262 int ADXL345::getInterruptEnableControl(void) {
Owen 0:745335502422 263
Owen 0:745335502422 264 return oneByteRead(ADXL345_INT_ENABLE_REG);
Owen 0:745335502422 265
Owen 0:745335502422 266 }
Owen 0:745335502422 267
Owen 0:745335502422 268 void ADXL345::setInterruptEnableControl(int settings) {
Owen 0:745335502422 269
Owen 0:745335502422 270 oneByteWrite(ADXL345_INT_ENABLE_REG, settings);
Owen 0:745335502422 271
Owen 0:745335502422 272 }
Owen 0:745335502422 273
Owen 0:745335502422 274 int ADXL345::getInterruptMappingControl(void) {
Owen 0:745335502422 275
Owen 0:745335502422 276 return oneByteRead(ADXL345_INT_MAP_REG);
Owen 0:745335502422 277
Owen 0:745335502422 278 }
Owen 0:745335502422 279
Owen 0:745335502422 280 void ADXL345::setInterruptMappingControl(int settings) {
Owen 0:745335502422 281
Owen 0:745335502422 282 oneByteWrite(ADXL345_INT_MAP_REG, settings);
Owen 0:745335502422 283
Owen 0:745335502422 284 }
Owen 0:745335502422 285
Owen 0:745335502422 286 int ADXL345::getInterruptSource(void){
Owen 0:745335502422 287
Owen 0:745335502422 288 return oneByteRead(ADXL345_INT_SOURCE_REG);
Owen 0:745335502422 289
Owen 0:745335502422 290 }
Owen 0:745335502422 291
Owen 0:745335502422 292 int ADXL345::getDataFormatControl(void){
Owen 0:745335502422 293
Owen 0:745335502422 294 return oneByteRead(ADXL345_DATA_FORMAT_REG);
Owen 0:745335502422 295
Owen 0:745335502422 296 }
Owen 0:745335502422 297
Owen 0:745335502422 298 void ADXL345::setDataFormatControl(int settings){
Owen 0:745335502422 299
Owen 0:745335502422 300 oneByteWrite(ADXL345_DATA_FORMAT_REG, settings);
Owen 0:745335502422 301
Owen 0:745335502422 302 }
Owen 0:745335502422 303
Owen 0:745335502422 304 void ADXL345::setDataRate(int rate) {
Owen 0:745335502422 305
Owen 0:745335502422 306 //Get the current register contents, so we don't clobber the power bit.
Owen 0:745335502422 307 char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
Owen 0:745335502422 308
Owen 0:745335502422 309 registerContents &= 0x10;
Owen 0:745335502422 310 registerContents |= rate;
Owen 0:745335502422 311
Owen 0:745335502422 312 oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
Owen 0:745335502422 313
Owen 0:745335502422 314 }
Owen 0:745335502422 315
Owen 0:745335502422 316 int ADXL345::getAx(){
Owen 0:745335502422 317
Owen 0:745335502422 318 char buffer[2];
Owen 0:745335502422 319
Owen 0:745335502422 320 multiByteRead(ADXL345_DATAX0_REG, buffer, 2);
Owen 0:745335502422 321
Owen 0:745335502422 322 return ((int)buffer[1] << 8 | (int)buffer[0]);
Owen 0:745335502422 323 }
Owen 0:745335502422 324
Owen 0:745335502422 325
Owen 0:745335502422 326 int ADXL345::getAy(){
Owen 0:745335502422 327
Owen 0:745335502422 328 char buffer[2];
Owen 0:745335502422 329
Owen 0:745335502422 330 multiByteRead(ADXL345_DATAY0_REG, buffer, 2);
Owen 0:745335502422 331
Owen 0:745335502422 332 return ((int)buffer[1] << 8 | (int)buffer[0]);
Owen 0:745335502422 333 }
Owen 0:745335502422 334
Owen 0:745335502422 335 int ADXL345::getAz(){
Owen 0:745335502422 336
Owen 0:745335502422 337 char buffer[2];
Owen 0:745335502422 338
Owen 0:745335502422 339 multiByteRead(ADXL345_DATAZ0_REG, buffer, 2);
Owen 0:745335502422 340
Owen 0:745335502422 341 return ((int)buffer[1] << 8 | (int)buffer[0]);
Owen 0:745335502422 342 }
Owen 0:745335502422 343
Owen 0:745335502422 344
Owen 0:745335502422 345
Owen 0:745335502422 346 void ADXL345::getOutput(int* readings){
Owen 0:745335502422 347
Owen 0:745335502422 348 char buffer[6];
Owen 0:745335502422 349
Owen 0:745335502422 350 multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
Owen 0:745335502422 351
Owen 0:745335502422 352 readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
Owen 0:745335502422 353 readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
Owen 0:745335502422 354 readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
Owen 0:745335502422 355
Owen 0:745335502422 356 }
Owen 0:745335502422 357
Owen 0:745335502422 358 int ADXL345::getFifoControl(void){
Owen 0:745335502422 359
Owen 0:745335502422 360 return oneByteRead(ADXL345_FIFO_CTL);
Owen 0:745335502422 361
Owen 0:745335502422 362 }
Owen 0:745335502422 363
Owen 0:745335502422 364 void ADXL345::setFifoControl(int settings){
Owen 0:745335502422 365
Owen 0:745335502422 366 oneByteWrite(ADXL345_FIFO_CTL, settings);
Owen 0:745335502422 367
Owen 0:745335502422 368 }
Owen 0:745335502422 369
Owen 0:745335502422 370 int ADXL345::getFifoStatus(void){
Owen 0:745335502422 371
Owen 0:745335502422 372 return oneByteRead(ADXL345_FIFO_STATUS);
Owen 0:745335502422 373
Owen 0:745335502422 374 }
Owen 0:745335502422 375
Owen 0:745335502422 376 int ADXL345::oneByteRead(int address) {
Owen 0:745335502422 377
Owen 0:745335502422 378 int tx = (ADXL345_SPI_READ | (address & 0x3F));
Owen 0:745335502422 379 int rx = 0;
Owen 0:745335502422 380
Owen 0:745335502422 381 nCS_ = 0;
Owen 0:745335502422 382 //Send address to read from.
Owen 0:745335502422 383 spi_.write(tx);
Owen 0:745335502422 384 //Read back contents of address.
Owen 0:745335502422 385 rx = spi_.write(0x00);
Owen 0:745335502422 386 nCS_ = 1;
Owen 0:745335502422 387
Owen 0:745335502422 388 return rx;
Owen 0:745335502422 389
Owen 0:745335502422 390 }
Owen 0:745335502422 391
Owen 0:745335502422 392 void ADXL345::oneByteWrite(int address, char data) {
Owen 0:745335502422 393
Owen 0:745335502422 394 int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
Owen 0:745335502422 395
Owen 0:745335502422 396 nCS_ = 0;
Owen 0:745335502422 397 //Send address to write to.
Owen 0:745335502422 398 spi_.write(tx);
Owen 0:745335502422 399 //Send data to be written.
Owen 0:745335502422 400 spi_.write(data);
Owen 0:745335502422 401 nCS_ = 1;
Owen 0:745335502422 402
Owen 0:745335502422 403 }
Owen 0:745335502422 404
Owen 0:745335502422 405 void ADXL345::multiByteRead(int startAddress, char* buffer, int size) {
Owen 0:745335502422 406
Owen 0:745335502422 407 int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
Owen 0:745335502422 408
Owen 0:745335502422 409 nCS_ = 0;
Owen 0:745335502422 410 //Send address to start reading from.
Owen 0:745335502422 411 spi_.write(tx);
Owen 0:745335502422 412
Owen 0:745335502422 413 for (int i = 0; i < size; i++) {
Owen 0:745335502422 414 buffer[i] = spi_.write(0x00);
Owen 0:745335502422 415 }
Owen 0:745335502422 416
Owen 0:745335502422 417 nCS_ = 1;
Owen 0:745335502422 418
Owen 0:745335502422 419 }
Owen 0:745335502422 420
Owen 0:745335502422 421 void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) {
Owen 0:745335502422 422
Owen 0:745335502422 423 int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
Owen 0:745335502422 424
Owen 0:745335502422 425 nCS_ = 0;
Owen 0:745335502422 426 //Send address to start reading from.
Owen 0:745335502422 427 spi_.write(tx);
Owen 0:745335502422 428
Owen 0:745335502422 429 for (int i = 0; i < size; i++) {
Owen 0:745335502422 430 buffer[i] = spi_.write(0x00);
Owen 0:745335502422 431 }
Owen 0:745335502422 432
Owen 0:745335502422 433 nCS_ = 1;
Owen 0:745335502422 434
Owen 0:745335502422 435 }