Arduino中国 | Flamingo EDA

Dec/09

25

Arduino 电子积木 SD卡存储模块

SD卡是我们经常使用到的一种存储设备,像数码相机,MP3,阅读器,GPS导航仪等都会使用到它。在使用Arduino的时候,如何我们要将一些数据记录下来,就可以使用到SD卡。不过要让SD卡能够同Arduino配合使用可不是一件简单的事情,首先在硬件上我们要解决其与Arduino连接的问题,其次软件上我们还要解决文件的存储问题,以便使用读卡器之样的设备能够很方便地将保存在SD卡中的数据读出。

针对硬件上的需求,我们开发了SD卡模块,它通过SPI接口与Arduino进行通信:

同时,为了方便同Arduino的SPI接口进行连接,提供了相应的IDC扩展板,该扩展板上有一个专门为SPI设计的六芯插座:

电路的连接上则依然沿用了电子积木所一向强调的即插即用方式,我们只需要用6芯的IDC连接线,将IDC扩展板上的SPI插座与SD卡模块上的相应插座连接起来,就完成了电路部分的连接:

回到软件部分,我们的目标是要让SD卡上记录的数据能够在电脑上顺利地读出,因此在向SD卡上记录相应数据的时候需要遵循一定的规定,即文件系统的类型。对于像Arduino这样的小系统来讲,相对简单的FAT16是一个比较不错的选择。

在这里我们要用到读卡器来对SD卡进行相应的处理,将其格式化成FAT文件系统:



Arduino上使用的软件则是基于SparkFun提供的FAT库,但根据硬件差异做了一定的修改,你可以从这里下载到修改过的FAT库。该库需要被解压缩到Arduino的hardware\libraries子目录中。

测试时使用的时FAT库里自带的示例程序:

//Include all the libraries necessary for FAT32
#include <byteordering.h>
#include <fat.h>
#include <FAT16.h>
#include <fat_config.h>
#include <partition.h>
#include <partition_config.h>
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>

FAT TestFile;      //This will be the file we manipulate in the sketch
char buffer[512];  //Data will be temporarily stored to this buffer before being written to the file
int read_size=0;   //Used as an indicator for how many characters are read from the file
int count=0;       //Miscellaneous variable

void setup()
{
  Serial.begin(9600);  //Initiate serial communication at 9600 bps

  TestFile.initialize();  //Initialize the SD card and the FAT file system.
  Serial.println("Starting...");
  TestFile.create_file("Sample.txt");  //Create a file on the SD card named "Read_File_Test.txt"
                                       //NOTE: This function will return a 0 value if it was unable to create the file.
  TestFile.open();  //Now that the file has been created, open it so we can write to it.
}

void loop()
{
  TestFile.write("This is test data.");  //using the write function will always write to the beginning of the file. 
                                         // Here we add some text to the file.
  TestFile.close();  //We are done writing to the file for now. Close it for later use.

  while(1){
    TestFile.open();  //Open the file. When the file is opened we will be looking at the beginning of the file.
    read_size=TestFile.read(buffer); //Read the contents of the file. This will only read the amount of data specified
                                    // by the size of 'buffer.'

    Serial.println(read_size, DEC);  //Print the number of characters read by the read function.
    for(int i=0; iSerial.print(buffer[i], BYTE);  //Print out the contents of the buffer.
    }
    Serial.println();

    sprintf(buffer, "%d", count++); //Now we'll use the buffer to write data back to the file. 
                                   // Here's we'll only add one value to buffer, the 'count' variable. 
    TestFile.write(buffer);         //Write the new buffer to the end of the file
    TestFile.close();               //Close the file for later use.

    delay(1000);  //Wait one second before repeating the loop.
  }
}

这样Arduino在运行的时候,会不断地向SD卡模块上的Sample.txt文件中写入相应的数据,随后我们同样可以通过读卡器读出该文件中记录的内容。

需要注意的是,由于用来操作SD卡和文件系统的代码相对较多,目前该模块只能运行在带有ATmega328P的Arduino上面。另外,目前市场上可选的SD卡种类繁多,可能不是每种SD卡都能够被很好的兼容,目前我测试过通过的SD卡有:

  • SanDisk 2GB SD2 Card
  • Apacer 60X 1GB SD Card

测试没有通过的SD卡有:

  • SanDisk 16MB SD Card

·

4 comments

  • Bruce Fleming · 2010/11/24 at 5:16 AM

    By the way, when the Translate function is used on my webbrowser, it presents the weblink incorrectly. Spaces are introduced into the link that can be deleted and the link works correctly.

  • 客戶 · 2011/01/28 at 12:35 AM

    您好:我買了這塊模組,
    但是我的Arduino程式裡,沒有hardware\libraries這個路徑,hardware跟libraries兩資料夾是同時被放在arduino\路徑下。

    請問我該將FAT解壓縮在哪邊?

    目前我跑的結果下列這行出錯
    for(int i=0; iSerial.print(buffer[i], BYTE);

    error:’iserial’was not declared in this scope

  • Author comment by flamingoeda · 2011/02/18 at 7:23 AM

    你用的是新版本的Arduino,直接放到Arduino安装目录下的libraries目录中主就可以了。

  • Vincent · 2011/07/06 at 8:08 PM

    请问我购买的板子是Maple STM32(LeafLabs开发)的,能不能在上面使用Arduino的标准模块呢?我需要用到的模块有扩展板、SD读卡器和音乐播放器。

Leave a Reply

使用新浪微博登录

<<

>>

Theme Design by devolux.nh2.me