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

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by Neil Thiessen

Revision:
0:8f2b6eed2a9d
Child:
1:e0c49c5ad6d1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jul 29 20:13:41 2014 +0000
@@ -0,0 +1,66 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+
+DigitalIn button(p21, PullUp);
+SDFileSystem sd(p5, p6, p7, p20, p22, "sd", 6000000);
+
+int main()
+{
+    while(1) {
+        //Print the start message
+        printf("\nPress the button to perform tests: ");
+
+        //Wait for the button to be pressed
+        while(button);
+
+        //Display the card type and capacity
+        printf("\nCard type: ");
+        if (sd.card_type() == SDFileSystem::CARD_NONE)
+            printf("None\n");
+        else if (sd.card_type() == SDFileSystem::CARD_MMC)
+            printf("MMC\n");
+        else if (sd.card_type() == SDFileSystem::CARD_SD)
+            printf("SD\n");
+        else if (sd.card_type() == SDFileSystem::CARD_SDHC)
+            printf("SDHC\n");
+        else
+            printf("Unknown\n");
+        printf("Sectors: %llu\n", sd.disk_sectors());
+        printf("Capacity: %.1fMB\n", (sd.disk_sectors() * 512) / 1048576.0);
+
+        //Format the card
+        /*printf("Formatting card...");
+        if (sd.format() == 0)
+            printf("success!\n");
+        else
+            printf("failed!\n");*/
+
+        //Perform a write test
+        printf("Writing to card...");
+        FILE *fp = fopen("/sd/sdtest.txt", "w");
+        if (fp != NULL) {
+            fprintf(fp, "We're writing to an SD card!");
+            fclose(fp);
+            printf("success!\n");
+        } else {
+            printf("failed!\n");
+        }
+
+        //Perform a read test
+        printf("Reading from card...");
+        fp = fopen("/sd/sdtest.txt", "r");
+        if (fp != NULL) {
+            char c = fgetc(fp);
+            if (c == 'W')
+                printf("success!\n");
+            else
+                printf("incorrect char (%c)!\n", c);
+            fclose(fp);
+        } else {
+            printf("failed!\n");
+        }
+
+        //Delay for 0.2 seconds for simple debouncing
+        wait(0.2);
+    }
+}