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

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by Neil Thiessen

main.cpp

Committer:
neilt6
Date:
2014-07-30
Revision:
2:e4b38da7d1fc
Parent:
1:e0c49c5ad6d1
Child:
7:17ca3091939f

File content as of revision 2:e4b38da7d1fc:

#include "mbed.h"
#include "SDFileSystem.h"

DigitalIn button(p21, PullUp);
SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NC, 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);
    }
}