Reading Analog Ports and Saving in a micro SD CARD with the KL25z

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by Neil Thiessen

Committer:
neilt6
Date:
Tue Jul 29 20:13:41 2014 +0000
Revision:
0:8f2b6eed2a9d
Child:
1:e0c49c5ad6d1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew 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 0:8f2b6eed2a9d 5 SDFileSystem sd(p5, p6, p7, p20, p22, "sd", 6000000);
neilt6 0:8f2b6eed2a9d 6
neilt6 0:8f2b6eed2a9d 7 int main()
neilt6 0:8f2b6eed2a9d 8 {
neilt6 0:8f2b6eed2a9d 9 while(1) {
neilt6 0:8f2b6eed2a9d 10 //Print the start message
neilt6 0:8f2b6eed2a9d 11 printf("\nPress the button to perform tests: ");
neilt6 0:8f2b6eed2a9d 12
neilt6 0:8f2b6eed2a9d 13 //Wait for the button to be pressed
neilt6 0:8f2b6eed2a9d 14 while(button);
neilt6 0:8f2b6eed2a9d 15
neilt6 0:8f2b6eed2a9d 16 //Display the card type and capacity
neilt6 0:8f2b6eed2a9d 17 printf("\nCard type: ");
neilt6 0:8f2b6eed2a9d 18 if (sd.card_type() == SDFileSystem::CARD_NONE)
neilt6 0:8f2b6eed2a9d 19 printf("None\n");
neilt6 0:8f2b6eed2a9d 20 else if (sd.card_type() == SDFileSystem::CARD_MMC)
neilt6 0:8f2b6eed2a9d 21 printf("MMC\n");
neilt6 0:8f2b6eed2a9d 22 else if (sd.card_type() == SDFileSystem::CARD_SD)
neilt6 0:8f2b6eed2a9d 23 printf("SD\n");
neilt6 0:8f2b6eed2a9d 24 else if (sd.card_type() == SDFileSystem::CARD_SDHC)
neilt6 0:8f2b6eed2a9d 25 printf("SDHC\n");
neilt6 0:8f2b6eed2a9d 26 else
neilt6 0:8f2b6eed2a9d 27 printf("Unknown\n");
neilt6 0:8f2b6eed2a9d 28 printf("Sectors: %llu\n", sd.disk_sectors());
neilt6 0:8f2b6eed2a9d 29 printf("Capacity: %.1fMB\n", (sd.disk_sectors() * 512) / 1048576.0);
neilt6 0:8f2b6eed2a9d 30
neilt6 0:8f2b6eed2a9d 31 //Format the card
neilt6 0:8f2b6eed2a9d 32 /*printf("Formatting card...");
neilt6 0:8f2b6eed2a9d 33 if (sd.format() == 0)
neilt6 0:8f2b6eed2a9d 34 printf("success!\n");
neilt6 0:8f2b6eed2a9d 35 else
neilt6 0:8f2b6eed2a9d 36 printf("failed!\n");*/
neilt6 0:8f2b6eed2a9d 37
neilt6 0:8f2b6eed2a9d 38 //Perform a write test
neilt6 0:8f2b6eed2a9d 39 printf("Writing to card...");
neilt6 0:8f2b6eed2a9d 40 FILE *fp = fopen("/sd/sdtest.txt", "w");
neilt6 0:8f2b6eed2a9d 41 if (fp != NULL) {
neilt6 0:8f2b6eed2a9d 42 fprintf(fp, "We're writing to an SD card!");
neilt6 0:8f2b6eed2a9d 43 fclose(fp);
neilt6 0:8f2b6eed2a9d 44 printf("success!\n");
neilt6 0:8f2b6eed2a9d 45 } else {
neilt6 0:8f2b6eed2a9d 46 printf("failed!\n");
neilt6 0:8f2b6eed2a9d 47 }
neilt6 0:8f2b6eed2a9d 48
neilt6 0:8f2b6eed2a9d 49 //Perform a read test
neilt6 0:8f2b6eed2a9d 50 printf("Reading from card...");
neilt6 0:8f2b6eed2a9d 51 fp = fopen("/sd/sdtest.txt", "r");
neilt6 0:8f2b6eed2a9d 52 if (fp != NULL) {
neilt6 0:8f2b6eed2a9d 53 char c = fgetc(fp);
neilt6 0:8f2b6eed2a9d 54 if (c == 'W')
neilt6 0:8f2b6eed2a9d 55 printf("success!\n");
neilt6 0:8f2b6eed2a9d 56 else
neilt6 0:8f2b6eed2a9d 57 printf("incorrect char (%c)!\n", c);
neilt6 0:8f2b6eed2a9d 58 fclose(fp);
neilt6 0:8f2b6eed2a9d 59 } else {
neilt6 0:8f2b6eed2a9d 60 printf("failed!\n");
neilt6 0:8f2b6eed2a9d 61 }
neilt6 0:8f2b6eed2a9d 62
neilt6 0:8f2b6eed2a9d 63 //Delay for 0.2 seconds for simple debouncing
neilt6 0:8f2b6eed2a9d 64 wait(0.2);
neilt6 0:8f2b6eed2a9d 65 }
neilt6 0:8f2b6eed2a9d 66 }