Write 10 lines of Accelerometer data from FRDM-KL25Z to SD Card and read it back.

Dependencies:   MMA8451Q SDFileSystem mbed

Fork of SDCardTest by Orko Garai

Committer:
ogarai
Date:
Wed Dec 31 06:57:15 2014 +0000
Revision:
8:8d866ae976a3
Parent:
7:dbf3c52ac1b5
Switch from test mode to data-logging mode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:666a082cf50f 1 #include "mbed.h"
simon 0:666a082cf50f 2 #include "SDFileSystem.h"
kaushik_ray_1 6:f8e5916f19b8 3 #include "MMA8451Q.h"
simon 0:666a082cf50f 4
ogarai 4:823307a8e5dc 5 SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
kaushik_ray_1 6:f8e5916f19b8 6 MMA8451Q acc(PTE25,PTE24);
kaushik_ray_1 6:f8e5916f19b8 7
simon 0:666a082cf50f 8 int main() {
kaushik_ray_1 6:f8e5916f19b8 9 int16_t data[3];
kaushik_ray_1 6:f8e5916f19b8 10
simon 0:666a082cf50f 11 FILE *fp = fopen("/sd/sdtest.txt", "w");
simon 0:666a082cf50f 12 if(fp == NULL) {
simon 0:666a082cf50f 13 error("Could not open file for write\n");
simon 0:666a082cf50f 14 }
ogarai 8:8d866ae976a3 15 // Enable the below line and disable the next line for testing
ogarai 8:8d866ae976a3 16 // for(int i=0;i<10;i++)
ogarai 8:8d866ae976a3 17 while(1)
kaushik_ray_1 6:f8e5916f19b8 18 {
kaushik_ray_1 6:f8e5916f19b8 19 acc.getAccAllAxis(data);
kaushik_ray_1 6:f8e5916f19b8 20 fprintf(fp, "%d",data[0]);
ogarai 7:dbf3c52ac1b5 21 printf("%d",data[0]);
kaushik_ray_1 6:f8e5916f19b8 22 fprintf(fp, ", %d",data[1]);
ogarai 7:dbf3c52ac1b5 23 printf(", %d",data[1]);
kaushik_ray_1 6:f8e5916f19b8 24 fprintf(fp, ", %d\n",data[2]);
ogarai 7:dbf3c52ac1b5 25 printf(", %d\n",data[2]);
ogarai 7:dbf3c52ac1b5 26 }
ogarai 7:dbf3c52ac1b5 27 fclose(fp);
ogarai 7:dbf3c52ac1b5 28 printf("Done Writing! Now Reading... \n");
ogarai 7:dbf3c52ac1b5 29
ogarai 7:dbf3c52ac1b5 30 fp = fopen("/sd/sdtest.txt", "r");
ogarai 7:dbf3c52ac1b5 31 if(fp == NULL) {
ogarai 7:dbf3c52ac1b5 32 error("Could not open file for read\n");
kaushik_ray_1 6:f8e5916f19b8 33 }
ogarai 7:dbf3c52ac1b5 34 int ch;
ogarai 7:dbf3c52ac1b5 35
ogarai 7:dbf3c52ac1b5 36 while((ch = fgetc(fp)) != EOF)
ogarai 7:dbf3c52ac1b5 37 {
ogarai 7:dbf3c52ac1b5 38 printf("%c", ch);
ogarai 7:dbf3c52ac1b5 39 }
ogarai 7:dbf3c52ac1b5 40 printf("DONE Reading!\n");
ogarai 7:dbf3c52ac1b5 41 fclose(fp);
kaushik_ray_1 6:f8e5916f19b8 42 }