jjjj
Dependencies: SDFileSystem mbed
Fork of Nucleo_Ex05_SD by
main.cpp@3:1e51c10aa4f2, 2018-05-03 (annotated)
- Committer:
- brainliang
- Date:
- Thu May 03 10:53:02 2018 +0000
- Revision:
- 3:1e51c10aa4f2
- Parent:
- 2:1c1602268656
for robot arm training
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
beaglescout007 | 0:b0a3ecd53c7d | 1 | #include "mbed.h" |
brainliang | 3:1e51c10aa4f2 | 2 | #include "TextLCD.h" |
beaglescout007 | 0:b0a3ecd53c7d | 3 | #include "SDFileSystem.h" |
beaglescout007 | 0:b0a3ecd53c7d | 4 | |
brainliang | 3:1e51c10aa4f2 | 5 | //LCD显示屏 rs, e, d4-d7, model |
brainliang | 3:1e51c10aa4f2 | 6 | TextLCD lcd(PC_13, PC_14, PC_15, PA_0, PA_1, PA_4, TextLCD::LCD20x4); // rs, e, d4-d7, model V4版本 |
brainliang | 3:1e51c10aa4f2 | 7 | |
brainliang | 3:1e51c10aa4f2 | 8 | //SD卡 mosi, miso, sclk, cs, name 用SPI端口 |
brainliang | 3:1e51c10aa4f2 | 9 | SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd"); |
brainliang | 3:1e51c10aa4f2 | 10 | Serial pc(PA_9, PA_10); //与电脑通讯的串口 |
beaglescout007 | 0:b0a3ecd53c7d | 11 | |
brainliang | 3:1e51c10aa4f2 | 12 | int main() { |
brainliang | 2:1c1602268656 | 13 | DirHandle *dh = opendir("/sd/"); //读取SD卡中的文件列表 |
brainliang | 2:1c1602268656 | 14 | if(!dh){ |
brainliang | 2:1c1602268656 | 15 | pc.printf("opendir error\n"); |
brainliang | 2:1c1602268656 | 16 | return 1; |
brainliang | 2:1c1602268656 | 17 | } |
brainliang | 2:1c1602268656 | 18 | dirent *entry; |
brainliang | 2:1c1602268656 | 19 | for(;;){ |
brainliang | 2:1c1602268656 | 20 | entry = dh->readdir(); |
brainliang | 2:1c1602268656 | 21 | if(!entry) |
brainliang | 2:1c1602268656 | 22 | break; |
brainliang | 2:1c1602268656 | 23 | pc.printf("%s\r\n", entry->d_name); |
brainliang | 2:1c1602268656 | 24 | } |
brainliang | 2:1c1602268656 | 25 | dh->closedir(); |
brainliang | 2:1c1602268656 | 26 | |
brainliang | 2:1c1602268656 | 27 | // 写文件例子 |
brainliang | 2:1c1602268656 | 28 | FILE *fp2 = fopen("/sd/write.txt", "w"); |
brainliang | 2:1c1602268656 | 29 | if (fp2 == NULL) |
brainliang | 2:1c1602268656 | 30 | { |
brainliang | 2:1c1602268656 | 31 | pc.printf("open error2!!\r\n"); |
brainliang | 2:1c1602268656 | 32 | return 1; |
brainliang | 2:1c1602268656 | 33 | } |
brainliang | 2:1c1602268656 | 34 | pc.printf("file opened for write\r\n"); |
brainliang | 2:1c1602268656 | 35 | fprintf(fp2, "hello\r\n"); |
brainliang | 2:1c1602268656 | 36 | fprintf(fp2, "%d", 23333); |
brainliang | 2:1c1602268656 | 37 | fclose(fp2); //写完文件要记得关闭,不然可能没保存上 |
brainliang | 2:1c1602268656 | 38 | |
zhangyx | 1:d65338ce2e7c | 39 | // 读文件例子 |
zhangyx | 1:d65338ce2e7c | 40 | FILE *fp = fopen("/sd/test.txt", "r"); //打开文件,路径以“/sd/”开头 |
beaglescout007 | 0:b0a3ecd53c7d | 41 | |
zhangyx | 1:d65338ce2e7c | 42 | if (fp == NULL) //打开失败,原因可能是文件不存在,或卡没有连接好 |
zhangyx | 1:d65338ce2e7c | 43 | { |
brainliang | 2:1c1602268656 | 44 | pc.printf("open error!!\r\n"); |
zhangyx | 1:d65338ce2e7c | 45 | return 1; |
zhangyx | 1:d65338ce2e7c | 46 | } |
brainliang | 2:1c1602268656 | 47 | pc.printf("file opened for read\r\n"); |
zhangyx | 1:d65338ce2e7c | 48 | char buf[64]; |
zhangyx | 1:d65338ce2e7c | 49 | while (fgets(buf, sizeof(buf), fp) != NULL) //读入一行的C函数 |
zhangyx | 1:d65338ce2e7c | 50 | { |
brainliang | 2:1c1602268656 | 51 | pc.printf("%s", buf); |
zhangyx | 1:d65338ce2e7c | 52 | } |
zhangyx | 1:d65338ce2e7c | 53 | fclose(fp); //关闭文件,释放资源 |
zhangyx | 1:d65338ce2e7c | 54 | |
brainliang | 2:1c1602268656 | 55 | //pc.printf("card type is 0x%x\r\n" , sd.card_type()); |
beaglescout007 | 0:b0a3ecd53c7d | 56 | |
zhangyx | 1:d65338ce2e7c | 57 | return 0; |
brainliang | 3:1e51c10aa4f2 | 58 | |
brainliang | 3:1e51c10aa4f2 | 59 | |
brainliang | 3:1e51c10aa4f2 | 60 | lcd.printf("Hello World!"); |
brainliang | 3:1e51c10aa4f2 | 61 | wait_ms(1000); //等待1秒 |
brainliang | 3:1e51c10aa4f2 | 62 | lcd.cls(); //清屏 |
brainliang | 3:1e51c10aa4f2 | 63 | |
brainliang | 3:1e51c10aa4f2 | 64 | for(int i=0; i<10000; i+=4){ |
brainliang | 3:1e51c10aa4f2 | 65 | lcd.locate(0,0); //分别控制起始的列和行,从0开始 |
brainliang | 3:1e51c10aa4f2 | 66 | lcd.printf("%d", i); |
brainliang | 3:1e51c10aa4f2 | 67 | wait_ms(1000); //等待1秒 |
brainliang | 3:1e51c10aa4f2 | 68 | |
brainliang | 3:1e51c10aa4f2 | 69 | lcd.locate(0,1); |
brainliang | 3:1e51c10aa4f2 | 70 | lcd.printf("%d", i+1); |
brainliang | 3:1e51c10aa4f2 | 71 | wait_ms(1000); |
brainliang | 3:1e51c10aa4f2 | 72 | |
brainliang | 3:1e51c10aa4f2 | 73 | lcd.locate(0,2); |
brainliang | 3:1e51c10aa4f2 | 74 | lcd.printf("%d", i+2); |
brainliang | 3:1e51c10aa4f2 | 75 | wait_ms(1000); |
brainliang | 3:1e51c10aa4f2 | 76 | |
brainliang | 3:1e51c10aa4f2 | 77 | lcd.locate(0,3); |
brainliang | 3:1e51c10aa4f2 | 78 | lcd.printf("%d", i+3); |
brainliang | 3:1e51c10aa4f2 | 79 | wait_ms(1000); |
brainliang | 3:1e51c10aa4f2 | 80 | |
brainliang | 3:1e51c10aa4f2 | 81 | lcd.cls(); //清屏 |
brainliang | 3:1e51c10aa4f2 | 82 | } |
brainliang | 3:1e51c10aa4f2 | 83 | |
brainliang | 3:1e51c10aa4f2 | 84 | |
brainliang | 3:1e51c10aa4f2 | 85 | |
beaglescout007 | 0:b0a3ecd53c7d | 86 | } |