Reading Analog Ports and Saving in a micro SD CARD with the KL25z
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@7:17ca3091939f, 2014-08-07 (annotated)
- Committer:
- neilt6
- Date:
- Thu Aug 07 16:46:14 2014 +0000
- Revision:
- 7:17ca3091939f
- Parent:
- 2:e4b38da7d1fc
- Child:
- 10:ae649a596123
Updated SDFileSystem library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 0:8f2b6eed2a9d | 1 | #include "mbed.h" |
neilt6 | 0:8f2b6eed2a9d | 2 | #include "SDFileSystem.h" |
neilt6 | 0:8f2b6eed2a9d | 3 | |
neilt6 | 0:8f2b6eed2a9d | 4 | DigitalIn button(p21, PullUp); |
neilt6 | 7:17ca3091939f | 5 | SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NO, 20000000); |
neilt6 | 0:8f2b6eed2a9d | 6 | |
neilt6 | 0:8f2b6eed2a9d | 7 | int main() |
neilt6 | 0:8f2b6eed2a9d | 8 | { |
neilt6 | 7:17ca3091939f | 9 | //Enable large frames for performance |
neilt6 | 7:17ca3091939f | 10 | sd.large_frames(true); |
neilt6 | 7:17ca3091939f | 11 | |
neilt6 | 0:8f2b6eed2a9d | 12 | while(1) { |
neilt6 | 0:8f2b6eed2a9d | 13 | //Print the start message |
neilt6 | 0:8f2b6eed2a9d | 14 | printf("\nPress the button to perform tests: "); |
neilt6 | 0:8f2b6eed2a9d | 15 | |
neilt6 | 0:8f2b6eed2a9d | 16 | //Wait for the button to be pressed |
neilt6 | 0:8f2b6eed2a9d | 17 | while(button); |
neilt6 | 0:8f2b6eed2a9d | 18 | |
neilt6 | 0:8f2b6eed2a9d | 19 | //Display the card type and capacity |
neilt6 | 0:8f2b6eed2a9d | 20 | printf("\nCard type: "); |
neilt6 | 0:8f2b6eed2a9d | 21 | if (sd.card_type() == SDFileSystem::CARD_NONE) |
neilt6 | 0:8f2b6eed2a9d | 22 | printf("None\n"); |
neilt6 | 0:8f2b6eed2a9d | 23 | else if (sd.card_type() == SDFileSystem::CARD_MMC) |
neilt6 | 0:8f2b6eed2a9d | 24 | printf("MMC\n"); |
neilt6 | 0:8f2b6eed2a9d | 25 | else if (sd.card_type() == SDFileSystem::CARD_SD) |
neilt6 | 0:8f2b6eed2a9d | 26 | printf("SD\n"); |
neilt6 | 0:8f2b6eed2a9d | 27 | else if (sd.card_type() == SDFileSystem::CARD_SDHC) |
neilt6 | 0:8f2b6eed2a9d | 28 | printf("SDHC\n"); |
neilt6 | 0:8f2b6eed2a9d | 29 | else |
neilt6 | 0:8f2b6eed2a9d | 30 | printf("Unknown\n"); |
neilt6 | 0:8f2b6eed2a9d | 31 | printf("Sectors: %llu\n", sd.disk_sectors()); |
neilt6 | 0:8f2b6eed2a9d | 32 | printf("Capacity: %.1fMB\n", (sd.disk_sectors() * 512) / 1048576.0); |
neilt6 | 0:8f2b6eed2a9d | 33 | |
neilt6 | 0:8f2b6eed2a9d | 34 | //Format the card |
neilt6 | 0:8f2b6eed2a9d | 35 | /*printf("Formatting card..."); |
neilt6 | 0:8f2b6eed2a9d | 36 | if (sd.format() == 0) |
neilt6 | 0:8f2b6eed2a9d | 37 | printf("success!\n"); |
neilt6 | 0:8f2b6eed2a9d | 38 | else |
neilt6 | 0:8f2b6eed2a9d | 39 | printf("failed!\n");*/ |
neilt6 | 0:8f2b6eed2a9d | 40 | |
neilt6 | 0:8f2b6eed2a9d | 41 | //Perform a write test |
neilt6 | 0:8f2b6eed2a9d | 42 | printf("Writing to card..."); |
neilt6 | 0:8f2b6eed2a9d | 43 | FILE *fp = fopen("/sd/sdtest.txt", "w"); |
neilt6 | 0:8f2b6eed2a9d | 44 | if (fp != NULL) { |
neilt6 | 0:8f2b6eed2a9d | 45 | fprintf(fp, "We're writing to an SD card!"); |
neilt6 | 0:8f2b6eed2a9d | 46 | fclose(fp); |
neilt6 | 0:8f2b6eed2a9d | 47 | printf("success!\n"); |
neilt6 | 0:8f2b6eed2a9d | 48 | } else { |
neilt6 | 0:8f2b6eed2a9d | 49 | printf("failed!\n"); |
neilt6 | 0:8f2b6eed2a9d | 50 | } |
neilt6 | 0:8f2b6eed2a9d | 51 | |
neilt6 | 0:8f2b6eed2a9d | 52 | //Perform a read test |
neilt6 | 0:8f2b6eed2a9d | 53 | printf("Reading from card..."); |
neilt6 | 0:8f2b6eed2a9d | 54 | fp = fopen("/sd/sdtest.txt", "r"); |
neilt6 | 0:8f2b6eed2a9d | 55 | if (fp != NULL) { |
neilt6 | 0:8f2b6eed2a9d | 56 | char c = fgetc(fp); |
neilt6 | 0:8f2b6eed2a9d | 57 | if (c == 'W') |
neilt6 | 0:8f2b6eed2a9d | 58 | printf("success!\n"); |
neilt6 | 0:8f2b6eed2a9d | 59 | else |
neilt6 | 0:8f2b6eed2a9d | 60 | printf("incorrect char (%c)!\n", c); |
neilt6 | 0:8f2b6eed2a9d | 61 | fclose(fp); |
neilt6 | 0:8f2b6eed2a9d | 62 | } else { |
neilt6 | 0:8f2b6eed2a9d | 63 | printf("failed!\n"); |
neilt6 | 0:8f2b6eed2a9d | 64 | } |
neilt6 | 0:8f2b6eed2a9d | 65 | |
neilt6 | 0:8f2b6eed2a9d | 66 | //Delay for 0.2 seconds for simple debouncing |
neilt6 | 0:8f2b6eed2a9d | 67 | wait(0.2); |
neilt6 | 0:8f2b6eed2a9d | 68 | } |
neilt6 | 0:8f2b6eed2a9d | 69 | } |