albatross / ADXL345_I2C

Fork of ADXL345_I2C by Ian Molesworth

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADXL345_I2C.cpp Source File

ADXL345_I2C.cpp

00001 /**
00002  * @author Jose R. Padron
00003  * 
00004  * @section LICENSE
00005  *
00006  * Copyright (c) 2010 ARM Limited
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  * @section DESCRIPTION
00027  *
00028  * ADXL345, triple axis, digital interface, accelerometer.
00029  *
00030  * Datasheet:
00031  *
00032  * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
00033  */  
00034  
00035 /**
00036  * Includes
00037  */
00038 #include "ADXL345_I2C.h"
00039 
00040 ADXL345_I2C::ADXL345_I2C(PinName sda, 
00041                  PinName scl) : i2c_(sda,scl) {
00042 
00043      
00044     //100KHz, as specified by the datasheet.
00045     i2c_.frequency(100000);
00046     wait_us(500);
00047 }
00048 
00049 char ADXL345_I2C::getDevId(void) {
00050 
00051     return oneByteRead(ADXL345_DEVID_REG);
00052 
00053 }
00054 
00055 int ADXL345_I2C::getTapThreshold(void) {
00056 
00057     return oneByteRead(ADXL345_THRESH_TAP_REG);
00058 
00059 }
00060 
00061 void ADXL345_I2C::setTapThreshold(int threshold) {
00062 
00063     oneByteWrite(ADXL345_THRESH_TAP_REG, threshold);
00064 
00065 }
00066 
00067 int ADXL345_I2C::getOffset(int axis) {
00068 
00069     int address = 0;
00070 
00071     if (axis == ADXL345_X) {
00072         address = ADXL345_OFSX_REG;
00073     } else if (axis == ADXL345_Y) {
00074         address = ADXL345_OFSY_REG;
00075     } else if (axis == ADXL345_Z) {
00076         address = ADXL345_OFSZ_REG;
00077     }
00078 
00079     return oneByteRead(address);
00080 
00081 }
00082 
00083 void ADXL345_I2C::setOffset(int axis, char offset) {
00084 
00085     char address = 0;
00086 
00087     if (axis == ADXL345_X) {
00088         address = ADXL345_OFSX_REG;
00089     } else if (axis == ADXL345_Y) {
00090         address = ADXL345_OFSY_REG;
00091     } else if (axis == ADXL345_Z) {
00092         address = ADXL345_OFSZ_REG;
00093     }
00094 
00095     return oneByteWrite(address, offset);
00096 
00097 }
00098 
00099 int ADXL345_I2C::getTapDuration(void) {
00100 
00101     return oneByteRead(ADXL345_DUR_REG)*625;
00102 
00103 }
00104 
00105 void ADXL345_I2C::setTapDuration(int duration_us) {
00106 
00107     int tapDuration = duration_us / 625;
00108 
00109     oneByteWrite(ADXL345_DUR_REG, tapDuration);
00110 
00111 }
00112 
00113 float ADXL345_I2C::getTapLatency(void) {
00114 
00115     return oneByteRead(ADXL345_LATENT_REG)*1.25;
00116 
00117 }
00118 
00119 void ADXL345_I2C::setTapLatency(int latency_ms) {
00120 
00121     int tapLatency = latency_ms / 1.25;
00122 
00123     oneByteWrite(ADXL345_LATENT_REG, tapLatency);
00124 
00125 }
00126 
00127 float ADXL345_I2C::getWindowTime(void) {
00128 
00129     return oneByteRead(ADXL345_WINDOW_REG)*1.25;
00130 
00131 }
00132 
00133 void ADXL345_I2C::setWindowTime(int window_ms) {
00134 
00135     int windowTime = window_ms / 1.25;
00136 
00137     oneByteWrite(ADXL345_WINDOW_REG, windowTime);
00138 
00139 }
00140 
00141 int ADXL345_I2C::getActivityThreshold(void) {
00142 
00143     return oneByteRead(ADXL345_THRESH_ACT_REG);
00144 
00145 }
00146 
00147 void ADXL345_I2C::setActivityThreshold(int threshold) {
00148 
00149     oneByteWrite(ADXL345_THRESH_ACT_REG, threshold);
00150 
00151 }
00152 
00153 int ADXL345_I2C::getInactivityThreshold(void) {
00154 
00155     return oneByteRead(ADXL345_THRESH_INACT_REG);
00156 
00157 }
00158 
00159 void ADXL345_I2C::setInactivityThreshold(int threshold) {
00160 
00161     return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold);
00162 
00163 }
00164 
00165 int ADXL345_I2C::getTimeInactivity(void) {
00166 
00167     return oneByteRead(ADXL345_TIME_INACT_REG);
00168 
00169 }
00170 
00171 void ADXL345_I2C::setTimeInactivity(int timeInactivity) {
00172 
00173     oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
00174 
00175 }
00176 
00177 int ADXL345_I2C::getActivityInactivityControl(void) {
00178 
00179     return oneByteRead(ADXL345_ACT_INACT_CTL_REG);
00180 
00181 }
00182 
00183 void ADXL345_I2C::setActivityInactivityControl(int settings) {
00184 
00185     oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
00186 
00187 }
00188 
00189 int ADXL345_I2C::getFreefallThreshold(void) {
00190 
00191     return oneByteRead(ADXL345_THRESH_FF_REG);
00192 
00193 }
00194 
00195 void ADXL345_I2C::setFreefallThreshold(int threshold) {
00196 
00197     oneByteWrite(ADXL345_THRESH_FF_REG, threshold);
00198 
00199 }
00200 
00201 int ADXL345_I2C::getFreefallTime(void) {
00202 
00203     return oneByteRead(ADXL345_TIME_FF_REG)*5;
00204 
00205 }
00206 
00207 void ADXL345_I2C::setFreefallTime(int freefallTime_ms) {
00208 
00209     int freefallTime = freefallTime_ms / 5;
00210 
00211     oneByteWrite(ADXL345_TIME_FF_REG, freefallTime);
00212 
00213 }
00214 
00215 int ADXL345_I2C::getTapAxisControl(void) {
00216 
00217     return oneByteRead(ADXL345_TAP_AXES_REG);
00218 
00219 }
00220 
00221 void ADXL345_I2C::setTapAxisControl(int settings) {
00222 
00223     oneByteWrite(ADXL345_TAP_AXES_REG, settings);
00224 
00225 }
00226 
00227 int ADXL345_I2C::getTapSource(void) {
00228 
00229     return oneByteRead(ADXL345_ACT_TAP_STATUS_REG);
00230 
00231 }
00232 
00233 void ADXL345_I2C::setPowerMode(char mode) {
00234 
00235     //Get the current register contents, so we don't clobber the rate value.
00236     char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
00237 
00238     registerContents = (mode << 4) | registerContents;
00239 
00240     oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
00241 
00242 }
00243 
00244 int ADXL345_I2C::getPowerControl(void) {
00245 
00246     return oneByteRead(ADXL345_POWER_CTL_REG);
00247 
00248 }
00249 
00250 void ADXL345_I2C::setPowerControl(int settings) {
00251 
00252     oneByteWrite(ADXL345_POWER_CTL_REG, settings);
00253 
00254 }
00255 
00256 int ADXL345_I2C::getInterruptEnableControl(void) {
00257 
00258     return oneByteRead(ADXL345_INT_ENABLE_REG);
00259 
00260 }
00261 
00262 void ADXL345_I2C::setInterruptEnableControl(int settings) {
00263 
00264     oneByteWrite(ADXL345_INT_ENABLE_REG, settings);
00265 
00266 }
00267 
00268 int ADXL345_I2C::getInterruptMappingControl(void) {
00269 
00270     return oneByteRead(ADXL345_INT_MAP_REG);
00271 
00272 }
00273 
00274 void ADXL345_I2C::setInterruptMappingControl(int settings) {
00275 
00276     oneByteWrite(ADXL345_INT_MAP_REG, settings);
00277 
00278 }
00279 
00280 int ADXL345_I2C::getInterruptSource(void){
00281 
00282     return oneByteRead(ADXL345_INT_SOURCE_REG);
00283 
00284 }
00285 
00286 int ADXL345_I2C::getDataFormatControl(void){
00287 
00288     return oneByteRead(ADXL345_DATA_FORMAT_REG);
00289 
00290 }
00291 
00292 void ADXL345_I2C::setDataFormatControl(int settings){
00293 
00294     oneByteWrite(ADXL345_DATA_FORMAT_REG, settings);
00295 
00296 }
00297 
00298 void ADXL345_I2C::setDataRate(int rate) {
00299 
00300     //Get the current register contents, so we don't clobber the power bit.
00301     char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
00302 
00303     registerContents &= 0x10;
00304     registerContents |= rate;
00305 
00306     oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
00307 
00308 }
00309 
00310 int ADXL345_I2C::getAx(){
00311 
00312     char buffer[2];
00313     
00314     TwoByteRead(ADXL345_DATAX0_REG, buffer);
00315     
00316     return ((int)buffer[1] << 8 | (int)buffer[0]);
00317 }
00318 
00319 
00320 int ADXL345_I2C::getAy(){
00321 
00322     char buffer[2];
00323     
00324     TwoByteRead(ADXL345_DATAY0_REG, buffer);
00325     
00326     return ((int)buffer[1] << 8 | (int)buffer[0]);
00327 }
00328 
00329 int ADXL345_I2C::getAz(){
00330 
00331     char buffer[2];
00332     
00333     TwoByteRead(ADXL345_DATAZ0_REG, buffer);
00334     
00335     return ((int)buffer[1] << 8 | (int)buffer[0]);
00336 }
00337 
00338 
00339 
00340 void ADXL345_I2C::getOutput(int* readings){
00341     
00342       char buffer[2];
00343     
00344     TwoByteRead(ADXL345_DATAX0_REG, buffer);
00345     readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
00346     TwoByteRead(ADXL345_DATAY0_REG, buffer);
00347     readings[1] = (int)buffer[1] << 8 | (int)buffer[0];
00348     TwoByteRead(ADXL345_DATAZ0_REG, buffer);
00349     readings[2] = (int)buffer[1] << 8 | (int)buffer[0];
00350 
00351 }
00352 
00353 int ADXL345_I2C::getFifoControl(void){
00354 
00355     return oneByteRead(ADXL345_FIFO_CTL);
00356 
00357 }
00358 
00359 void ADXL345_I2C::setFifoControl(int settings){
00360 
00361     oneByteWrite(ADXL345_FIFO_STATUS, settings);
00362 
00363 }
00364 
00365 int ADXL345_I2C::getFifoStatus(void){
00366 
00367     return oneByteRead(ADXL345_FIFO_STATUS);
00368 
00369 }
00370 
00371 char ADXL345_I2C::oneByteRead(char address) {
00372 
00373    
00374    char  rx[1];
00375    char tx[1];
00376  
00377   // nCS_ = 1;  
00378    tx[0]=address;
00379     
00380        
00381     i2c_.write(ADXL345_I2C_WRITE, tx,1);
00382     i2c_.read(ADXL345_I2C_READ,rx,1);
00383     
00384   //nCS_ = 0;
00385     return rx[0];
00386 
00387 }
00388 
00389 void ADXL345_I2C::oneByteWrite(char address, char data) {
00390  // nCS_ = 1;
00391     char tx[2];
00392    
00393     tx[0]=address;
00394     tx[1]=data;
00395 
00396     i2c_.write(ADXL345_I2C_WRITE,tx,2);
00397    
00398   //nCS_ = 0;
00399 }
00400 
00401 void ADXL345_I2C::TwoByteRead(char startAddress, char* buffer) {
00402 
00403 
00404    // nCS_ = 1;
00405     //Send address to start reading from.
00406     char tx[1];
00407     tx[0]=startAddress;
00408     i2c_.write(ADXL345_I2C_WRITE,tx,1);
00409     i2c_.read(ADXL345_I2C_READ,buffer,2);
00410     
00411   //nCS_ = 0;
00412 }
00413 
00414 void ADXL345_I2C::TwoByteWrite(char startAddress, char* buffer) {
00415 
00416 //nCS_ = 1;
00417     //Send address to start reading from.
00418     char tx[1];
00419     tx[0]=startAddress;
00420         i2c_.write(ADXL345_I2C_WRITE,tx,1);
00421          i2c_.write(ADXL345_I2C_WRITE,buffer,2);
00422     
00423     
00424  //nCS_ = 0;
00425 
00426 }