教程六 SD卡读取例程
来自Waveshare Wiki
各模块使用教程
- 序章 安装Arduino IDE
- 教程一 带编码器电机控制例程
- 教程二 无编码器电机控制例程
- 教程三 ST3215总线舵机控制例程
- 教程四 PWM舵机控制例程
- 教程五 IMU数据读取例程
- 教程六 SD卡读取例程
- 教程七 INA219电压电流监测例程
- 教程八 OLED屏幕控制例程
- General Driver for Robots 主页
SD卡读写
General Driver for Robots板载有SD卡模块,可对SD卡进行读写操作,以下提供SD卡读写程序。
例程
上传程序
首先将SD卡格式化为FAT32格式。
下载压缩包后打开SDCard.ino,打开文件夹进入路径C:\Users\用户名\AppData\Local\Arduino15\libraries,删除SD文件夹。
删除后切回到Arduino IDE,用USB线将多功能驱动板和计算机连接起来(此处插入的是多功能驱动板USB的Type-C接口),点击“工具”→“端口”,再点击新出现的COM(我这里新出现的COM为COM26)。
在Arduino IDE中,点击“工具”→“开发板”→“ESP32”→“ESP32 Dev Module”。开发板以及端口都选择好后上传程序。上传程序后将SD卡插入到驱动板上的SD卡槽,需要先上传程序再插入SD卡,不然会出现上传程序失败的结果。打开串口监视器,若出现SD卡读取失败的情况则断开USB线重新连接。
程序解析
#include "FS.h" #include "SD.h" #include "SPI.h" #define SCK 14 #define MISO 12 #define MOSI 13 #define CS 15 void readFile(fs::FS &fs, const char * path){ Serial.printf("Reading file: %s\n", path); File file = fs.open(path); if(!file){ Serial.println("Failed to open file for reading"); return; } Serial.print("Read from file: "); while(file.available()){ Serial.print((char)file.read()); } file.close(); } void writeFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Writing file: %s\n", path); File file = fs.open(path, FILE_WRITE); if(!file){ Serial.println("Failed to open file for writing"); return; } if(file.print(message)){ Serial.println("File written"); } else { Serial.println("Write failed"); } file.close(); } void appendFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Appending to file: %s\n", path); File file = fs.open(path, FILE_APPEND); if(!file){ Serial.println("Failed to open file for appending"); return; } if(file.print(message)){ Serial.println("Message appended"); } else { Serial.println("Append failed"); } file.close(); } void deleteFile(fs::FS &fs, const char * path){ Serial.printf("Deleting file: %s\n", path); if(fs.remove(path)){ Serial.println("File deleted"); } else { Serial.println("Delete failed"); } } void testFileIO(fs::FS &fs, const char * path){ File file = fs.open(path); static uint8_t buf[512]; size_t len = 0; uint32_t start = millis(); uint32_t end = start; if(file){ len = file.size(); size_t flen = len; start = millis(); while(len){ size_t toRead = len; if(toRead > 512){ toRead = 512; } file.read(buf, toRead); len -= toRead; } end = millis() - start; Serial.printf("%u bytes read for %u ms\n", flen, end); file.close(); } else { Serial.println("Failed to open file for reading"); } file = fs.open(path, FILE_WRITE); if(!file){ Serial.println("Failed to open file for writing"); return; } size_t i; start = millis(); for(i=0; i<2048; i++){ file.write(buf, 512); } end = millis() - start; Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end); file.close(); } void setup(){ Serial.begin(115200); SPIClass spi = SPIClass(HSPI); spi.begin(SCK, MISO, MOSI, CS); if (!SD.begin(CS,spi,80000000)) { Serial.println("Card Mount Failed"); return; } uint8_t cardType = SD.cardType(); if(cardType == CARD_NONE){ Serial.println("No SD card attached"); return; } Serial.print("SD Card Type: "); if(cardType == CARD_MMC){ Serial.println("MMC"); } else if(cardType == CARD_SD){ Serial.println("SDSC"); } else if(cardType == CARD_SDHC){ Serial.println("SDHC"); } else { Serial.println("UNKNOWN"); } uint64_t cardSize = SD.cardSize() / (1024 * 1024); Serial.printf("SD Card Size: %lluMB\n", cardSize); delay(2000); deleteFile(SD, "/hello.txt"); writeFile(SD, "/hello.txt", "Hello "); appendFile(SD, "/hello.txt", "World!\n"); readFile(SD, "/hello.txt"); testFileIO(SD, "/test.txt"); Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024)); Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024)); } void loop(){ }