Write 10 lines of Accelerometer data from FRDM-KL25Z to SD Card and read it back.
Dependencies: MMA8451Q SDFileSystem mbed
Fork of SDCardTest by
main.cpp@7:dbf3c52ac1b5, 2014-12-31 (annotated)
- Committer:
- ogarai
- Date:
- Wed Dec 31 06:38:35 2014 +0000
- Revision:
- 7:dbf3c52ac1b5
- Parent:
- 6:f8e5916f19b8
- Child:
- 8:8d866ae976a3
Write 10 lines of sensor data to SD card and read the contents back.
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:dbf3c52ac1b5 | 15 | |
ogarai | 7:dbf3c52ac1b5 | 16 | for(int i=0;i<10;i++) |
kaushik_ray_1 | 6:f8e5916f19b8 | 17 | { |
kaushik_ray_1 | 6:f8e5916f19b8 | 18 | acc.getAccAllAxis(data); |
kaushik_ray_1 | 6:f8e5916f19b8 | 19 | fprintf(fp, "%d",data[0]); |
ogarai | 7:dbf3c52ac1b5 | 20 | printf("%d",data[0]); |
kaushik_ray_1 | 6:f8e5916f19b8 | 21 | fprintf(fp, ", %d",data[1]); |
ogarai | 7:dbf3c52ac1b5 | 22 | printf(", %d",data[1]); |
kaushik_ray_1 | 6:f8e5916f19b8 | 23 | fprintf(fp, ", %d\n",data[2]); |
ogarai | 7:dbf3c52ac1b5 | 24 | printf(", %d\n",data[2]); |
ogarai | 7:dbf3c52ac1b5 | 25 | } |
ogarai | 7:dbf3c52ac1b5 | 26 | fclose(fp); |
ogarai | 7:dbf3c52ac1b5 | 27 | printf("Done Writing! Now Reading... \n"); |
ogarai | 7:dbf3c52ac1b5 | 28 | |
ogarai | 7:dbf3c52ac1b5 | 29 | fp = fopen("/sd/sdtest.txt", "r"); |
ogarai | 7:dbf3c52ac1b5 | 30 | if(fp == NULL) { |
ogarai | 7:dbf3c52ac1b5 | 31 | error("Could not open file for read\n"); |
kaushik_ray_1 | 6:f8e5916f19b8 | 32 | } |
ogarai | 7:dbf3c52ac1b5 | 33 | int ch; |
ogarai | 7:dbf3c52ac1b5 | 34 | |
ogarai | 7:dbf3c52ac1b5 | 35 | while((ch = fgetc(fp)) != EOF) |
ogarai | 7:dbf3c52ac1b5 | 36 | { |
ogarai | 7:dbf3c52ac1b5 | 37 | printf("%c", ch); |
ogarai | 7:dbf3c52ac1b5 | 38 | } |
ogarai | 7:dbf3c52ac1b5 | 39 | printf("DONE Reading!\n"); |
ogarai | 7:dbf3c52ac1b5 | 40 | fclose(fp); |
kaushik_ray_1 | 6:f8e5916f19b8 | 41 | } |