Opens/Creates txt file, writes to it, then prints to terminal from it
Dependencies: SDFileSystem mbed
Fork of FTF2014_lab4 by
main.cpp@3:deefd66fdc9e, 2015-03-04 (annotated)
- Committer:
- joshwilkins2013
- Date:
- Wed Mar 04 00:32:29 2015 +0000
- Revision:
- 3:deefd66fdc9e
- Parent:
- 0:a83db87be46c
Simple SD card test outputing floats in a loop
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 0:a83db87be46c | 1 | #include "mbed.h" |
Kojto | 0:a83db87be46c | 2 | #include "SDFileSystem.h" |
Kojto | 0:a83db87be46c | 3 | |
Kojto | 0:a83db87be46c | 4 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
joshwilkins2013 | 3:deefd66fdc9e | 5 | |
Kojto | 0:a83db87be46c | 6 | Serial pc(USBTX, USBRX); |
joshwilkins2013 | 3:deefd66fdc9e | 7 | |
joshwilkins2013 | 3:deefd66fdc9e | 8 | FILE *Data; // Creates name of file |
joshwilkins2013 | 3:deefd66fdc9e | 9 | char buffer[1024]; // Buffer size (# of characters) |
Kojto | 0:a83db87be46c | 10 | |
Kojto | 0:a83db87be46c | 11 | int main() { |
Kojto | 0:a83db87be46c | 12 | pc.printf("Initializing \n"); |
joshwilkins2013 | 3:deefd66fdc9e | 13 | |
joshwilkins2013 | 3:deefd66fdc9e | 14 | float x = 214; |
joshwilkins2013 | 3:deefd66fdc9e | 15 | double y = 200; |
joshwilkins2013 | 3:deefd66fdc9e | 16 | |
joshwilkins2013 | 3:deefd66fdc9e | 17 | Data = fopen("/sd/Data.txt", "r"); // Opens file if it exists, r means read |
joshwilkins2013 | 3:deefd66fdc9e | 18 | if (Data != NULL) remove("/sd/Data.txt"); // If it does close it then remove it |
joshwilkins2013 | 3:deefd66fdc9e | 19 | fclose(Data); |
joshwilkins2013 | 3:deefd66fdc9e | 20 | |
joshwilkins2013 | 3:deefd66fdc9e | 21 | Data = fopen("/sd/Data.txt", "w"); // w means write |
joshwilkins2013 | 3:deefd66fdc9e | 22 | for(int i=0; i<4; i++){ |
joshwilkins2013 | 3:deefd66fdc9e | 23 | if (Data == NULL) pc.printf("Unable to write the file \n\r"); // This chunk for writing to file on SD card |
joshwilkins2013 | 3:deefd66fdc9e | 24 | else fprintf(Data, "%f\t%f\n",x,y); // can read float or double not int (only checked these three types) |
joshwilkins2013 | 3:deefd66fdc9e | 25 | x++; |
joshwilkins2013 | 3:deefd66fdc9e | 26 | y--; |
Kojto | 0:a83db87be46c | 27 | } |
joshwilkins2013 | 3:deefd66fdc9e | 28 | fclose(Data); |
joshwilkins2013 | 3:deefd66fdc9e | 29 | |
joshwilkins2013 | 3:deefd66fdc9e | 30 | Data = fopen("/sd/Data.txt", "r"); |
joshwilkins2013 | 3:deefd66fdc9e | 31 | if (Data != NULL) { |
joshwilkins2013 | 3:deefd66fdc9e | 32 | int size = fread(buffer, sizeof(char), 1024, Data); // This chunk for reading the file on the SD card |
joshwilkins2013 | 3:deefd66fdc9e | 33 | printf("%s\n", buffer); // can read float or double not int (only checked these three types) |
Kojto | 0:a83db87be46c | 34 | } |
joshwilkins2013 | 3:deefd66fdc9e | 35 | fclose(Data); |
Kojto | 0:a83db87be46c | 36 | } |