Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAX31855 SDFileSystem mbed
SDSaveFunction.cpp@5:a85aa204463b, 2016-03-17 (annotated)
- Committer:
- JLarkin
- Date:
- Thu Mar 17 14:46:33 2016 +0000
- Revision:
- 5:a85aa204463b
- Parent:
- 3:9339e7b93415
- Child:
- 6:76e8649a643f
Modified SDSaveFunction.cpp to stand-alone functions, not a main program. Still needs corresponding .h file.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| DanielBlomdahl | 3:9339e7b93415 | 1 | /* Program demonstrating data logging to SD card Analog data is read in (from a potentiometer). The time since program start and the voltage are written to a tab-delimited data file. |
| DanielBlomdahl | 3:9339e7b93415 | 2 | */ |
| DanielBlomdahl | 3:9339e7b93415 | 3 | #include "mbed.h" |
| DanielBlomdahl | 3:9339e7b93415 | 4 | #include "SDFileSystem.h" |
| JLarkin | 5:a85aa204463b | 5 | #include "SDSaveFunction.h" |
| DanielBlomdahl | 3:9339e7b93415 | 6 | |
| DanielBlomdahl | 3:9339e7b93415 | 7 | //Create an SDFileSystem object |
| DanielBlomdahl | 3:9339e7b93415 | 8 | |
| JLarkin | 5:a85aa204463b | 9 | SDFileSystem fs(PTD2, PTD3, PTD1, , "fs"); |
| DanielBlomdahl | 3:9339e7b93415 | 10 | FILE *fp; |
| JLarkin | 5:a85aa204463b | 11 | |
| JLarkin | 5:a85aa204463b | 12 | extern Serial pc; |
| JLarkin | 5:a85aa204463b | 13 | |
| JLarkin | 5:a85aa204463b | 14 | int mountSDCard() { |
| JLarkin | 5:a85aa204463b | 15 | //Mount the filesystem |
| JLarkin | 5:a85aa204463b | 16 | int mountFailure = fs.mount(); |
| JLarkin | 5:a85aa204463b | 17 | if (mountFailure != 0) { |
| JLarkin | 5:a85aa204463b | 18 | pc.printf("Failed to mount the SD card.\n\r"); |
| JLarkin | 5:a85aa204463b | 19 | return -1; // ends function with error status |
| JLarkin | 5:a85aa204463b | 20 | } |
| JLarkin | 5:a85aa204463b | 21 | else |
| JLarkin | 5:a85aa204463b | 22 | return 0; |
| DanielBlomdahl | 3:9339e7b93415 | 23 | } |
| JLarkin | 5:a85aa204463b | 24 | |
| JLarkin | 5:a85aa204463b | 25 | int openDataFile() { |
| JLarkin | 5:a85aa204463b | 26 | fp = fopen("/fs/dataLog.txt", "w"); // Open file and prepare to write |
| JLarkin | 5:a85aa204463b | 27 | if (fp == NULL) { |
| JLarkin | 5:a85aa204463b | 28 | pc.printf("Failed to open file.\n\r"); |
| JLarkin | 5:a85aa204463b | 29 | return -1; |
| JLarkin | 5:a85aa204463b | 30 | } |
| JLarkin | 5:a85aa204463b | 31 | // Write a header row |
| JLarkin | 5:a85aa204463b | 32 | fprintf(fp, "Time (s) \t Voltage (V)\n\r"); // Needs to be modified for your particular experiment |
| JLarkin | 5:a85aa204463b | 33 | return 0; |
| DanielBlomdahl | 3:9339e7b93415 | 34 | } |
| JLarkin | 5:a85aa204463b | 35 | |
| JLarkin | 5:a85aa204463b | 36 | int closeDateFile() { // Close the file and unmount the file system so the SD card is happy |
| JLarkin | 5:a85aa204463b | 37 | fclose(fp); |
| JLarkin | 5:a85aa204463b | 38 | fs.unmount(); |
| JLarkin | 5:a85aa204463b | 39 | pc.printf("It is now safe to remove the memory card.\r\n"); |
| JLarkin | 5:a85aa204463b | 40 | return 0; |
| DanielBlomdahl | 3:9339e7b93415 | 41 | } |
| JLarkin | 5:a85aa204463b | 42 | |
| JLarkin | 5:a85aa204463b | 43 | void writeData(float firstThing, float secondThing) { // Need to modify for your particular experiment |
| JLarkin | 5:a85aa204463b | 44 | fprintf(fp, "%.2f \t %.2f\n\r", firstThing, secondThing); |
| JLarkin | 5:a85aa204463b | 45 | } |
