![](/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
main.cpp@2:1c1602268656, 2018-05-03 (annotated)
- Committer:
- brainliang
- Date:
- Thu May 03 10:36:22 2018 +0000
- Revision:
- 2:1c1602268656
- Parent:
- 1:d65338ce2e7c
- Child:
- 3:1e51c10aa4f2
jkj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
beaglescout007 | 0:b0a3ecd53c7d | 1 | #include "mbed.h" |
beaglescout007 | 0:b0a3ecd53c7d | 2 | #include "SDFileSystem.h" |
beaglescout007 | 0:b0a3ecd53c7d | 3 | |
brainliang | 2:1c1602268656 | 4 | // mosi, miso, sclk, name 用SPI端口 |
brainliang | 2:1c1602268656 | 5 | //SDFileSystem sd(PB_15, PB_14, PB_13, PA_9, "sd"); |
brainliang | 2:1c1602268656 | 6 | //Serial pc(PA_2, PA_3); |
brainliang | 2:1c1602268656 | 7 | SDFileSystem sd(PA_7, PA_6, PA_5, PA_15, "sd"); //教学板上的管脚 |
brainliang | 2:1c1602268656 | 8 | Serial pc(PA_9, PA_10); //教学板上与电脑通讯的串口 |
beaglescout007 | 0:b0a3ecd53c7d | 9 | |
beaglescout007 | 0:b0a3ecd53c7d | 10 | int main() |
beaglescout007 | 0:b0a3ecd53c7d | 11 | { |
brainliang | 2:1c1602268656 | 12 | DirHandle *dh = opendir("/sd/"); //读取SD卡中的文件列表 |
brainliang | 2:1c1602268656 | 13 | if(!dh){ |
brainliang | 2:1c1602268656 | 14 | pc.printf("opendir error\n"); |
brainliang | 2:1c1602268656 | 15 | return 1; |
brainliang | 2:1c1602268656 | 16 | } |
brainliang | 2:1c1602268656 | 17 | dirent *entry; |
brainliang | 2:1c1602268656 | 18 | for(;;){ |
brainliang | 2:1c1602268656 | 19 | entry = dh->readdir(); |
brainliang | 2:1c1602268656 | 20 | if(!entry) |
brainliang | 2:1c1602268656 | 21 | break; |
brainliang | 2:1c1602268656 | 22 | pc.printf("%s\r\n", entry->d_name); |
brainliang | 2:1c1602268656 | 23 | } |
brainliang | 2:1c1602268656 | 24 | dh->closedir(); |
brainliang | 2:1c1602268656 | 25 | |
brainliang | 2:1c1602268656 | 26 | // 写文件例子 |
brainliang | 2:1c1602268656 | 27 | FILE *fp2 = fopen("/sd/write.txt", "w"); |
brainliang | 2:1c1602268656 | 28 | if (fp2 == NULL) |
brainliang | 2:1c1602268656 | 29 | { |
brainliang | 2:1c1602268656 | 30 | pc.printf("open error2!!\r\n"); |
brainliang | 2:1c1602268656 | 31 | return 1; |
brainliang | 2:1c1602268656 | 32 | } |
brainliang | 2:1c1602268656 | 33 | pc.printf("file opened for write\r\n"); |
brainliang | 2:1c1602268656 | 34 | fprintf(fp2, "hello\r\n"); |
brainliang | 2:1c1602268656 | 35 | fprintf(fp2, "%d", 23333); |
brainliang | 2:1c1602268656 | 36 | fclose(fp2); //写完文件要记得关闭,不然可能没保存上 |
brainliang | 2:1c1602268656 | 37 | |
zhangyx | 1:d65338ce2e7c | 38 | // 读文件例子 |
zhangyx | 1:d65338ce2e7c | 39 | FILE *fp = fopen("/sd/test.txt", "r"); //打开文件,路径以“/sd/”开头 |
beaglescout007 | 0:b0a3ecd53c7d | 40 | |
zhangyx | 1:d65338ce2e7c | 41 | if (fp == NULL) //打开失败,原因可能是文件不存在,或卡没有连接好 |
zhangyx | 1:d65338ce2e7c | 42 | { |
brainliang | 2:1c1602268656 | 43 | pc.printf("open error!!\r\n"); |
zhangyx | 1:d65338ce2e7c | 44 | return 1; |
zhangyx | 1:d65338ce2e7c | 45 | } |
brainliang | 2:1c1602268656 | 46 | pc.printf("file opened for read\r\n"); |
zhangyx | 1:d65338ce2e7c | 47 | char buf[64]; |
zhangyx | 1:d65338ce2e7c | 48 | while (fgets(buf, sizeof(buf), fp) != NULL) //读入一行的C函数 |
zhangyx | 1:d65338ce2e7c | 49 | { |
brainliang | 2:1c1602268656 | 50 | pc.printf("%s", buf); |
zhangyx | 1:d65338ce2e7c | 51 | } |
zhangyx | 1:d65338ce2e7c | 52 | fclose(fp); //关闭文件,释放资源 |
zhangyx | 1:d65338ce2e7c | 53 | |
brainliang | 2:1c1602268656 | 54 | //pc.printf("card type is 0x%x\r\n" , sd.card_type()); |
beaglescout007 | 0:b0a3ecd53c7d | 55 | |
zhangyx | 1:d65338ce2e7c | 56 | return 0; |
beaglescout007 | 0:b0a3ecd53c7d | 57 | } |