![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ADXL345 test on L476
main.cpp@1:2098adebc6da, 2017-12-16 (annotated)
- Committer:
- tifo
- Date:
- Sat Dec 16 11:22:15 2017 +0000
- Revision:
- 1:2098adebc6da
- Parent:
- 0:a0f7c6807a3a
- Child:
- 2:2a57e2a50796
gps added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tifo | 0:a0f7c6807a3a | 1 | #include "mbed.h" |
tifo | 0:a0f7c6807a3a | 2 | #include "adxl345.h" |
tifo | 0:a0f7c6807a3a | 3 | |
tifo | 0:a0f7c6807a3a | 4 | |
tifo | 0:a0f7c6807a3a | 5 | ADXL345 adxl; |
tifo | 0:a0f7c6807a3a | 6 | |
tifo | 0:a0f7c6807a3a | 7 | int x, y, z; |
tifo | 0:a0f7c6807a3a | 8 | |
tifo | 1:2098adebc6da | 9 | volatile char input_buffer[90]; // store everything received |
tifo | 1:2098adebc6da | 10 | volatile char message_buffer[90]; // store message |
tifo | 1:2098adebc6da | 11 | volatile char input_buffer_counter = 0; |
tifo | 1:2098adebc6da | 12 | volatile char message_counter = 0; |
tifo | 1:2098adebc6da | 13 | volatile char input_flag = false; |
tifo | 1:2098adebc6da | 14 | |
tifo | 1:2098adebc6da | 15 | volatile char latitude[13] = {' ','1',' ','3',' ',' ',' ',' ',' ',' ',' ',' ',' '}; |
tifo | 1:2098adebc6da | 16 | volatile char longtitude[13] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};; |
tifo | 1:2098adebc6da | 17 | volatile char coorFlag = 0; |
tifo | 1:2098adebc6da | 18 | |
tifo | 1:2098adebc6da | 19 | |
tifo | 1:2098adebc6da | 20 | Serial mySerial(SERIAL_TX, SERIAL_RX, 9600); // tx, rx |
tifo | 1:2098adebc6da | 21 | Serial gpsSerial(A0, A1, 9600); // tx, rx |
tifo | 0:a0f7c6807a3a | 22 | |
tifo | 0:a0f7c6807a3a | 23 | I2C i2cAcc(I2C_SDA, I2C_SCL); |
tifo | 0:a0f7c6807a3a | 24 | |
tifo | 0:a0f7c6807a3a | 25 | void ADXL_ISR(); |
tifo | 1:2098adebc6da | 26 | void rxHandler(); |
tifo | 0:a0f7c6807a3a | 27 | |
tifo | 0:a0f7c6807a3a | 28 | int main() { |
tifo | 0:a0f7c6807a3a | 29 | |
tifo | 0:a0f7c6807a3a | 30 | i2cAcc.frequency(100000); |
tifo | 0:a0f7c6807a3a | 31 | |
tifo | 0:a0f7c6807a3a | 32 | adxl.powerOn(); // Power on the ADXL345 |
tifo | 0:a0f7c6807a3a | 33 | |
tifo | 0:a0f7c6807a3a | 34 | adxl.setRangeSetting(8); // Give the range settings |
tifo | 0:a0f7c6807a3a | 35 | // Accepted values are 2g, 4g, 8g or 16g |
tifo | 0:a0f7c6807a3a | 36 | // Higher Values = Wider Measurement Range |
tifo | 0:a0f7c6807a3a | 37 | // Lower Values = Greater Sensitivity |
tifo | 0:a0f7c6807a3a | 38 | |
tifo | 0:a0f7c6807a3a | 39 | |
tifo | 0:a0f7c6807a3a | 40 | adxl.setActivityXYZ(1, 0, 0); // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF) |
tifo | 0:a0f7c6807a3a | 41 | adxl.setActivityThreshold(75); // 62.5mg per increment // Set activity // Inactivity thresholds (0-255) |
tifo | 0:a0f7c6807a3a | 42 | |
tifo | 0:a0f7c6807a3a | 43 | adxl.setInactivityXYZ(1, 0, 0); // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF) |
tifo | 0:a0f7c6807a3a | 44 | adxl.setInactivityThreshold(75); // 62.5mg per increment // Set inactivity // Inactivity thresholds (0-255) |
tifo | 0:a0f7c6807a3a | 45 | adxl.setTimeInactivity(10); // How many seconds of no activity is inactive? |
tifo | 0:a0f7c6807a3a | 46 | |
tifo | 0:a0f7c6807a3a | 47 | adxl.setTapDetectionOnXYZ(0, 0, 1); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF) |
tifo | 0:a0f7c6807a3a | 48 | |
tifo | 0:a0f7c6807a3a | 49 | // Set values for what is considered a TAP and what is a DOUBLE TAP (0-255) |
tifo | 0:a0f7c6807a3a | 50 | adxl.setTapThreshold(50); // 62.5 mg per increment |
tifo | 0:a0f7c6807a3a | 51 | adxl.setTapDuration(15); // 625 μs per increment |
tifo | 0:a0f7c6807a3a | 52 | adxl.setDoubleTapLatency(80); // 1.25 ms per increment |
tifo | 0:a0f7c6807a3a | 53 | adxl.setDoubleTapWindow(200); // 1.25 ms per increment |
tifo | 0:a0f7c6807a3a | 54 | |
tifo | 0:a0f7c6807a3a | 55 | // Set values for what is considered FREE FALL (0-255) |
tifo | 0:a0f7c6807a3a | 56 | adxl.setFreeFallThreshold(10); // (5 - 9) recommended - 62.5mg per increment |
tifo | 0:a0f7c6807a3a | 57 | adxl.setFreeFallDuration(10); // (20 - 70) recommended - 5ms per increment |
tifo | 0:a0f7c6807a3a | 58 | |
tifo | 0:a0f7c6807a3a | 59 | // Setting all interupts to take place on INT1 pin |
tifo | 0:a0f7c6807a3a | 60 | adxl.setImportantInterruptMapping(1, 1, 1, 1, 1); // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);" |
tifo | 0:a0f7c6807a3a | 61 | // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts. |
tifo | 0:a0f7c6807a3a | 62 | // This library may have a problem using INT2 pin. Default to INT1 pin. |
tifo | 0:a0f7c6807a3a | 63 | |
tifo | 0:a0f7c6807a3a | 64 | // Turn on Interrupts for each mode (1 == ON, 0 == OFF) |
tifo | 0:a0f7c6807a3a | 65 | adxl.InactivityINT(1); |
tifo | 0:a0f7c6807a3a | 66 | adxl.ActivityINT(1); |
tifo | 0:a0f7c6807a3a | 67 | adxl.FreeFallINT(1); |
tifo | 0:a0f7c6807a3a | 68 | adxl.doubleTapINT(1); |
tifo | 0:a0f7c6807a3a | 69 | adxl.singleTapINT(1); |
tifo | 0:a0f7c6807a3a | 70 | |
tifo | 0:a0f7c6807a3a | 71 | |
tifo | 0:a0f7c6807a3a | 72 | mySerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n"); |
tifo | 0:a0f7c6807a3a | 73 | |
tifo | 1:2098adebc6da | 74 | gpsSerial.attach(rxHandler); |
tifo | 1:2098adebc6da | 75 | |
tifo | 0:a0f7c6807a3a | 76 | |
tifo | 0:a0f7c6807a3a | 77 | |
tifo | 0:a0f7c6807a3a | 78 | while(1) { |
tifo | 0:a0f7c6807a3a | 79 | |
tifo | 0:a0f7c6807a3a | 80 | // Accelerometer Readings |
tifo | 0:a0f7c6807a3a | 81 | //int x,y,z; |
tifo | 0:a0f7c6807a3a | 82 | //adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z |
tifo | 0:a0f7c6807a3a | 83 | |
tifo | 0:a0f7c6807a3a | 84 | // Output Results to Serial |
tifo | 0:a0f7c6807a3a | 85 | /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */ |
tifo | 0:a0f7c6807a3a | 86 | //mySerial.printf("X: %i\nY: %i\nZ: %i\n================\n", x, y, z); |
tifo | 0:a0f7c6807a3a | 87 | //wait(2); |
tifo | 0:a0f7c6807a3a | 88 | |
tifo | 1:2098adebc6da | 89 | //ADXL_ISR(); |
tifo | 0:a0f7c6807a3a | 90 | // You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR(); |
tifo | 0:a0f7c6807a3a | 91 | // and place it within the loop instead. |
tifo | 0:a0f7c6807a3a | 92 | // This may come in handy when it doesn't matter when the action occurs. |
tifo | 1:2098adebc6da | 93 | /* |
tifo | 1:2098adebc6da | 94 | if(input_flag) // if message received |
tifo | 1:2098adebc6da | 95 | { |
tifo | 1:2098adebc6da | 96 | // check type of message and either send it directly, or convert to another type and send |
tifo | 1:2098adebc6da | 97 | // |
tifo | 1:2098adebc6da | 98 | // GGA input, send directly ============================================================= |
tifo | 1:2098adebc6da | 99 | if(input_buffer[1] == 'G') |
tifo | 1:2098adebc6da | 100 | { |
tifo | 1:2098adebc6da | 101 | for(char i=0; i<message_counter; i++) // copy message stored to output_buffer; |
tifo | 1:2098adebc6da | 102 | message_buffer[i] = input_buffer[i]; |
tifo | 1:2098adebc6da | 103 | |
tifo | 1:2098adebc6da | 104 | message_counter = input_buffer_counter; // set output pointer/counter value to input message counter value |
tifo | 1:2098adebc6da | 105 | } |
tifo | 1:2098adebc6da | 106 | } |
tifo | 1:2098adebc6da | 107 | */ |
tifo | 1:2098adebc6da | 108 | |
tifo | 1:2098adebc6da | 109 | if(coorFlag) // show coordinates |
tifo | 1:2098adebc6da | 110 | { |
tifo | 1:2098adebc6da | 111 | mySerial.printf("latitude: %s\n", latitude); |
tifo | 1:2098adebc6da | 112 | mySerial.printf("longtitude: %s\n", longtitude); |
tifo | 1:2098adebc6da | 113 | |
tifo | 1:2098adebc6da | 114 | coorFlag = 0; |
tifo | 1:2098adebc6da | 115 | } |
tifo | 1:2098adebc6da | 116 | |
tifo | 0:a0f7c6807a3a | 117 | |
tifo | 0:a0f7c6807a3a | 118 | } |
tifo | 0:a0f7c6807a3a | 119 | } |
tifo | 0:a0f7c6807a3a | 120 | |
tifo | 0:a0f7c6807a3a | 121 | |
tifo | 0:a0f7c6807a3a | 122 | /********************* ISR *********************/ |
tifo | 0:a0f7c6807a3a | 123 | /* Look for Interrupts and Triggered Action */ |
tifo | 0:a0f7c6807a3a | 124 | void ADXL_ISR() { |
tifo | 0:a0f7c6807a3a | 125 | |
tifo | 0:a0f7c6807a3a | 126 | // getInterruptSource clears all triggered actions after returning value |
tifo | 0:a0f7c6807a3a | 127 | // Do not call again until you need to recheck for triggered actions |
tifo | 0:a0f7c6807a3a | 128 | int interrupts = adxl.getInterruptSource(); |
tifo | 0:a0f7c6807a3a | 129 | |
tifo | 0:a0f7c6807a3a | 130 | // Free Fall Detection |
tifo | 0:a0f7c6807a3a | 131 | if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){ |
tifo | 0:a0f7c6807a3a | 132 | mySerial.printf("*** FREE FALL ***\n"); |
tifo | 0:a0f7c6807a3a | 133 | //add code here to do when free fall is sensed |
tifo | 0:a0f7c6807a3a | 134 | } |
tifo | 0:a0f7c6807a3a | 135 | |
tifo | 0:a0f7c6807a3a | 136 | // Inactivity |
tifo | 0:a0f7c6807a3a | 137 | if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){ |
tifo | 0:a0f7c6807a3a | 138 | mySerial.printf("*** INACTIVITY ***\n"); |
tifo | 0:a0f7c6807a3a | 139 | //add code here to do when inactivity is sensed |
tifo | 0:a0f7c6807a3a | 140 | } |
tifo | 0:a0f7c6807a3a | 141 | |
tifo | 0:a0f7c6807a3a | 142 | // Activity |
tifo | 0:a0f7c6807a3a | 143 | if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){ |
tifo | 0:a0f7c6807a3a | 144 | mySerial.printf("*** ACTIVITY ***\n"); |
tifo | 0:a0f7c6807a3a | 145 | //add code here to do when activity is sensed |
tifo | 0:a0f7c6807a3a | 146 | } |
tifo | 0:a0f7c6807a3a | 147 | |
tifo | 0:a0f7c6807a3a | 148 | // Double Tap Detection |
tifo | 0:a0f7c6807a3a | 149 | if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){ |
tifo | 0:a0f7c6807a3a | 150 | mySerial.printf("*** DOUBLE TAP ***\n"); |
tifo | 0:a0f7c6807a3a | 151 | //add code here to do when a 2X tap is sensed |
tifo | 0:a0f7c6807a3a | 152 | } |
tifo | 0:a0f7c6807a3a | 153 | |
tifo | 0:a0f7c6807a3a | 154 | // Tap Detection |
tifo | 0:a0f7c6807a3a | 155 | if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){ |
tifo | 0:a0f7c6807a3a | 156 | mySerial.printf("*** TAP ***\n"); |
tifo | 0:a0f7c6807a3a | 157 | //add code here to do when a tap is sensed |
tifo | 0:a0f7c6807a3a | 158 | } |
tifo | 0:a0f7c6807a3a | 159 | } |
tifo | 0:a0f7c6807a3a | 160 | |
tifo | 1:2098adebc6da | 161 | |
tifo | 1:2098adebc6da | 162 | // RX interrupt handler |
tifo | 1:2098adebc6da | 163 | // stores everything into input_buffer |
tifo | 1:2098adebc6da | 164 | void rxHandler() |
tifo | 1:2098adebc6da | 165 | { |
tifo | 1:2098adebc6da | 166 | char tmp; |
tifo | 1:2098adebc6da | 167 | |
tifo | 1:2098adebc6da | 168 | do |
tifo | 1:2098adebc6da | 169 | { |
tifo | 1:2098adebc6da | 170 | tmp = gpsSerial.getc(); // read serial data |
tifo | 1:2098adebc6da | 171 | |
tifo | 1:2098adebc6da | 172 | if(tmp == '$') // if message start character( every nmea message starts with $) |
tifo | 1:2098adebc6da | 173 | { |
tifo | 1:2098adebc6da | 174 | input_buffer_counter = 0; // reset inut buffer counter |
tifo | 1:2098adebc6da | 175 | return; |
tifo | 1:2098adebc6da | 176 | } |
tifo | 1:2098adebc6da | 177 | |
tifo | 1:2098adebc6da | 178 | if(tmp == '*') // if end of message( every nmea message ends with *+CRC) |
tifo | 1:2098adebc6da | 179 | { |
tifo | 1:2098adebc6da | 180 | if(input_buffer[3] == 'L') // if nmea string type is GPGLL |
tifo | 1:2098adebc6da | 181 | { |
tifo | 1:2098adebc6da | 182 | int t = 0; |
tifo | 1:2098adebc6da | 183 | int lat = 0; |
tifo | 1:2098adebc6da | 184 | int lon = 0; |
tifo | 1:2098adebc6da | 185 | |
tifo | 1:2098adebc6da | 186 | for(int i=0; i<13; i++) // clear latitude and longtitude |
tifo | 1:2098adebc6da | 187 | { |
tifo | 1:2098adebc6da | 188 | latitude[lat++] = ' '; |
tifo | 1:2098adebc6da | 189 | longtitude[lon++] = ' '; |
tifo | 1:2098adebc6da | 190 | } |
tifo | 1:2098adebc6da | 191 | |
tifo | 1:2098adebc6da | 192 | while(input_buffer[t] != ',') // find coma after GPGLL |
tifo | 1:2098adebc6da | 193 | t++; // t points coma after GPGLL |
tifo | 1:2098adebc6da | 194 | |
tifo | 1:2098adebc6da | 195 | t++; // set to to first latitude character |
tifo | 1:2098adebc6da | 196 | |
tifo | 1:2098adebc6da | 197 | |
tifo | 1:2098adebc6da | 198 | lat = 0; |
tifo | 1:2098adebc6da | 199 | |
tifo | 1:2098adebc6da | 200 | while(input_buffer[t] != ',') // copy latitude value |
tifo | 1:2098adebc6da | 201 | { |
tifo | 1:2098adebc6da | 202 | latitude[lat] = input_buffer[t]; |
tifo | 1:2098adebc6da | 203 | lat++; |
tifo | 1:2098adebc6da | 204 | t++; // t points coma after latitude |
tifo | 1:2098adebc6da | 205 | } |
tifo | 1:2098adebc6da | 206 | latitude[lat] = input_buffer[t]; // copy coma |
tifo | 1:2098adebc6da | 207 | t++; |
tifo | 1:2098adebc6da | 208 | latitude[lat] = input_buffer[t]; // copy N or S direction |
tifo | 1:2098adebc6da | 209 | t++; |
tifo | 1:2098adebc6da | 210 | |
tifo | 1:2098adebc6da | 211 | t++; // set t to first character of longtitude |
tifo | 1:2098adebc6da | 212 | |
tifo | 1:2098adebc6da | 213 | |
tifo | 1:2098adebc6da | 214 | lon = 0; |
tifo | 1:2098adebc6da | 215 | |
tifo | 1:2098adebc6da | 216 | while(input_buffer[t] != ',') // copy longtitude value |
tifo | 1:2098adebc6da | 217 | { |
tifo | 1:2098adebc6da | 218 | longtitude[lon] = input_buffer[t]; |
tifo | 1:2098adebc6da | 219 | lon++; |
tifo | 1:2098adebc6da | 220 | t++; // t points coma after longtitude |
tifo | 1:2098adebc6da | 221 | } |
tifo | 1:2098adebc6da | 222 | longtitude[lon] = input_buffer[t]; // copy coma |
tifo | 1:2098adebc6da | 223 | t++; |
tifo | 1:2098adebc6da | 224 | longtitude[lon] = input_buffer[t]; // copy W or E direction |
tifo | 1:2098adebc6da | 225 | |
tifo | 1:2098adebc6da | 226 | coorFlag = 1; |
tifo | 1:2098adebc6da | 227 | |
tifo | 1:2098adebc6da | 228 | gpsSerial.printf("message: %s", input_buffer); |
tifo | 1:2098adebc6da | 229 | |
tifo | 1:2098adebc6da | 230 | } |
tifo | 1:2098adebc6da | 231 | return; |
tifo | 1:2098adebc6da | 232 | } |
tifo | 1:2098adebc6da | 233 | |
tifo | 1:2098adebc6da | 234 | input_buffer[input_buffer_counter] = tmp; |
tifo | 1:2098adebc6da | 235 | input_buffer_counter++; |
tifo | 1:2098adebc6da | 236 | } |
tifo | 1:2098adebc6da | 237 | while(gpsSerial.readable()); |
tifo | 1:2098adebc6da | 238 | } |
tifo | 1:2098adebc6da | 239 | |
tifo | 1:2098adebc6da | 240 |