Example of using the SDFileSystem library on a K64F to write files from LSM6DS33 (accel and gyro)
Dependencies: FXOS8700CQ LSM6DS3 SDFileSystem mbed
Fork of 2545_SD_Card by
main.cpp@1:23691b50336d, 2018-07-11 (annotated)
- Committer:
- danilloaguiar
- Date:
- Wed Jul 11 00:59:13 2018 +0000
- Revision:
- 1:23691b50336d
- Parent:
- 0:5448330e1a33
ACCEL AND GYRO LSM6DS33 SAVE IN SD CARD K64F
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:5448330e1a33 | 1 | #include "mbed.h" |
eencae | 0:5448330e1a33 | 2 | #include "SDFileSystem.h" |
danilloaguiar | 1:23691b50336d | 3 | #include "FXOS8700CQ.h" |
danilloaguiar | 1:23691b50336d | 4 | #include "LSM6DS3.h" |
eencae | 0:5448330e1a33 | 5 | |
eencae | 0:5448330e1a33 | 6 | // Connections to SD card holder on K64F (SPI interface) |
eencae | 0:5448330e1a33 | 7 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
danilloaguiar | 1:23691b50336d | 8 | Timer t; |
danilloaguiar | 1:23691b50336d | 9 | FXOS8700CQ fxos(PTE25,PTE24); |
danilloaguiar | 1:23691b50336d | 10 | LSM6DS3 LSM6DS3(PTE25,PTE24); |
danilloaguiar | 1:23691b50336d | 11 | Serial pc(USBTX, USBRX); |
danilloaguiar | 1:23691b50336d | 12 | Data fxos_acc; |
danilloaguiar | 1:23691b50336d | 13 | unsigned long int start_time = 0, end_time = 0; |
danilloaguiar | 1:23691b50336d | 14 | uint16_t adc_value; |
danilloaguiar | 1:23691b50336d | 15 | AnalogIn pot0(A0), pot1(A1), pot2(A2), pot3(A3); |
eencae | 0:5448330e1a33 | 16 | |
danilloaguiar | 1:23691b50336d | 17 | |
eencae | 0:5448330e1a33 | 18 | |
eencae | 0:5448330e1a33 | 19 | int main() |
eencae | 0:5448330e1a33 | 20 | { |
danilloaguiar | 1:23691b50336d | 21 | LSM6DS3.begin(); |
danilloaguiar | 1:23691b50336d | 22 | fxos.init(); |
danilloaguiar | 1:23691b50336d | 23 | double t0, t1; |
danilloaguiar | 1:23691b50336d | 24 | pc.baud(115200); // full-speed! |
danilloaguiar | 1:23691b50336d | 25 | pc.printf("#### Example #####\n"); |
eencae | 0:5448330e1a33 | 26 | FILE *fp; // this is our file pointer |
danilloaguiar | 1:23691b50336d | 27 | t.start(); |
danilloaguiar | 1:23691b50336d | 28 | t0 = t.read_us(); |
danilloaguiar | 1:23691b50336d | 29 | fp = fopen("/sd/Teste1.txt", "wb"); |
danilloaguiar | 1:23691b50336d | 30 | //read Accel & Gyro |
danilloaguiar | 1:23691b50336d | 31 | LSM6DS3.readAccel(); |
danilloaguiar | 1:23691b50336d | 32 | LSM6DS3.readGyro(); |
danilloaguiar | 1:23691b50336d | 33 | start_time = t.read_us(); |
danilloaguiar | 1:23691b50336d | 34 | fxos_acc = fxos.get_values(); |
danilloaguiar | 1:23691b50336d | 35 | end_time = t.read_us(); |
danilloaguiar | 1:23691b50336d | 36 | pc.printf("\nPass time: %d\n", end_time - start_time); |
danilloaguiar | 1:23691b50336d | 37 | //serial send Accel (board) |
danilloaguiar | 1:23691b50336d | 38 | pc.printf("BoardAccelerX[%f]\n",fxos_acc.ax); |
danilloaguiar | 1:23691b50336d | 39 | pc.printf("BoardAccelerY[%f]\n",fxos_acc.ay); |
danilloaguiar | 1:23691b50336d | 40 | pc.printf("BoardAccelerZ[%f]\n",fxos_acc.az); |
danilloaguiar | 1:23691b50336d | 41 | //serial send Gyro |
danilloaguiar | 1:23691b50336d | 42 | pc.printf("GyroX[%f]\n",LSM6DS3.gx); |
danilloaguiar | 1:23691b50336d | 43 | pc.printf("GyroY[%f]\n",LSM6DS3.gy); |
danilloaguiar | 1:23691b50336d | 44 | pc.printf("GyroZ[%f]\n",LSM6DS3.gz); |
danilloaguiar | 1:23691b50336d | 45 | //serial send Accel (lsm6ds33) |
danilloaguiar | 1:23691b50336d | 46 | pc.printf("AccelerX[%f]\n",LSM6DS3.ax); |
danilloaguiar | 1:23691b50336d | 47 | pc.printf("AccelerY[%f]\n",LSM6DS3.ay); |
danilloaguiar | 1:23691b50336d | 48 | pc.printf("AccelerZ[%f]\n",LSM6DS3.az); |
eencae | 0:5448330e1a33 | 49 | |
eencae | 0:5448330e1a33 | 50 | |
eencae | 0:5448330e1a33 | 51 | if (fp == NULL) { // if it can't open the file then print error message |
danilloaguiar | 1:23691b50336d | 52 | pc.printf("Error! Unable to open file!\n"); |
danilloaguiar | 1:23691b50336d | 53 | } else { // opened file so can write |
danilloaguiar | 1:23691b50336d | 54 | fprintf(fp, "%f %f %f \n %f %f %f \n %f %f %f \n", fxos_acc.ax,fxos_acc.ay,fxos_acc.az,LSM6DS3.gx,LSM6DS3.gy,LSM6DS3.gz,LSM6DS3.ax,LSM6DS3.ay,LSM6DS3.az); |
danilloaguiar | 1:23691b50336d | 55 | fclose(fp); // ensure you close the file after writing |
danilloaguiar | 1:23691b50336d | 56 | t1 = t.read_us(); |
danilloaguiar | 1:23691b50336d | 57 | pc.printf("The time taken was %lf u-seconds\n", t1-t0); |
danilloaguiar | 1:23691b50336d | 58 | t.stop(); |
eencae | 0:5448330e1a33 | 59 | } |
eencae | 0:5448330e1a33 | 60 | } |