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@3:9339e7b93415, 2016-03-14 (annotated)
- Committer:
- DanielBlomdahl
- Date:
- Mon Mar 14 20:41:22 2016 +0000
- Revision:
- 3:9339e7b93415
- Child:
- 5:a85aa204463b
This is the March 14th update
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" |
| DanielBlomdahl | 3:9339e7b93415 | 5 | |
| DanielBlomdahl | 3:9339e7b93415 | 6 | |
| DanielBlomdahl | 3:9339e7b93415 | 7 | //Create an SDFileSystem object |
| DanielBlomdahl | 3:9339e7b93415 | 8 | |
| DanielBlomdahl | 3:9339e7b93415 | 9 | SDFileSystem fs(NC, PTD3, PTD1, , "fs"); |
| DanielBlomdahl | 3:9339e7b93415 | 10 | |
| DanielBlomdahl | 3:9339e7b93415 | 11 | Serial pc(USBTX,USBRX); |
| DanielBlomdahl | 3:9339e7b93415 | 12 | SPI spi(NC,PTD3,PTD1); |
| DanielBlomdahl | 3:9339e7b93415 | 13 | |
| DanielBlomdahl | 3:9339e7b93415 | 14 | Timer t; |
| DanielBlomdahl | 3:9339e7b93415 | 15 | Ticker sampleTime; |
| DanielBlomdahl | 3:9339e7b93415 | 16 | FILE *fp; |
| DanielBlomdahl | 3:9339e7b93415 | 17 | bool mountFailure; |
| DanielBlomdahl | 3:9339e7b93415 | 18 | void writeData(); |
| DanielBlomdahl | 3:9339e7b93415 | 19 | int main() { |
| DanielBlomdahl | 3:9339e7b93415 | 20 | //Mount the filesystem |
| DanielBlomdahl | 3:9339e7b93415 | 21 | mountFailure = fs.mount(); |
| DanielBlomdahl | 3:9339e7b93415 | 22 | if (mountFailure != 0) { |
| DanielBlomdahl | 3:9339e7b93415 | 23 | pc.printf("Failed to mount the SD card.\n\r"); |
| DanielBlomdahl | 3:9339e7b93415 | 24 | return -1; // ends program with error status } |
| DanielBlomdahl | 3:9339e7b93415 | 25 | fp = fopen("/fs/dataLog.txt", "w"); // Open file and prepare to write |
| DanielBlomdahl | 3:9339e7b93415 | 26 | if (fp == NULL) { |
| DanielBlomdahl | 3:9339e7b93415 | 27 | pc.printf("Failed |
| DanielBlomdahl | 3:9339e7b93415 | 28 | fs.unmount(); |
| DanielBlomdahl | 3:9339e7b93415 | 29 | return -1; |
| DanielBlomdahl | 3:9339e7b93415 | 30 | } |
| DanielBlomdahl | 3:9339e7b93415 | 31 | // Write a header row |
| DanielBlomdahl | 3:9339e7b93415 | 32 | fprintf(fp, "Time (s) |
| DanielBlomdahl | 3:9339e7b93415 | 33 | // If open failed, exit well to open the file.\n\r"); |
| DanielBlomdahl | 3:9339e7b93415 | 34 | \t Voltage (V)\n\r"); |
| DanielBlomdahl | 3:9339e7b93415 | 35 | // Start the Timer and the Ticker |
| DanielBlomdahl | 3:9339e7b93415 | 36 | t.start(); |
| DanielBlomdahl | 3:9339e7b93415 | 37 | sampleTime.attach(&writeData, 0.1); // write data every 100 ms |
| DanielBlomdahl | 3:9339e7b93415 | 38 | while (t.read()<5) { |
| DanielBlomdahl | 3:9339e7b93415 | 39 | } |
| DanielBlomdahl | 3:9339e7b93415 | 40 | // Done so remove the sampleTime ticker before it attempts to write again sampleTime.detach(); |
| DanielBlomdahl | 3:9339e7b93415 | 41 | // Close the file and unmount the file system so the SD card is happy fclose(fp); |
| DanielBlomdahl | 3:9339e7b93415 | 42 | fs.unmount(); |
| DanielBlomdahl | 3:9339e7b93415 | 43 | pc.printf("It is now safe to remove the memory card.\r\n"); } |
| DanielBlomdahl | 3:9339e7b93415 | 44 | void writeData() { |
| DanielBlomdahl | 3:9339e7b93415 | 45 | fprintf(fp, "%.2f \t %.2f\n\r", t.read(), 3.3*pot.read()); |
| DanielBlomdahl | 3:9339e7b93415 | 46 | } |
