Arduino ESP32 第三方库读取SD卡信息

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino ESP32 第三方库读取SD卡信息相关的知识,希望对你有一定的参考价值。

Arduino ESP32 第三方库读取SD卡信息(二)


相关篇《Arduino ESP32 第三方库读取SD卡信息(一)

如果网页无法加载,将域名改为镜像地址的例如:https://hub.fastgit.org/nhatuan84/esp32-micro-sdcard

  • 该库支持软SPI接口和硬SPI接口.如果使用硬SPI接口直接定义不用带参数:
    if (!SD.begin())  
    Serial.println("initialization failed!");
    return;
  
  • 如果是软SPI接口就这样初始化:
  if (!SD.begin(26, 14, 13, 27)) 
    Serial.println("initialization failed!");
    return;
  


接线说明

  • 软SPI接口接线方式:
		Soft SPI 
[ESP32 IO26 – CS MICRO SD]
[ESP32 IO14 – MOSI MICRO SD]
[ESP32 IO13 – MISO MICRO SD]
[ESP32 IO27 – SCK MICRO SD]
[ESP32 GND – GND MICRO SD]
[VIN – VCC MICRO SD]

  • 硬SPI接口接线方式:
    Hard SPI
 * MICROSD CS    -    ESP32 IO5
  MICROSD SCK   -     ESP32 IO18
  MICROSD MOSI  -    ESP32 IO23
  MICROSD MISO   -   ESP32 IO19
  MICROSD Vcc   -      ESP32 VIN
  MICROSD GND   -    ESP32 GND

实例代码二

/*
  SD card test 
   
 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.
 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header
 		Pin 4 used here for consistency with other Arduino examples

 
 created  28 Mar 2011  by Limor Fried 
 modified 9 Apr 2012   by Tom Igoe
 */
 // include the SD library:
#include <SPI.h>
#include <mySD.h>

// set up variables using the SD utility library functions:
Sd2Card card;//SD卡类型
SdVolume volume;//文件名
SdFile root;//目录

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
//const int chipSelect = 17;    

void setup()

 // Open serial communications and wait for port to open:
  Serial.begin(115200);
   while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
  


  Serial.print("\\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
// pinMode(SS, OUTPUT);


  // we'll use the initialization code from the utility libraries
  // /*初始化SD库Soft SPI引脚*/ 
  while (!card.init(SPI_HALF_SPEED, 26, 14, 13, 27)) 
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
   
  
  // print the type of card
  Serial.print("\\nCard type: ");
  switch(card.type()) //获取SD卡类型
    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
  if (!volume.init(card)) 
    Serial.println("Could not find FAT16/FAT32 partition.\\nMake sure you've formatted the card");
    return;
  


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  //打印SD卡容量信息
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;           // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

  
  Serial.println("\\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);//读取SD目录下的文件,创建日期,文件大小。



void loop(void) 
  

  • 编译信息
使用 1.0  版本的库 SPI 在文件夹: C:\\Users\\Administrator\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.6\\libraries\\SPI 
使用库 esp32-micro-sdcard-master 在文件夹: C:\\Program Files (x86)\\Arduino\\libraries\\esp32-micro-sdcard-master (legacy)
"C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\Arduino15\\\\packages\\\\esp32\\\\tools\\\\xtensa-esp32-elf-gcc\\\\1.22.0-97-gc752ad5-5.2.0/bin/xtensa-esp32-elf-size" -A "d:\\\\arduino\\\\MyHexDir/nanosdcard.ino.ino.elf"
项目使用了 215454 字节,占用了 (16%) 程序存储空间。最大为 1310720 字节。
全局变量使用了14280字节,(4%)的动态内存,余留313400字节局部变量。最大为327680字节。
  • 串口打印信息
  • root.ls(LS_R | LS_DATE | LS_SIZE);我觉得这个库最大的亮点就是这个函数,如果可以移植到其他SD库里面使用还是蛮不错的。该功能还是不错的。

以上是关于Arduino ESP32 第三方库读取SD卡信息的主要内容,如果未能解决你的问题,请参考以下文章

Arduino ESP32 第三方库读取SD卡信息

Arduino ESP32 读取Micro sd卡容量信息示例

Arduino ESP32 Web服务器从microSD卡读取

Arduino ESP32 读取SD卡接口选择参考

esp32 Arduino SD卡写入文件的问题

ESP32基于Arduino框架,SD卡+MAX98357模块+MP3播放器