jjjj

Dependencies:   SDFileSystem mbed

Fork of Nucleo_Ex05_SD by YX ZHANG

Committer:
zhangyx
Date:
Wed Aug 23 03:11:41 2017 +0000
Revision:
1:d65338ce2e7c
Parent:
0:b0a3ecd53c7d
Child:
2:1c1602268656
stm32 spi based sd card demo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
beaglescout007 0:b0a3ecd53c7d 1 #include "mbed.h"
beaglescout007 0:b0a3ecd53c7d 2 #include "SDFileSystem.h"
beaglescout007 0:b0a3ecd53c7d 3
zhangyx 1:d65338ce2e7c 4 // mosi, miso, sclk, name
zhangyx 1:d65338ce2e7c 5 SDFileSystem sd(PB_15, PB_14, PB_13, PA_9, "sd");
beaglescout007 0:b0a3ecd53c7d 6
beaglescout007 0:b0a3ecd53c7d 7 int main()
beaglescout007 0:b0a3ecd53c7d 8 {
zhangyx 1:d65338ce2e7c 9 // 读文件例子
zhangyx 1:d65338ce2e7c 10 FILE *fp = fopen("/sd/test.txt", "r"); //打开文件,路径以“/sd/”开头
beaglescout007 0:b0a3ecd53c7d 11
zhangyx 1:d65338ce2e7c 12 if (fp == NULL) //打开失败,原因可能是文件不存在,或卡没有连接好
zhangyx 1:d65338ce2e7c 13 {
zhangyx 1:d65338ce2e7c 14 printf("open error!!\r\n");
zhangyx 1:d65338ce2e7c 15 return 1;
zhangyx 1:d65338ce2e7c 16 }
zhangyx 1:d65338ce2e7c 17 printf("file opened for read\r\n");
zhangyx 1:d65338ce2e7c 18 char buf[64];
zhangyx 1:d65338ce2e7c 19 while (fgets(buf, sizeof(buf), fp) != NULL) //读入一行的C函数
zhangyx 1:d65338ce2e7c 20 {
zhangyx 1:d65338ce2e7c 21 printf("read '%s'\r\n", buf);
zhangyx 1:d65338ce2e7c 22 }
zhangyx 1:d65338ce2e7c 23 fclose(fp); //关闭文件,释放资源
zhangyx 1:d65338ce2e7c 24
zhangyx 1:d65338ce2e7c 25 //printf("card type is 0x%x\r\n" , sd.card_type());
beaglescout007 0:b0a3ecd53c7d 26
zhangyx 1:d65338ce2e7c 27 // 写文件例子
zhangyx 1:d65338ce2e7c 28 FILE *fp2 = fopen("/sd/write.txt", "w");
zhangyx 1:d65338ce2e7c 29 if (fp2 == NULL)
zhangyx 1:d65338ce2e7c 30 {
zhangyx 1:d65338ce2e7c 31 printf("open error2!!\r\n");
zhangyx 1:d65338ce2e7c 32 return 1;
zhangyx 1:d65338ce2e7c 33 }
zhangyx 1:d65338ce2e7c 34 printf("file opened for write\r\n");
zhangyx 1:d65338ce2e7c 35 fprintf(fp2, "hello\r\n");
zhangyx 1:d65338ce2e7c 36 fprintf(fp2, "%d", 23333);
zhangyx 1:d65338ce2e7c 37 fclose(fp2); //写完文件要记得关闭,不然可能没保存上
beaglescout007 0:b0a3ecd53c7d 38
zhangyx 1:d65338ce2e7c 39 return 0;
beaglescout007 0:b0a3ecd53c7d 40 }