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