![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dsfa
Dependencies: dsf SDFileSystem TextLCD mbed stepper_moter
Fork of my_example_Nucleo_Ex05_SD_zhangyuxiang2 by
Diff: main.cpp
- Revision:
- 2:1c1602268656
- Parent:
- 1:d65338ce2e7c
- Child:
- 3:1e51c10aa4f2
--- a/main.cpp Wed Aug 23 03:11:41 2017 +0000 +++ b/main.cpp Thu May 03 10:36:22 2018 +0000 @@ -1,40 +1,57 @@ #include "mbed.h" #include "SDFileSystem.h" -// mosi, miso, sclk, name -SDFileSystem sd(PB_15, PB_14, PB_13, PA_9, "sd"); +// mosi, miso, sclk, name 用SPI端口 +//SDFileSystem sd(PB_15, PB_14, PB_13, PA_9, "sd"); +//Serial pc(PA_2, PA_3); +SDFileSystem sd(PA_7, PA_6, PA_5, PA_15, "sd"); //教学板上的管脚 +Serial pc(PA_9, PA_10); //教学板上与电脑通讯的串口 int main() { + DirHandle *dh = opendir("/sd/"); //读取SD卡中的文件列表 + if(!dh){ + pc.printf("opendir error\n"); + return 1; + } + dirent *entry; + for(;;){ + entry = dh->readdir(); + if(!entry) + break; + pc.printf("%s\r\n", entry->d_name); + } + dh->closedir(); + + // 写文件例子 + FILE *fp2 = fopen("/sd/write.txt", "w"); + if (fp2 == NULL) + { + pc.printf("open error2!!\r\n"); + return 1; + } + pc.printf("file opened for write\r\n"); + fprintf(fp2, "hello\r\n"); + fprintf(fp2, "%d", 23333); + fclose(fp2); //写完文件要记得关闭,不然可能没保存上 + // 读文件例子 FILE *fp = fopen("/sd/test.txt", "r"); //打开文件,路径以“/sd/”开头 if (fp == NULL) //打开失败,原因可能是文件不存在,或卡没有连接好 { - printf("open error!!\r\n"); + pc.printf("open error!!\r\n"); return 1; } - printf("file opened for read\r\n"); + pc.printf("file opened for read\r\n"); char buf[64]; while (fgets(buf, sizeof(buf), fp) != NULL) //读入一行的C函数 { - printf("read '%s'\r\n", buf); + pc.printf("%s", buf); } fclose(fp); //关闭文件,释放资源 - //printf("card type is 0x%x\r\n" , sd.card_type()); - - // 写文件例子 - FILE *fp2 = fopen("/sd/write.txt", "w"); - if (fp2 == NULL) - { - printf("open error2!!\r\n"); - return 1; - } - printf("file opened for write\r\n"); - fprintf(fp2, "hello\r\n"); - fprintf(fp2, "%d", 23333); - fclose(fp2); //写完文件要记得关闭,不然可能没保存上 + //pc.printf("card type is 0x%x\r\n" , sd.card_type()); return 0; }