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

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by Neil Thiessen

Revision:
26:50363a135213
Parent:
20:ec1c4f02d50e
--- a/main.cpp	Mon Aug 29 15:06:56 2016 +0000
+++ b/main.cpp	Thu Aug 10 18:08:02 2017 +0000
@@ -1,129 +1,49 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
-
-Timer timer;
-DigitalIn button(p21, PullUp);
-SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NEG_NO, 25000000);
-char buffer[4096];
-
-void writeTest()
-{
-    //Test write performance by creating a 1MB file
-    printf("Testing %iB write performance...", sizeof(buffer));
-    FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
-    if (file != NULL) {
-        timer.start();
-        for (int i = 0; i < (1048576 / sizeof(buffer)); i++) {
-            if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
-                timer.stop();
-                printf("write error!\n");
-                timer.reset();
-                return;
-            }
-        }
-        timer.stop();
-        if (file->close())
-            printf("failed to close file!\n");
-        else
-            printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
-        timer.reset();
-    } else {
-        printf("failed to create file!\n");
-    }
-}
-
-void readTest()
-{
-    //Test read performance by reading the 1MB file created by writeTest()
-    printf("Testing %iB read performance...", sizeof(buffer));
-    FileHandle* file = sd.open("Test File.bin", O_RDONLY);
-    if (file != NULL) {
-        timer.start();
-        int iterations = 0;
-        while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
-            iterations++;
-        timer.stop();
-        if (iterations != (1048576 / sizeof(buffer)))
-            printf("read error!\n");
-        else if (file->close())
-            printf("failed to close file!\n");
-        else if (sd.remove("Test File.bin"))
-            printf("failed to delete file!\n");
-        else
-            printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
-        timer.reset();
-    } else {
-        printf("failed to open file!\n");
+#include "math.h" 
+ 
+ // sd card breakout board: https://www.sparkfun.com/products/544
+ //
+ // frdm-kl25z sd card connections spi0
+ // ------------------------------------------------
+ // Header -- kl25z -- SPI          
+ // J2-8   -- PTD2  -- MOSI
+ // J2-6   -- PTD0  -- CS
+ // J9-12  -- GND   -- Vss (GND) 
+ // J9-4   -- P3V3  -- Vdd (+3.3v)
+ // J2-12  -- PTD1  -- SCK
+ // J9-14  -- GND   -- Vss (GND)
+ // J2-10  -- PTD3  -- MISO
+AnalogIn   ain(A0);
+AnalogIn   ain2(A1);
+AnalogIn   ain3(A2);  
+ char buffer[3];
+ int t1;
+ float a;
+ float b;
+ float c;
+SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); //  mosi, miso, sck, cs
+Serial pc(USBTX,USBRX);
+ 
+int main() {
+    
+    pc.baud(115200);
+    pc.printf("start sd card !\r\n");
+    mkdir("/sd/POF", 0777);
+      
+    FILE *fp = fopen("/sd/POF/test.txt", "w");
+    if(fp == NULL) {
+        error("Could not open file for write\r\n");
     }
-}
-
-int main()
-{
-    //Configure CRC, large frames, and write validation
-    sd.crc(true);
-    sd.large_frames(true);
-    sd.write_validation(true);
-
-    //Fill the buffer with random data for the write test
-    srand(time(NULL));
-    for (int i = 0; i < sizeof(buffer); i++)
-        buffer[i] = rand();
-
-    while(1) {
-        //Simple button debouncing
-        wait(0.5);
-
-        //Print the start message
-        printf("\nPress the button to perform tests: ");
-
-        //Wait for the button to be pressed
-        while(button);
-
-        //Make sure a card is present
-        if (!sd.card_present()) {
-            printf("\nNo card present!\n");
-            continue;
-        }
-
-        //Try to mount the SD card
-        printf("\nMounting SD card...");
-        if (sd.mount() != 0) {
-            printf("failed!\n");
-            continue;
-        }
-        printf("success!\n");
-
-        //Display the card type
-        printf("\tCard type: ");
-        SDFileSystem::CardType cardType = sd.card_type();
-        if (cardType == SDFileSystem::CARD_NONE)
-            printf("None\n");
-        else if (cardType == SDFileSystem::CARD_MMC)
-            printf("MMC\n");
-        else if (cardType == SDFileSystem::CARD_SD)
-            printf("SD\n");
-        else if (cardType == SDFileSystem::CARD_SDHC)
-            printf("SDHC\n");
-        else
-            printf("Unknown\n");
-
-        //Display the card capacity
-        printf("\tSectors: %u\n", sd.disk_sectors());
-        printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0);
-
-        /*//Format the card
-        printf("Formatting SD card...");
-        if (sd.format() != 0) {
-            printf("failed!\n");
-            continue;
-        }
-        printf("success!\n");*/
-
-        //Perform a read/write test
-        writeTest();
-        readTest();
-
-        //Unmount the SD card
-        sd.unmount();
+    else
+    while (1){
+    a=ain.read();
+    b=ain2.read();
+    c=ain3.read();
+    wait(0.0050);
+    fprintf(fp,"%0.3f, %0.3f,%0.3f\n",a, b,c );
     }
-}
+    
+    fclose(fp);
+    
+}
\ No newline at end of file