![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ADXL345 test on L476
main.cpp@0:a0f7c6807a3a, 2017-11-30 (annotated)
- Committer:
- tifo
- Date:
- Thu Nov 30 20:01:44 2017 +0000
- Revision:
- 0:a0f7c6807a3a
- Child:
- 1:2098adebc6da
Manual acc reading, pseudo int are working.
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 | 0:a0f7c6807a3a | 9 | Serial mySerial(SERIAL_TX, SERIAL_RX, 9600); // tx, rx |
tifo | 0:a0f7c6807a3a | 10 | |
tifo | 0:a0f7c6807a3a | 11 | I2C i2cAcc(I2C_SDA, I2C_SCL); |
tifo | 0:a0f7c6807a3a | 12 | |
tifo | 0:a0f7c6807a3a | 13 | void ADXL_ISR(); |
tifo | 0:a0f7c6807a3a | 14 | |
tifo | 0:a0f7c6807a3a | 15 | int main() { |
tifo | 0:a0f7c6807a3a | 16 | |
tifo | 0:a0f7c6807a3a | 17 | i2cAcc.frequency(100000); |
tifo | 0:a0f7c6807a3a | 18 | |
tifo | 0:a0f7c6807a3a | 19 | adxl.powerOn(); // Power on the ADXL345 |
tifo | 0:a0f7c6807a3a | 20 | |
tifo | 0:a0f7c6807a3a | 21 | adxl.setRangeSetting(8); // Give the range settings |
tifo | 0:a0f7c6807a3a | 22 | // Accepted values are 2g, 4g, 8g or 16g |
tifo | 0:a0f7c6807a3a | 23 | // Higher Values = Wider Measurement Range |
tifo | 0:a0f7c6807a3a | 24 | // Lower Values = Greater Sensitivity |
tifo | 0:a0f7c6807a3a | 25 | |
tifo | 0:a0f7c6807a3a | 26 | |
tifo | 0:a0f7c6807a3a | 27 | 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 | 28 | adxl.setActivityThreshold(75); // 62.5mg per increment // Set activity // Inactivity thresholds (0-255) |
tifo | 0:a0f7c6807a3a | 29 | |
tifo | 0:a0f7c6807a3a | 30 | 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 | 31 | adxl.setInactivityThreshold(75); // 62.5mg per increment // Set inactivity // Inactivity thresholds (0-255) |
tifo | 0:a0f7c6807a3a | 32 | adxl.setTimeInactivity(10); // How many seconds of no activity is inactive? |
tifo | 0:a0f7c6807a3a | 33 | |
tifo | 0:a0f7c6807a3a | 34 | adxl.setTapDetectionOnXYZ(0, 0, 1); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF) |
tifo | 0:a0f7c6807a3a | 35 | |
tifo | 0:a0f7c6807a3a | 36 | // Set values for what is considered a TAP and what is a DOUBLE TAP (0-255) |
tifo | 0:a0f7c6807a3a | 37 | adxl.setTapThreshold(50); // 62.5 mg per increment |
tifo | 0:a0f7c6807a3a | 38 | adxl.setTapDuration(15); // 625 μs per increment |
tifo | 0:a0f7c6807a3a | 39 | adxl.setDoubleTapLatency(80); // 1.25 ms per increment |
tifo | 0:a0f7c6807a3a | 40 | adxl.setDoubleTapWindow(200); // 1.25 ms per increment |
tifo | 0:a0f7c6807a3a | 41 | |
tifo | 0:a0f7c6807a3a | 42 | // Set values for what is considered FREE FALL (0-255) |
tifo | 0:a0f7c6807a3a | 43 | adxl.setFreeFallThreshold(10); // (5 - 9) recommended - 62.5mg per increment |
tifo | 0:a0f7c6807a3a | 44 | adxl.setFreeFallDuration(10); // (20 - 70) recommended - 5ms per increment |
tifo | 0:a0f7c6807a3a | 45 | |
tifo | 0:a0f7c6807a3a | 46 | // Setting all interupts to take place on INT1 pin |
tifo | 0:a0f7c6807a3a | 47 | adxl.setImportantInterruptMapping(1, 1, 1, 1, 1); // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);" |
tifo | 0:a0f7c6807a3a | 48 | // 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 | 49 | // This library may have a problem using INT2 pin. Default to INT1 pin. |
tifo | 0:a0f7c6807a3a | 50 | |
tifo | 0:a0f7c6807a3a | 51 | // Turn on Interrupts for each mode (1 == ON, 0 == OFF) |
tifo | 0:a0f7c6807a3a | 52 | adxl.InactivityINT(1); |
tifo | 0:a0f7c6807a3a | 53 | adxl.ActivityINT(1); |
tifo | 0:a0f7c6807a3a | 54 | adxl.FreeFallINT(1); |
tifo | 0:a0f7c6807a3a | 55 | adxl.doubleTapINT(1); |
tifo | 0:a0f7c6807a3a | 56 | adxl.singleTapINT(1); |
tifo | 0:a0f7c6807a3a | 57 | |
tifo | 0:a0f7c6807a3a | 58 | |
tifo | 0:a0f7c6807a3a | 59 | mySerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n"); |
tifo | 0:a0f7c6807a3a | 60 | |
tifo | 0:a0f7c6807a3a | 61 | |
tifo | 0:a0f7c6807a3a | 62 | |
tifo | 0:a0f7c6807a3a | 63 | while(1) { |
tifo | 0:a0f7c6807a3a | 64 | |
tifo | 0:a0f7c6807a3a | 65 | // Accelerometer Readings |
tifo | 0:a0f7c6807a3a | 66 | //int x,y,z; |
tifo | 0:a0f7c6807a3a | 67 | //adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z |
tifo | 0:a0f7c6807a3a | 68 | |
tifo | 0:a0f7c6807a3a | 69 | // Output Results to Serial |
tifo | 0:a0f7c6807a3a | 70 | /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */ |
tifo | 0:a0f7c6807a3a | 71 | //mySerial.printf("X: %i\nY: %i\nZ: %i\n================\n", x, y, z); |
tifo | 0:a0f7c6807a3a | 72 | //wait(2); |
tifo | 0:a0f7c6807a3a | 73 | |
tifo | 0:a0f7c6807a3a | 74 | ADXL_ISR(); |
tifo | 0:a0f7c6807a3a | 75 | // You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR(); |
tifo | 0:a0f7c6807a3a | 76 | // and place it within the loop instead. |
tifo | 0:a0f7c6807a3a | 77 | // This may come in handy when it doesn't matter when the action occurs. |
tifo | 0:a0f7c6807a3a | 78 | |
tifo | 0:a0f7c6807a3a | 79 | } |
tifo | 0:a0f7c6807a3a | 80 | } |
tifo | 0:a0f7c6807a3a | 81 | |
tifo | 0:a0f7c6807a3a | 82 | |
tifo | 0:a0f7c6807a3a | 83 | /********************* ISR *********************/ |
tifo | 0:a0f7c6807a3a | 84 | /* Look for Interrupts and Triggered Action */ |
tifo | 0:a0f7c6807a3a | 85 | void ADXL_ISR() { |
tifo | 0:a0f7c6807a3a | 86 | |
tifo | 0:a0f7c6807a3a | 87 | // getInterruptSource clears all triggered actions after returning value |
tifo | 0:a0f7c6807a3a | 88 | // Do not call again until you need to recheck for triggered actions |
tifo | 0:a0f7c6807a3a | 89 | int interrupts = adxl.getInterruptSource(); |
tifo | 0:a0f7c6807a3a | 90 | |
tifo | 0:a0f7c6807a3a | 91 | // Free Fall Detection |
tifo | 0:a0f7c6807a3a | 92 | if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){ |
tifo | 0:a0f7c6807a3a | 93 | mySerial.printf("*** FREE FALL ***\n"); |
tifo | 0:a0f7c6807a3a | 94 | //add code here to do when free fall is sensed |
tifo | 0:a0f7c6807a3a | 95 | } |
tifo | 0:a0f7c6807a3a | 96 | |
tifo | 0:a0f7c6807a3a | 97 | // Inactivity |
tifo | 0:a0f7c6807a3a | 98 | if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){ |
tifo | 0:a0f7c6807a3a | 99 | mySerial.printf("*** INACTIVITY ***\n"); |
tifo | 0:a0f7c6807a3a | 100 | //add code here to do when inactivity is sensed |
tifo | 0:a0f7c6807a3a | 101 | } |
tifo | 0:a0f7c6807a3a | 102 | |
tifo | 0:a0f7c6807a3a | 103 | // Activity |
tifo | 0:a0f7c6807a3a | 104 | if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){ |
tifo | 0:a0f7c6807a3a | 105 | mySerial.printf("*** ACTIVITY ***\n"); |
tifo | 0:a0f7c6807a3a | 106 | //add code here to do when activity is sensed |
tifo | 0:a0f7c6807a3a | 107 | } |
tifo | 0:a0f7c6807a3a | 108 | |
tifo | 0:a0f7c6807a3a | 109 | // Double Tap Detection |
tifo | 0:a0f7c6807a3a | 110 | if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){ |
tifo | 0:a0f7c6807a3a | 111 | mySerial.printf("*** DOUBLE TAP ***\n"); |
tifo | 0:a0f7c6807a3a | 112 | //add code here to do when a 2X tap is sensed |
tifo | 0:a0f7c6807a3a | 113 | } |
tifo | 0:a0f7c6807a3a | 114 | |
tifo | 0:a0f7c6807a3a | 115 | // Tap Detection |
tifo | 0:a0f7c6807a3a | 116 | if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){ |
tifo | 0:a0f7c6807a3a | 117 | mySerial.printf("*** TAP ***\n"); |
tifo | 0:a0f7c6807a3a | 118 | //add code here to do when a tap is sensed |
tifo | 0:a0f7c6807a3a | 119 | } |
tifo | 0:a0f7c6807a3a | 120 | } |
tifo | 0:a0f7c6807a3a | 121 |