Arduino Uno SD卡模块 (一)获取SDcard的信息

欢迎关注「凌顺实验室」微信公众号

 

 

 

 

 

SD卡的应用非常广泛也都非常方便

 

当我没有使用联网的情况下,SD卡就能储存我的数据,以便以后调用,所以开始写Arduino SD卡应用系列分享

 

首先我们先看看这个模块

实验效果

本次实验实现获取SD卡的基本信息,例如:SD卡的容量,类型,文件列表

 

BOM表

Arduiino Uno *1

SD卡模块  *1

SD卡   *1

调线若干

 

接线

Arduino Uno   <----->   Sd Card 模块

       GND          <----->           GND

        5V            <----->             +5

       CS            <----->            Pin 4

      MOSI         <----->           Pin 11

        SCK        <----->            Pin 13

      MISO         <----->           Pin 12

 

 

程序

这个实验还是需要下载库

下载地址: https://github.com/greiman/SdFat

具体如何操作,再次说一下,下载解压到Arduino IDE的安装路径里的库文件夹libraries    

 

库里提供了很多SD卡模块示例程序,可以多多参考

 

 

 

/*
  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:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
 		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:
// 加载SD库
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
// 使用SD实用程序库函数设置变量:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// 改变以匹配你的SD模块和开发板类型
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;

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("\nInitializing SD card...");

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

  // print the type of card
  //显示卡的类型
  Serial.print("\nCard 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.\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);
}


void loop(void) {

}

 

 

 

 

 

程序实现思路解读

 

1,声明加载SPI库和SD库 
#include <SPI.h>
#include <SD.h>

 

2,使用SD实用程序库函数设置变量

Sd2Card card;      (card 为 Sd2Card库函数对象,名称自定义了可以改成自己想要的设置,调用就可以使” card.函数名  “,大概我这样理解的,如有更好的理解请大神们指出)

SdVolume volume;  (同上)

SdFile root;             (同上)

 

3,设置SD模块和板子类型

const int chipSelect = 4;

 

之后就可以引用函数进行操作了

例如:

card.init(SPI_HALF_SPEED, chipSelect)    //可以判断SD卡是否正常工作

card.type()   //返回SD卡的类型

volume.init(card)        //可判断能否获取SD卡数据信息

volume.fatType()        

volume.blocksPerCluster()       

root.ls(LS_R | LS_DATE | LS_SIZE);  //返回文件列表

等等......

 

 

 

 

  • 6
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值