This library enables users to communicate with the ADXL345 accelerometer through the I2C bus on the mbed. The API names are similar and work nearly the same way as those made in the SPI libraries for the ADXL345.

Dependencies:   mbed

Dependents:   sensors-example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADXL345_I2C.cpp Source File

ADXL345_I2C.cpp

00001 /**
00002  * @author Peter Swanson
00003  * A personal note from me: Jesus Christ has changed my life so much it blows my mind. I say this because
00004  *                  today, religion is thought of as something that you do or believe and has about as
00005  *                  little impact on a person as their political stance. But for me, God gives me daily
00006  *                  strength and has filled my life with the satisfaction that I could never find in any
00007  *                  of the other things that I once looked for it in. 
00008  * If your interested, heres verse that changed my life:
00009  *      Rom 8:1-3: "Therefore, there is now no condemnation for those who are in Christ Jesus,
00010  *                  because through Christ Jesus, the law of the Spirit who gives life has set
00011  *                  me free from the law of sin (which brings...) and death. For what the law 
00012  *                  was powerless to do in that it was weakened by the flesh, God did by sending
00013  *                  His own Son in the likeness of sinful flesh to be a sin offering. And so He
00014  *                  condemned sin in the flesh in order that the righteous requirements of the 
00015  *                  (God's) law might be fully met in us, who live not according to the flesh
00016  *                  but according to the Spirit."
00017  *
00018  *  A special thanks to Ewout van Bekkum for all his patient help in developing this library!
00019  *
00020  * @section LICENSE
00021  *
00022  * Permission is hereby granted, free of charge, to any person obtaining a copy
00023  * of this software and associated documentation files (the "Software"), to deal
00024  * in the Software without restriction, including without limitation the rights
00025  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00026  * copies of the Software, and to permit persons to whom the Software is
00027  * furnished to do so, subject to the following conditions:
00028  *
00029  * The above copyright notice and this permission notice shall be included in
00030  * all copies or substantial portions of the Software.
00031  *
00032  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00033  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00034  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00035  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00036  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00037  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00038  * THE SOFTWARE.
00039  *
00040  * @section DESCRIPTION
00041  *
00042  * ADXL345, triple axis, I2C interface, accelerometer.
00043  *
00044  * Datasheet:
00045  *
00046  * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
00047  */  
00048  
00049 /**
00050  * Includes
00051  */
00052 #include "ADXL345_I2C.h"
00053 
00054 //#include "mbed.h"
00055 
00056 ADXL345_I2C::ADXL345_I2C(PinName sda, PinName scl) : i2c_(sda, scl) {
00057 
00058     //400kHz, allowing us to use the fastest data rates.
00059     i2c_.frequency(400000);   
00060 // initialize the BW data rate
00061     char tx[2];
00062     tx[0] = ADXL345_BW_RATE_REG;
00063     tx[1] = ADXL345_1600HZ; //value greater than or equal to 0x0A is written into the rate bits (Bit D3 through Bit D0) in the BW_RATE register 
00064  i2c_.write( ADXL345_I2C_WRITE , tx, 2);  
00065 
00066 //Data format (for +-16g) - This is done by setting Bit D3 of the DATA_FORMAT register (Address 0x31) and writing a value of 0x03 to the range bits (Bit D1 and Bit D0) of the DATA_FORMAT register (Address 0x31).
00067    
00068  char rx[2];
00069     rx[0] = ADXL345_DATA_FORMAT_REG;
00070     rx[1] = 0x0B; 
00071      // full res and +_16g
00072  i2c_.write( ADXL345_I2C_WRITE , rx, 2); 
00073  
00074  // Set Offset  - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE.
00075   char x[2];
00076     x[0] = ADXL345_OFSX_REG ;
00077     x[1] = 0xFD; 
00078  i2c_.write( ADXL345_I2C_WRITE , x, 2);
00079   char y[2];
00080     y[0] = ADXL345_OFSY_REG ;
00081     y[1] = 0x03; 
00082  i2c_.write( ADXL345_I2C_WRITE , y, 2);
00083  char z[2];
00084     z[0] = ADXL345_OFSZ_REG ;
00085     z[1] = 0xFE; 
00086  i2c_.write( ADXL345_I2C_WRITE , z, 2);
00087 }
00088 
00089 
00090 char ADXL345_I2C::SingleByteRead(char address){   
00091    char tx = address;
00092    char output; 
00093     i2c_.write( ADXL345_I2C_WRITE , &tx, 1);  //tell it what you want to read
00094     i2c_.read( ADXL345_I2C_READ , &output, 1);    //tell it where to store the data
00095     return output;
00096   
00097 }
00098 
00099 
00100 /*
00101 ***info on the i2c_.write***
00102 address     8-bit I2C slave address [ addr | 0 ]
00103 data        Pointer to the byte-array data to send
00104 length        Number of bytes to send
00105 repeated    Repeated start, true - do not send stop at end
00106 returns     0 on success (ack), or non-0 on failure (nack)
00107 */
00108 
00109 int ADXL345_I2C::SingleByteWrite(char address, char data){ 
00110    int ack = 0;
00111    char tx[2];
00112    tx[0] = address;
00113    tx[1] = data;
00114    return   ack | i2c_.write( ADXL345_I2C_WRITE , tx, 2);   
00115 }
00116 
00117 
00118 
00119 void ADXL345_I2C::multiByteRead(char address, char* output, int size) {
00120     i2c_.write( ADXL345_I2C_WRITE, &address, 1);  //tell it where to read from
00121     i2c_.read( ADXL345_I2C_READ , output, size);      //tell it where to store the data read
00122 }
00123 
00124 
00125 int ADXL345_I2C::multiByteWrite(char address, char* ptr_data, int size) {
00126         int ack;
00127    
00128                ack = i2c_.write( ADXL345_I2C_WRITE, &address, 1);  //tell it where to write to
00129         return ack | i2c_.write( ADXL345_I2C_READ, ptr_data, size);  //tell it what data to write
00130                                     
00131 }
00132 
00133 
00134 void ADXL345_I2C::getOutput(int* readings){
00135     char buffer[6];    
00136     multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
00137     
00138     readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
00139     readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
00140     readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
00141 
00142 }
00143 
00144 
00145 
00146 char ADXL345_I2C::getDeviceID() {  
00147     return SingleByteRead(ADXL345_DEVID_REG);
00148     }
00149 //
00150 int ADXL345_I2C::setPowerMode(char mode) { 
00151 
00152     //Get the current register contents, so we don't clobber the rate value.
00153     char registerContents = (mode << 4) | SingleByteRead(ADXL345_BW_RATE_REG);
00154 
00155    return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents);
00156 
00157 }
00158 
00159 char ADXL345_I2C::getPowerControl() {    
00160     return SingleByteRead(ADXL345_POWER_CTL_REG);
00161 }
00162 
00163 int ADXL345_I2C::setPowerControl(char settings) {    
00164     return SingleByteWrite(ADXL345_POWER_CTL_REG, settings);
00165 
00166 }
00167 
00168 
00169 
00170 char ADXL345_I2C::getDataFormatControl(void){
00171 
00172     return SingleByteRead(ADXL345_DATA_FORMAT_REG);
00173 }
00174 
00175 int ADXL345_I2C::setDataFormatControl(char settings){
00176 
00177    return SingleByteWrite(ADXL345_DATA_FORMAT_REG, settings);
00178     
00179 }
00180 
00181 int ADXL345_I2C::setDataRate(char rate) {
00182 
00183     //Get the current register contents, so we don't clobber the power bit.
00184     char registerContents = SingleByteRead(ADXL345_BW_RATE_REG);
00185 
00186     registerContents &= 0x10;
00187     registerContents |= rate;
00188 
00189     return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents);
00190 
00191 }
00192 
00193 
00194 char ADXL345_I2C::getOffset(char axis) {     
00195 
00196     char address = 0;
00197 
00198     if (axis == ADXL345_X) {
00199         address = ADXL345_OFSX_REG;
00200     } else if (axis == ADXL345_Y) {
00201         address = ADXL345_OFSY_REG;
00202     } else if (axis == ADXL345_Z) {
00203         address = ADXL345_OFSZ_REG;
00204     }
00205 
00206    return SingleByteRead(address);
00207 }
00208 
00209 int ADXL345_I2C::setOffset(char axis, char offset) {        
00210 
00211     char address = 0;
00212 
00213     if (axis == ADXL345_X) {
00214         address = ADXL345_OFSX_REG;
00215     } else if (axis == ADXL345_Y) {
00216         address = ADXL345_OFSY_REG;
00217     } else if (axis == ADXL345_Z) {
00218         address = ADXL345_OFSZ_REG;
00219     }
00220 
00221    return SingleByteWrite(address, offset);
00222 
00223 }
00224 
00225 
00226 char ADXL345_I2C::getFifoControl(void){
00227 
00228     return SingleByteRead(ADXL345_FIFO_CTL);
00229 
00230 }
00231 
00232 int ADXL345_I2C::setFifoControl(char settings){
00233    return SingleByteWrite(ADXL345_FIFO_STATUS, settings);
00234 
00235 }
00236 
00237 char ADXL345_I2C::getFifoStatus(void){
00238 
00239     return SingleByteRead(ADXL345_FIFO_STATUS);
00240 
00241 }
00242 
00243 
00244 
00245 char ADXL345_I2C::getTapThreshold(void) {
00246 
00247     return SingleByteRead(ADXL345_THRESH_TAP_REG);
00248 }
00249 
00250 int ADXL345_I2C::setTapThreshold(char threshold) {   
00251 
00252    return SingleByteWrite(ADXL345_THRESH_TAP_REG, threshold);
00253 
00254 }
00255 
00256 
00257 float ADXL345_I2C::getTapDuration(void) {     
00258 
00259     return (float)SingleByteRead(ADXL345_DUR_REG)*625;
00260 }
00261 
00262 int ADXL345_I2C::setTapDuration(short int duration_us) {
00263 
00264     short int tapDuration = duration_us / 625;
00265     char tapChar[2];
00266      tapChar[0] = (tapDuration & 0x00FF);
00267      tapChar[1] = (tapDuration >> 8) & 0x00FF;
00268     return multiByteWrite(ADXL345_DUR_REG, tapChar, 2);
00269 
00270 }
00271 
00272 float ADXL345_I2C::getTapLatency(void) {
00273 
00274     return (float)SingleByteRead(ADXL345_LATENT_REG)*1.25;
00275 }
00276 
00277 int ADXL345_I2C::setTapLatency(short int latency_ms) {
00278 
00279     latency_ms = latency_ms / 1.25;
00280     char latChar[2];
00281      latChar[0] = (latency_ms & 0x00FF);
00282      latChar[1] = (latency_ms << 8) & 0xFF00;
00283     return multiByteWrite(ADXL345_LATENT_REG, latChar, 2);
00284 
00285 }
00286 
00287 float ADXL345_I2C::getWindowTime(void) {
00288 
00289     return (float)SingleByteRead(ADXL345_WINDOW_REG)*1.25;
00290 }
00291 
00292 int ADXL345_I2C::setWindowTime(short int window_ms) {
00293 
00294     window_ms = window_ms / 1.25;
00295     char windowChar[2];
00296     windowChar[0] = (window_ms & 0x00FF);
00297     windowChar[1] = ((window_ms << 8) & 0xFF00);
00298    return multiByteWrite(ADXL345_WINDOW_REG, windowChar, 2);
00299 
00300 }
00301 
00302 char ADXL345_I2C::getActivityThreshold(void) {
00303 
00304     return SingleByteRead(ADXL345_THRESH_ACT_REG);
00305 }
00306 
00307 int ADXL345_I2C::setActivityThreshold(char threshold) {
00308     return SingleByteWrite(ADXL345_THRESH_ACT_REG, threshold);
00309 
00310 }
00311 
00312 char ADXL345_I2C::getInactivityThreshold(void) {
00313     return SingleByteRead(ADXL345_THRESH_INACT_REG);
00314        
00315 }
00316 
00317 //int FUNCTION(short int * ptr_Output)
00318 //short int FUNCTION ()
00319 
00320 int ADXL345_I2C::setInactivityThreshold(char threshold) {
00321     return SingleByteWrite(ADXL345_THRESH_INACT_REG, threshold);
00322 
00323 }
00324 
00325 char ADXL345_I2C::getTimeInactivity(void) {
00326 
00327     return SingleByteRead(ADXL345_TIME_INACT_REG);
00328 
00329 }
00330 
00331 int ADXL345_I2C::setTimeInactivity(char timeInactivity) {
00332     return SingleByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
00333 
00334 }
00335 
00336 char ADXL345_I2C::getActivityInactivityControl(void) {
00337 
00338     return SingleByteRead(ADXL345_ACT_INACT_CTL_REG);
00339 
00340 }
00341 
00342 int ADXL345_I2C::setActivityInactivityControl(char settings) {
00343     return SingleByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
00344     
00345 }
00346 
00347 char ADXL345_I2C::getFreefallThreshold(void) {
00348 
00349     return SingleByteRead(ADXL345_THRESH_FF_REG);
00350 
00351 }
00352 
00353 int ADXL345_I2C::setFreefallThreshold(char threshold) {
00354    return SingleByteWrite(ADXL345_THRESH_FF_REG, threshold);
00355 
00356 }
00357 
00358 char ADXL345_I2C::getFreefallTime(void) {
00359 
00360     return SingleByteRead(ADXL345_TIME_FF_REG)*5;
00361 
00362 }
00363 
00364 int ADXL345_I2C::setFreefallTime(short int freefallTime_ms) {
00365      freefallTime_ms = freefallTime_ms / 5;
00366      char fallChar[2];
00367      fallChar[0] = (freefallTime_ms & 0x00FF);
00368      fallChar[1] = (freefallTime_ms << 8) & 0xFF00;
00369     
00370     return multiByteWrite(ADXL345_TIME_FF_REG, fallChar, 2);
00371 
00372 }
00373 
00374 char ADXL345_I2C::getTapAxisControl(void) {
00375 
00376     return SingleByteRead(ADXL345_TAP_AXES_REG);
00377 
00378 }
00379 
00380 int ADXL345_I2C::setTapAxisControl(char settings) {
00381    return SingleByteWrite(ADXL345_TAP_AXES_REG, settings);
00382 
00383 }
00384 
00385 char ADXL345_I2C::getTapSource(void) {
00386 
00387     return SingleByteRead(ADXL345_ACT_TAP_STATUS_REG);
00388 
00389 }
00390 
00391 
00392 
00393 char ADXL345_I2C::getInterruptEnableControl(void) {
00394 
00395     return SingleByteRead(ADXL345_INT_ENABLE_REG);
00396 
00397 }
00398 
00399 int ADXL345_I2C::setInterruptEnableControl(char settings) {
00400    return SingleByteWrite(ADXL345_INT_ENABLE_REG, settings);
00401 
00402 }
00403 
00404 char ADXL345_I2C::getInterruptMappingControl(void) {
00405 
00406     return SingleByteRead(ADXL345_INT_MAP_REG);
00407 
00408 }
00409 
00410 int ADXL345_I2C::setInterruptMappingControl(char settings) {
00411     return SingleByteWrite(ADXL345_INT_MAP_REG, settings);
00412 
00413 }
00414 
00415 char ADXL345_I2C::getInterruptSource(void){
00416 
00417     return SingleByteRead(ADXL345_INT_SOURCE_REG);
00418 
00419 }
00420 
00421 
00422 
00423