Committer:
SED9008
Date:
Fri Jun 01 08:16:28 2012 +0000
Revision:
0:a973ef498a1d

        

Who changed what in which revision?

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