关于SD/TF卡的扩展,市面上主要有两种类型的模块。
1.模块介绍
第一种是SD shield模块,比较大,可以直接堆叠插在UNO板子上,模块上一面可以插SD卡,背面可以插TF卡(小卡);
第二种是体积较小的SD卡扩展模块,一般是6个引脚,需要根据例程把对应引脚接到Arduino上,这种模块一般只能插SD/TF卡中的一种。
2.模块使用
因为SD shield模块是堆叠式设计,所以只要对准引脚插下去即可使用,而第二种扩展模块则需要多一个接线步骤,不过接线也很简单:
模块 Arduino
GND---------GND
VCC----------5V
MISO---------PIN12
MOSI---------PIN11
SCK-----------PIN13
CS-------------PIN4//片选
3.CardInfo程序
连线接好之后,第一件事当然是跑例程,Arduino的IDE中就带有了SD卡的例程,我们这里先跑一下CardInfo,用来检测SD卡能否被正确检测到。(IDE使用的是官方IDE1.8.5版本,如果找不到可以下载最新版本官方IDE并更新库文件)
程序贴在下面:
// include the SD library:
#include <SPI.h>
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
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
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;//可以看到片选定义到了pin4
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!
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();
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) {
}
可以看到,程序在Setup中完成SD卡的读取,并在串口输出SD卡的类型、卡的容量、卡内文件列表。
4.结果
话不多说,把shield模块插到板子上,再插进去我特意买的128Mb小卡,上电,烧程序,打开串口。嗯,shield模块看起来工作比较顺利,买的不知道几手的TF卡也还能用,测试结果如下:
Initializing SD card...Wiring is correct and a card is present.
Card type: SD2
Volume type is FAT16
Volume size (bytes): 126070784
Volume size (Kbytes): 123116
Volume size (Mbytes): 120
Files found on the card (name, date and size in bytes):
LOST.DIR/ 2015-10-08 03:42:08
ANDROID/ 2015-10-08 03:42:10
DATA/ 2015-10-08 14:04:16
NOMEDI~1 2015-10-08 03:42:10 0
COMMEI~1.BAC/ 2015-10-08 03:42:10
FILES/ 2015-10-08 03:42:10
COMMEI~1.FIL/ 2015-10-08 03:43:16
FILES/ 2015-10-08 03:43:16
COMMEI~1.SAF/ 2015-10-08 03:44:42
FILES/ 2015-10-08 03:44:42
COMMEI~1.MST/ 2015-10-08 10:41:30
CACHE/ 2015-10-08 10:41:30
FILES/ 2015-10-08 10:41:30
COMMEI~1.GAM/ 2015-10-08 10:41:30
FILES/ 2015-10-08 10:41:30
COMMEI~1.CUS/ 2015-10-08 14:04:16
FILES/ 2015-10-08 14:04:16
128Mb的卡读出来120Mb,没什么问题,下面这个文件列表应该是在某个安卓机上待过的痕迹,文件系统理所应当的是FAT16。
再测一下小的模块,线连好之后,上电,注意这里要接5V电源,3.3V的有可能可以检测到卡,但读不出卡里的文件。CS管脚连接的是PIN4,也可以自己在程序里改。如果遇到过流,Arduino无法识别了,重启下电脑就可以。
结果没有问题,也输出了上面的信息,程序是一个程序,就不贴在这里了。