将SD卡连接到Arduino并获取卡信息


在本教程中,我们将Arduino Uno连接到SD卡并提取卡信息。

电路图

电路图如下所示:

如您所见,您需要进行以下连接:

SD卡座Arduino Uno
Vcc5V
GNDGND
MISO12
MOSI11
SCK13
CS10

只有对于Vcc,请确保您的SD卡座输入电压为5V。如果输入电压为3.3V,请将其连接到Arduino Uno上的3.3V引脚。

代码详解

我们将逐步讲解内置SD库附带的示例代码。您可以从文件→示例→SD→CardInfo中访问它。

或者,您也可以在GitHub上访问它:https://github.com/arduinolibraries/SD/blob/master/examples/CardInfo/CardInfo.ino

我们首先包含SPI和SD库:

#include <SPI.h>
#include <SD.h>

接下来,我们根据SD库中定义的对象定义一些变量。

Sd2Card card;
SdVolume volume;
SdFile root;

接下来,我们定义芯片选择引脚。示例代码将芯片选择设置为4号引脚。但是,我们将CS引脚连接到Arduino Uno的10号引脚。因此,我们将更改它。

const int chipSelect = 10;

在Setup中,我们首先初始化Serial,然后初始化SD卡(使用芯片选择引脚和SPI协议,SPI协议的其他引脚MISO、MOSI、SCK在SPI库中默认定义)。

void setup() {
   // Open serial communications and wait for port to open:
   Serial.begin(9600);
   while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB port only
   }

   Serial.print("
Initializing SD card...");    // we'll use the initialization code from the utility libraries    // since we're just testing if the card is working!    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?");       while (1);    } else {       Serial.println("Wiring is correct and a card is present."); }

接下来,我们使用.type()函数获取并打印卡的类型。

   // print the type of card
   Serial.println();
   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
   if (!volume.init(card)) {
      Serial.println("Could not find FAT16/FAT32 partition.
Make sure you've formatted the card");       while (1);    }    Serial.print("Clusters: ");    Serial.println(volume.clusterCount());    Serial.print("Blocks x Cluster: ");    Serial.println(volume.blocksPerCluster());    Serial.print("Total Blocks: ");    Serial.println(volume.blocksPerCluster() * volume.clusterCount());    Serial.println();

之后,我们打印卷是FAT 16还是FAT 32类型。此外,我们还计算总容量(以kB为单位),利用一个块始终为512字节或0.5 kB这一事实。

   // 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);
   volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
   volumesize *= volume.clusterCount(); // we'll have a lot of clusters
   volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
   Serial.print("Volume size (Kb): ");
   Serial.println(volumesize);
   Serial.print("Volume size (Mb): ");
   volumesize /= 1024;
   Serial.println(volumesize);
   Serial.print("Volume size (Gb): ");
   Serial.println((float)volumesize / 1024.0);

最后,我们使用openRoot()函数列出卡上找到的所有文件,并打印日期、大小以及文件名。

Serial.println("
Files 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);

更新于:2021年5月29日

浏览量:357

启动你的职业生涯

完成课程获得认证

开始学习
广告