this is program how build nRF51822 to get ADXL345 data

Dependencies:   BLE_API mbed nRF51822

Fork of ADXL345_I2C by Peter Swanson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADXL345_I2C.h Source File

ADXL345_I2C.h

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 
00051 #ifndef ADXL345_I2C_H
00052 #define ADXL345_I2C_H
00053 
00054 /**
00055  * Includes
00056  */
00057 #include "mbed.h"
00058 
00059 /**
00060  * Defines
00061  */
00062 //Registers.
00063 #define ADXL345_DEVID_REG          0x00
00064 #define ADXL345_THRESH_TAP_REG     0x1D
00065 #define ADXL345_OFSX_REG           0x1E
00066 #define ADXL345_OFSY_REG           0x1F
00067 #define ADXL345_OFSZ_REG           0x20
00068 #define ADXL345_DUR_REG            0x21
00069 #define ADXL345_LATENT_REG         0x22
00070 #define ADXL345_WINDOW_REG         0x23
00071 #define ADXL345_THRESH_ACT_REG     0x24
00072 #define ADXL345_THRESH_INACT_REG   0x25
00073 #define ADXL345_TIME_INACT_REG     0x26
00074 #define ADXL345_ACT_INACT_CTL_REG  0x27
00075 #define ADXL345_THRESH_FF_REG      0x28
00076 #define ADXL345_TIME_FF_REG        0x29
00077 #define ADXL345_TAP_AXES_REG       0x2A
00078 #define ADXL345_ACT_TAP_STATUS_REG 0x2B
00079 #define ADXL345_BW_RATE_REG        0x2C
00080 #define ADXL345_POWER_CTL_REG      0x2D
00081 #define ADXL345_INT_ENABLE_REG     0x2E
00082 #define ADXL345_INT_MAP_REG        0x2F
00083 #define ADXL345_INT_SOURCE_REG     0x30
00084 #define ADXL345_DATA_FORMAT_REG    0x31
00085 #define ADXL345_DATAX0_REG         0x32
00086 #define ADXL345_DATAX1_REG         0x33
00087 #define ADXL345_DATAY0_REG         0x34
00088 #define ADXL345_DATAY1_REG         0x35
00089 #define ADXL345_DATAZ0_REG         0x36
00090 #define ADXL345_DATAZ1_REG         0x37
00091 #define ADXL345_FIFO_CTL           0x38
00092 #define ADXL345_FIFO_STATUS        0x39
00093 
00094 //Data rate codes.
00095 #define ADXL345_3200HZ      0x0F
00096 #define ADXL345_1600HZ      0x0E
00097 #define ADXL345_800HZ       0x0D
00098 #define ADXL345_400HZ       0x0C
00099 #define ADXL345_200HZ       0x0B
00100 #define ADXL345_100HZ       0x0A
00101 #define ADXL345_50HZ        0x09
00102 #define ADXL345_25HZ        0x08
00103 #define ADXL345_12HZ5       0x07
00104 #define ADXL345_6HZ25       0x06
00105 
00106 // read or write bytes
00107 #define ADXL345_I2C_READ    0xA7  
00108 #define ADXL345_I2C_WRITE   0xA6 
00109 #define ADXL345_I2C_ADDRESS 0x53   //the ADXL345 7-bit address is 0x53 when ALT ADDRESS is low as it is on the sparkfun chip: when ALT ADDRESS is high the address is 0x1D
00110 
00111 /////////////when ALT ADDRESS pin is high:
00112 //#define ADXL345_I2C_READ    0x3B   
00113 //#define ADXL345_I2C_WRITE   0x3A
00114 //#define ADXL345_I2C_ADDRESS 0x1D 
00115 
00116 #define ADXL345_X           0x00
00117 #define ADXL345_Y           0x01
00118 #define ADXL345_Z           0x02
00119 
00120 
00121 
00122 // modes
00123 #define MeasurementMode     0x08
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 class ADXL345_I2C {
00132 
00133 public:
00134 
00135     /**
00136      * Constructor.
00137      *
00138      * @param mosi mbed pin to use for SDA line of I2C interface.
00139      * @param sck mbed pin to use for SCL line of I2C interface.
00140      */
00141     ADXL345_I2C(PinName sda, PinName scl);
00142 
00143     /**
00144      * Get the output of all three axes.
00145      *
00146      * @param Pointer to a buffer to hold the accelerometer value for the
00147      *        x-axis, y-axis and z-axis [in that order].
00148      */
00149     void getOutput(int* readings);
00150 
00151     /**
00152      * Read the device ID register on the device.
00153      *
00154      * @return The device ID code [0xE5]
00155      */
00156     char getDeviceID(void);
00157 
00158 
00159     
00160      /**
00161      * Set the power mode.
00162      *
00163      * @param mode 0 -> Normal operation.
00164      *             1 -> Reduced power operation.
00165      */     
00166 int setPowerMode(char mode);
00167   
00168      /**
00169      * Set the power control settings.
00170      *
00171      * See datasheet for details.
00172      *
00173      * @param The control byte to write to the POWER_CTL register.
00174      */
00175  int setPowerControl(char settings);     
00176       /**
00177      * Get the power control settings.
00178      *
00179      * See datasheet for details.
00180      *
00181      * @return The contents of the POWER_CTL register.
00182      */
00183     char getPowerControl(void);
00184 
00185        
00186     /**
00187      * Get the data format settings.
00188      *
00189      * @return The contents of the DATA_FORMAT register.
00190      */
00191      
00192     char getDataFormatControl(void);
00193     
00194     /**
00195      * Set the data format settings.
00196      *
00197      * @param settings The control byte to write to the DATA_FORMAT register.
00198      */
00199     int setDataFormatControl(char settings);
00200   
00201        /**
00202      * Set the data rate.
00203      *
00204      * @param rate The rate code (see #defines or datasheet).
00205      */
00206     int setDataRate(char rate);
00207     
00208 
00209        /**
00210      * Get the current offset for a particular axis.
00211      *
00212      * @param axis 0x00 -> X-axis
00213      *             0x01 -> Y-axis
00214      *             0x02 -> Z-axis
00215      * @return The current offset as an 8-bit 2's complement number with scale
00216      *         factor 15.6mg/LSB.
00217      */
00218      
00219        char getOffset(char axis);
00220 
00221     /**
00222      * Set the offset for a particular axis.
00223      *
00224      * @param axis 0x00 -> X-axis
00225      *             0x01 -> Y-axis
00226      *             0x02 -> Z-axis
00227      * @param offset The offset as an 8-bit 2's complement number with scale
00228      *               factor 15.6mg/LSB.
00229      */
00230     int setOffset(char axis, char offset);
00231 
00232 
00233     
00234     /**
00235      * Get the FIFO control settings.
00236      *
00237      * @return The contents of the FIFO_CTL register.
00238      */
00239     char getFifoControl(void);
00240     
00241     /**
00242      * Set the FIFO control settings.
00243      *
00244      * @param The control byte to write to the FIFO_CTL register.
00245      */
00246     int setFifoControl(char settings);
00247     
00248     /**
00249      * Get FIFO status.
00250      *
00251      * @return The contents of the FIFO_STATUS register.
00252      */
00253     char getFifoStatus(void);
00254     
00255     /**
00256      * Read the tap threshold on the device.
00257      *
00258      * @return The tap threshold as an 8-bit number with a scale factor of
00259      *         62.5mg/LSB.
00260      */
00261     char getTapThreshold(void);
00262 
00263     /**
00264      * Set the tap threshold.
00265      *
00266      * @param The tap threshold as an 8-bit number with a scale factor of
00267      *        62.5mg/LSB.
00268      */
00269     int setTapThreshold(char threshold);
00270 
00271     /**
00272      * Get the tap duration required to trigger an event.
00273      *
00274      * @return The max time that an event must be above the tap threshold to
00275      *         qualify as a tap event, in microseconds.
00276      */
00277     float getTapDuration(void);
00278 
00279     /**
00280      * Set the tap duration required to trigger an event.
00281      *
00282      * @param duration_us The max time that an event must be above the tap
00283      *                    threshold to qualify as a tap event, in microseconds.
00284      *                    Time will be normalized by the scale factor which is
00285      *                    625us/LSB. A value of 0 disables the single/double
00286      *                    tap functions.
00287      */
00288     int setTapDuration(short int duration_us);
00289 
00290     /**
00291      * Get the tap latency between the detection of a tap and the time window.
00292      *
00293      * @return The wait time from the detection of a tap event to the start of
00294      *         the time window during which a possible second tap event can be
00295      *         detected in milliseconds.
00296      */
00297     float getTapLatency(void);
00298 
00299     /**
00300      * Set the tap latency between the detection of a tap and the time window.
00301      *
00302      * @param latency_ms The wait time from the detection of a tap event to the
00303      *                   start of the time window during which a possible
00304      *                   second tap event can be detected in milliseconds.
00305      *                   A value of 0 disables the double tap function.
00306      */
00307     int setTapLatency(short int latency_ms);
00308 
00309     /**
00310      * Get the time of window between tap latency and a double tap.
00311      *
00312      * @return The amount of time after the expiration of the latency time
00313      *         during which a second valid tap can begin, in milliseconds.
00314      */
00315     float getWindowTime(void);
00316 
00317     /**
00318      * Set the time of the window between tap latency and a double tap.
00319      *
00320      * @param window_ms The amount of time after the expiration of the latency
00321      *                  time during which a second valid tap can begin,
00322      *                  in milliseconds.
00323      */
00324     int setWindowTime(short int window_ms);
00325 
00326     /**
00327      * Get the threshold value for detecting activity.
00328      *
00329      * @return The threshold value for detecting activity as an 8-bit number.
00330      *         Scale factor is 62.5mg/LSB.
00331      */
00332      char getActivityThreshold(void);
00333 
00334     /**
00335      * Set the threshold value for detecting activity.
00336      *
00337      * @param threshold The threshold value for detecting activity as an 8-bit
00338      *                  number. Scale factor is 62.5mg/LSB. A value of 0 may
00339      *                  result in undesirable behavior if the activity
00340      *                  interrupt is enabled.
00341      */
00342     int setActivityThreshold(char threshold);
00343 
00344     /**
00345      * Get the threshold value for detecting inactivity.
00346      *
00347      * @return The threshold value for detecting inactivity as an 8-bit number.
00348      *         Scale factor is 62.5mg/LSB.
00349      */
00350      char getInactivityThreshold(void);
00351 
00352     /**
00353      * Set the threshold value for detecting inactivity.
00354      *
00355      * @param threshold The threshold value for detecting inactivity as an
00356      *                  8-bit number. Scale factor is 62.5mg/LSB.
00357      */
00358     int setInactivityThreshold(char threshold);
00359 
00360     /**
00361      * Get the time required for inactivity to be declared.
00362      *
00363      * @return The amount of time that acceleration must be less than the
00364      *         inactivity threshold for inactivity to be declared, in
00365      *         seconds.
00366      */
00367      char getTimeInactivity(void);
00368     
00369     /**
00370      * Set the time required for inactivity to be declared.
00371      *
00372      * @param inactivity The amount of time that acceleration must be less than
00373      *                   the inactivity threshold for inactivity to be
00374      *                   declared, in seconds. A value of 0 results in an
00375      *                   interrupt when the output data is less than the
00376      *                   threshold inactivity.
00377      */
00378     int setTimeInactivity(char timeInactivity);
00379     
00380     /**
00381      * Get the activity/inactivity control settings.
00382      *
00383      *      D7            D6             D5            D4
00384      * +-----------+--------------+--------------+--------------+
00385      * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable |
00386      * +-----------+--------------+--------------+--------------+
00387      *
00388      *        D3             D2               D1              D0
00389      * +-------------+----------------+----------------+----------------+
00390      * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable |
00391      * +-------------+----------------+----------------+----------------+
00392      *
00393      * See datasheet for details.
00394      *
00395      * @return The contents of the ACT_INACT_CTL register.
00396      */
00397      char getActivityInactivityControl(void);
00398     
00399     /**
00400      * Set the activity/inactivity control settings.
00401      *
00402      *      D7            D6             D5            D4
00403      * +-----------+--------------+--------------+--------------+
00404      * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable |
00405      * +-----------+--------------+--------------+--------------+
00406      *
00407      *        D3             D2               D1              D0
00408      * +-------------+----------------+----------------+----------------+
00409      * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable |
00410      * +-------------+----------------+----------------+----------------+
00411      *
00412      * See datasheet for details.
00413      *
00414      * @param settings The control byte to write to the ACT_INACT_CTL register.
00415      */
00416     int setActivityInactivityControl(char settings);
00417     
00418     /**
00419      * Get the threshold for free fall detection.
00420      *
00421      * @return The threshold value for free-fall detection, as an 8-bit number,
00422      *         with scale factor 62.5mg/LSB.
00423      */
00424      char getFreefallThreshold(void);
00425     
00426     /**
00427      * Set the threshold for free fall detection.
00428      *
00429      * @return The threshold value for free-fall detection, as an 8-bit number,
00430      *         with scale factor 62.5mg/LSB. A value of 0 may result in 
00431      *         undesirable behavior if the free-fall interrupt is enabled.
00432      *         Values between 300 mg and 600 mg (0x05 to 0x09) are recommended.
00433      */
00434     int setFreefallThreshold(char threshold);
00435     
00436     /**
00437      * Get the time required to generate a free fall interrupt.
00438      *
00439      * @return The minimum time that the value of all axes must be less than
00440      *         the freefall threshold to generate a free-fall interrupt, in
00441      *         milliseconds.
00442      */
00443      char getFreefallTime(void);
00444     
00445     /**
00446      * Set the time required to generate a free fall interrupt.
00447      *
00448      * @return The minimum time that the value of all axes must be less than
00449      *         the freefall threshold to generate a free-fall interrupt, in
00450      *         milliseconds. A value of 0 may result in undesirable behavior
00451      *         if the free-fall interrupt is enabled. Values between 100 ms 
00452      *         and 350 ms (0x14 to 0x46) are recommended.
00453      */
00454     int setFreefallTime(short int freefallTime_ms);
00455     
00456     /**
00457      * Get the axis tap settings.
00458      *
00459      *      D3           D2            D1             D0
00460      * +----------+--------------+--------------+--------------+
00461      * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable |
00462      * +----------+--------------+--------------+--------------+
00463      *
00464      * (D7-D4 are 0s).
00465      *
00466      * See datasheet for more details.
00467      *
00468      * @return The contents of the TAP_AXES register.
00469      */ 
00470      char getTapAxisControl(void);
00471     
00472     /**
00473      * Set the axis tap settings.
00474      *
00475      *      D3           D2            D1             D0
00476      * +----------+--------------+--------------+--------------+
00477      * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable |
00478      * +----------+--------------+--------------+--------------+
00479      *
00480      * (D7-D4 are 0s).
00481      *
00482      * See datasheet for more details.
00483      *
00484      * @param The control byte to write to the TAP_AXES register.
00485      */
00486     int setTapAxisControl(char settings);
00487     
00488     /**
00489      * Get the source of a tap.
00490      *
00491      * @return The contents of the ACT_TAP_STATUS register.
00492      */
00493      char getTapSource(void);
00494     
00495      /**
00496      * Get the interrupt enable settings.
00497      *
00498      * @return The contents of the INT_ENABLE register.
00499      */
00500 
00501      char getInterruptEnableControl(void);
00502     
00503     /**
00504      * Set the interrupt enable settings.
00505      *
00506      * @param settings The control byte to write to the INT_ENABLE register.
00507      */
00508     int setInterruptEnableControl(char settings);
00509     
00510     /**
00511      * Get the interrupt mapping settings.
00512      *
00513      * @return The contents of the INT_MAP register.
00514      */
00515      char getInterruptMappingControl(void);
00516     
00517     /**
00518      * Set the interrupt mapping settings.
00519      *
00520      * @param settings The control byte to write to the INT_MAP register.
00521      */
00522     int setInterruptMappingControl(char settings);
00523     
00524     /**
00525      * Get the interrupt source.
00526      *
00527      * @return The contents of the INT_SOURCE register.
00528      */
00529      char getInterruptSource(void);
00530     
00531    
00532 private:
00533 
00534     I2C i2c_;
00535     
00536 
00537     /**
00538      * Read one byte from a register on the device.
00539      *
00540      * @param: - the address to be read from
00541      *
00542      * @return: the value of the data read
00543      */
00544     char SingleByteRead(char address);
00545 
00546     /**
00547      * Write one byte to a register on the device.
00548      *
00549      * @param:
00550         - address of the register to write to.
00551         - the value of the data to store
00552      */
00553   
00554    
00555    int SingleByteWrite(char address, char data);
00556 
00557     /**
00558      * Read several consecutive bytes on the device and store them in a given location.
00559      *
00560      * @param startAddress: The address of the first register to read from.
00561      * @param ptr_output: a pointer to the location to store the data being read
00562      * @param size: The number of bytes to read.
00563      */
00564     void multiByteRead(char startAddress, char* ptr_output, int size);
00565 
00566     /**
00567      * Write several consecutive bytes  on the device.
00568      *
00569      * @param startAddress: The address of the first register to write to.
00570      * @param ptr_data: Pointer to a location which contains the data to write.
00571      * @param size: The number of bytes to write.
00572      */
00573     int multiByteWrite(char startAddress, char* ptr_data, int size);
00574 
00575 };
00576 
00577 #endif /* ADXL345_I2C_H */