Arduino SD卡 列出文件

/*
 SD卡测试
这个例子展示了如何使用实用程序库
sd库是基于获取您的SD卡的信息。
非常有用的测试卡,当你不确定它是否工作。
电路:
*附在SPI总线上的SD卡如下:
* MOSI引脚11上的Arduino Uno / Duemilanove /,
* MISO引脚12上的Arduino Uno / Duemilanove /,
* CLK引脚13上的Arduino Duemilanove /联合国/,
** -取决于您的SD卡屏蔽或模块。
4引脚用于与其他Arduino实例的一致性
创建28三月2011
Limor Fried
改性9 APR 2012
Tom Igoe
 */
// 库:包括SD
#include <SPI.h>
#include <SD.h>

// 使用sd实用程序库函数设置变量:
Sd2Card card;
SdVolume volume;
SdFile root;

// 改变这个匹配你的SD盾或模块;
// Arduino以太网盾: 4
// Adafruit SD盾和模块:10
// 和SD盾:8
const int chipSelect = 4;

void setup()
{
  // 打开串行通信并等待端口打开
  Serial.begin(115200);
  while (!Serial) {
    ; // 等待串口连接
  }


  Serial.print("
Initializing SD card...");//初始化SD卡

  //我们将使用实用程序库中的初始化代码。
  //因为我们只是在测试卡是否有效!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");//初始化失败。需要检查的东西:
    Serial.println("* is a card inserted?");//插卡了吗?
    Serial.println("* is your wiring correct?");//你的线路正确吗?
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    //你改变ChipSelect引脚来匹配你的盾或模块?
    return;
  } else {
    Serial.println("Wiring is correct and a card is present.");
    //接线正确,并有一张卡片。
  }

  // 打印的类型的卡
  Serial.print("
Card type: ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  //现在我们将尝试打开“音量”/ 'partition”应该是fat16和fat32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.
Make sure you've formatted the card");
    //无法找到FAT16/FAT32分区,NMAKE肯定你已经格式化卡
    return;
  }


  // print the type and size of the first FAT-type volume
  //打印第一个脂肪类型的类型和大小。
  uint32_t volumesize;
  Serial.print("
Volume type is FAT");//测定
  Serial.println(volume.fatType(), DEC);
  Serial.println();

  volumesize = volume.blocksPerCluster();    //卷大小(字节): 集群是块的集合。
  volumesize *= volume.clusterCount();       // 我们会有很多集群
  volumesize *= 512;                            //SD卡块总是512字节。
  Serial.print("Volume size (bytes): ");//卷大小(字节):
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");//卷的大小(字节):
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");//卷的大小(MB):
  volumesize /= 1024;
  Serial.println(volumesize);


  Serial.println("
Files found on the card (name, date and size in bytes): ");
  //卡上找到的文件(名称、日期和大小为字节):
  root.openRoot(volume);

  // 列出日期和大小的卡片中的所有文件
  root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {

}
原文地址:https://www.cnblogs.com/xiaohe520/p/7201439.html