![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ADXL345 test on L476
main.cpp
- Committer:
- tifo
- Date:
- 2017-12-16
- Revision:
- 1:2098adebc6da
- Parent:
- 0:a0f7c6807a3a
- Child:
- 2:2a57e2a50796
File content as of revision 1:2098adebc6da:
#include "mbed.h" #include "adxl345.h" ADXL345 adxl; int x, y, z; volatile char input_buffer[90]; // store everything received volatile char message_buffer[90]; // store message volatile char input_buffer_counter = 0; volatile char message_counter = 0; volatile char input_flag = false; volatile char latitude[13] = {' ','1',' ','3',' ',' ',' ',' ',' ',' ',' ',' ',' '}; volatile char longtitude[13] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};; volatile char coorFlag = 0; Serial mySerial(SERIAL_TX, SERIAL_RX, 9600); // tx, rx Serial gpsSerial(A0, A1, 9600); // tx, rx I2C i2cAcc(I2C_SDA, I2C_SCL); void ADXL_ISR(); void rxHandler(); int main() { i2cAcc.frequency(100000); adxl.powerOn(); // Power on the ADXL345 adxl.setRangeSetting(8); // Give the range settings // Accepted values are 2g, 4g, 8g or 16g // Higher Values = Wider Measurement Range // Lower Values = Greater Sensitivity adxl.setActivityXYZ(1, 0, 0); // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF) adxl.setActivityThreshold(75); // 62.5mg per increment // Set activity // Inactivity thresholds (0-255) adxl.setInactivityXYZ(1, 0, 0); // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF) adxl.setInactivityThreshold(75); // 62.5mg per increment // Set inactivity // Inactivity thresholds (0-255) adxl.setTimeInactivity(10); // How many seconds of no activity is inactive? adxl.setTapDetectionOnXYZ(0, 0, 1); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF) // Set values for what is considered a TAP and what is a DOUBLE TAP (0-255) adxl.setTapThreshold(50); // 62.5 mg per increment adxl.setTapDuration(15); // 625 μs per increment adxl.setDoubleTapLatency(80); // 1.25 ms per increment adxl.setDoubleTapWindow(200); // 1.25 ms per increment // Set values for what is considered FREE FALL (0-255) adxl.setFreeFallThreshold(10); // (5 - 9) recommended - 62.5mg per increment adxl.setFreeFallDuration(10); // (20 - 70) recommended - 5ms per increment // Setting all interupts to take place on INT1 pin adxl.setImportantInterruptMapping(1, 1, 1, 1, 1); // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);" // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts. // This library may have a problem using INT2 pin. Default to INT1 pin. // Turn on Interrupts for each mode (1 == ON, 0 == OFF) adxl.InactivityINT(1); adxl.ActivityINT(1); adxl.FreeFallINT(1); adxl.doubleTapINT(1); adxl.singleTapINT(1); mySerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n"); gpsSerial.attach(rxHandler); while(1) { // Accelerometer Readings //int x,y,z; //adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z // Output Results to Serial /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */ //mySerial.printf("X: %i\nY: %i\nZ: %i\n================\n", x, y, z); //wait(2); //ADXL_ISR(); // You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR(); // and place it within the loop instead. // This may come in handy when it doesn't matter when the action occurs. /* if(input_flag) // if message received { // check type of message and either send it directly, or convert to another type and send // // GGA input, send directly ============================================================= if(input_buffer[1] == 'G') { for(char i=0; i<message_counter; i++) // copy message stored to output_buffer; message_buffer[i] = input_buffer[i]; message_counter = input_buffer_counter; // set output pointer/counter value to input message counter value } } */ if(coorFlag) // show coordinates { mySerial.printf("latitude: %s\n", latitude); mySerial.printf("longtitude: %s\n", longtitude); coorFlag = 0; } } } /********************* ISR *********************/ /* Look for Interrupts and Triggered Action */ void ADXL_ISR() { // getInterruptSource clears all triggered actions after returning value // Do not call again until you need to recheck for triggered actions int interrupts = adxl.getInterruptSource(); // Free Fall Detection if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){ mySerial.printf("*** FREE FALL ***\n"); //add code here to do when free fall is sensed } // Inactivity if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){ mySerial.printf("*** INACTIVITY ***\n"); //add code here to do when inactivity is sensed } // Activity if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){ mySerial.printf("*** ACTIVITY ***\n"); //add code here to do when activity is sensed } // Double Tap Detection if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){ mySerial.printf("*** DOUBLE TAP ***\n"); //add code here to do when a 2X tap is sensed } // Tap Detection if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){ mySerial.printf("*** TAP ***\n"); //add code here to do when a tap is sensed } } // RX interrupt handler // stores everything into input_buffer void rxHandler() { char tmp; do { tmp = gpsSerial.getc(); // read serial data if(tmp == '$') // if message start character( every nmea message starts with $) { input_buffer_counter = 0; // reset inut buffer counter return; } if(tmp == '*') // if end of message( every nmea message ends with *+CRC) { if(input_buffer[3] == 'L') // if nmea string type is GPGLL { int t = 0; int lat = 0; int lon = 0; for(int i=0; i<13; i++) // clear latitude and longtitude { latitude[lat++] = ' '; longtitude[lon++] = ' '; } while(input_buffer[t] != ',') // find coma after GPGLL t++; // t points coma after GPGLL t++; // set to to first latitude character lat = 0; while(input_buffer[t] != ',') // copy latitude value { latitude[lat] = input_buffer[t]; lat++; t++; // t points coma after latitude } latitude[lat] = input_buffer[t]; // copy coma t++; latitude[lat] = input_buffer[t]; // copy N or S direction t++; t++; // set t to first character of longtitude lon = 0; while(input_buffer[t] != ',') // copy longtitude value { longtitude[lon] = input_buffer[t]; lon++; t++; // t points coma after longtitude } longtitude[lon] = input_buffer[t]; // copy coma t++; longtitude[lon] = input_buffer[t]; // copy W or E direction coorFlag = 1; gpsSerial.printf("message: %s", input_buffer); } return; } input_buffer[input_buffer_counter] = tmp; input_buffer_counter++; } while(gpsSerial.readable()); }