通信接口

SD卡专题 - SD卡在Arduino上的应用

字号+ 作者:PLC工程师 来源:未知 2016-04-12 23:10 我要评论( )

SD卡在Arduino上的应用

SD Library

The SD library allows for reading from and writing to SD cards, e.g. on the Arduino Ethernet Shield. It is built on sdfatlib by William Greiman. The library supports FAT16 and FAT32 file systems on standard SD cards and SDHC cards. It uses short 8.3 names for files. The file names passed to the SD library functions can include paths separated by forward-slashes, /, e.g. "directory/filename.txt". Because the working directory is always the root of the SD card, a name refers to the same file whether or not it includes a leading slash (e.g. "/file.txt" is equivalent to "file.txt"). As of version 1.0, the library supports opening multiple files.

The communication between the microcontroller and the SD card uses SPI, which takes place on digital pins 11, 12, and 13 (on most Arduino boards) or 50, 51, and 52 (Arduino Mega). Additionally, another pin must be used to select the SD card. This can be the hardware SS pin - pin 10 (on most Arduino boards) or pin 53 (on the Mega) - or another pin specified in the call to SD.begin(). Note that even if you don't use the hardware SS pin, it must be left as an output or the SD library won't work.

Notes on using the Library and various shields

Examples

 

  • Card Info: Get info about your SD card.
  • Datalogger: Log data from three analog sensors to an SD card.
  • Dump File: Read a file from the SD card.
  • Files: Create and destroy an SD card file.
  • List Files: Print out the files in a directory on a SD card.
  • Read Write: Read and write data to and from an SD card.

 

以下是SD卡操作的类:

begin()

Description

Initializes the SD library and card. This begins use of the SPI bus (digital pins 11, 12, and 13 on most Arduino boards; 50, 51, and 52 on the Mega) and the chip select pin, which defaults to the hardware SS pin (pin 10 on most Arduino boards, 53 on the Mega). Note that even if you use a different chip select pin, the hardware SS pin must be kept as an output or the SD library functions will not work.

Syntax

SD.begin()
SD.begin(cspin)

Parameters

cspin (optional): the pin connected to the chip select line of the SD card; defaults to the hardware SS line of the SPI bus

Returns

true on success; false on failure

exists()

Description

Tests whether a file or directory exists on the SD card.

Syntax

SD.exists(filename)

Parameters

filename: the name of the file to test for existence, which can include directories (delimited by forward-slashes, /)

Returns

true if the file or directory exists, false if not

mkdir()

Description

Create a directory on the SD card. This will also create any intermediate directories that don't already exists; e.g. SD.mkdir("a/b/c") will create a, b, and c.

Syntax

SD.mkdir(filename)

Parameters

filename: the name of the directory to create, with sub-directories separated by forward-slashes, /

Returns

true if the creation of the directory succeeded, false if not
 

open()

Description

Opens a file on the SD card. If the file is opened for writing, it will be created if it doesn't already exist (but the directory containing it must already exist).

Syntax

SD.open(filepath)
SD.open(filepath, mode)

Parameters

filename: the name the file to open, which can include directories (delimited by forward slashes, /) - char *

mode (optional): the mode in which to open the file, defaults to FILE_READ - byte. one of:

  • FILE_READ: open the file for reading, starting at the beginning of the file.
  • FILE_WRITE: open the file for reading and writing, starting at the end of the file.

Returns

a File object referring to the opened file; if the file couldn't be opened, this object will evaluate to false in a boolean context, i.e. you can test the return value with "if (f)".
 

remove()

Description

Remove a file from the SD card.

Syntax

SD.remove(filename)

Parameters

filename: the name of the file to remove, which can include directories (delimited by forward-slashes, /)

Returns

true if the removal of the file succeeded, false if not. (if the file didn't exist, the return value is unspecified)
 

rmdir()

Description

Remove a directory from the SD card. The directory must be empty.

Syntax

SD.rmdir(filename)

Parameters

filename: the name of the directory to remove, with sub-directories separated by forward-slashes, /

Returns

true if the removal of the directory succeeded, false if not. (if the directory didn't exist, the return value is unspecified)


以下是文件操作的类:

available()

Check if there are any bytes available for reading from the file.

available() inherits from the Stream utility class.

Syntax

file.available()

Parameters

file: an instance of the File class (returned by SD.open())

Returns

the number of bytes available (int)
 

close()

Close the file, and ensure that any data written to it is physically saved to the SD card.

Syntax

file.close()

Parameters

file: an instance of the File class (returned by SD.open())

Returns

none

flush()

Ensures that any bytes written to the file are physically saved to the SD card. This is done automatically when the file is closed.

flush() inherits from the Stream utility class.

Syntax

file.flush()

Parameters

file: an instance of the File class (returned by SD.open())

Returns

none

peek()

Read a byte from the file without advancing to the next one. That is, successive calls to peek() will return the same value, as will the next call to read().

peek() inherits from the Stream utility class.

Syntax

file.peek()

Parameters

file: an instance of the File class (returned by SD.open())

Returns

The next byte (or character), or -1 if none is available.

position()

Get the current position within the file (i.e. the location to which the next byte will be read from or written to).

Syntax

file.position()

Parameters

file: an instance of the File class (returned by SD.open())

Returns

the position within the file (unsigned long)
 

print()

Description

Print data to the file, which must have been opened for writing. Prints numbers as a sequence of digits, each an ASCII character (e.g. the number 123 is sent as the three characters '1', '2', '3').

Syntax

file.print(data) 
file.print(data, BASE)

Parameters

file: an instance of the File class (returned by SD.open())

data: the data to print (char, byte, int, long, or string)

BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).

Returns

byte
print() will return the number of bytes written, though reading that number is optional
 

println()

Description

Print data, followed by a carriage return and newline, to the File, which must have been opened for writing. Prints numbers as a sequence of digits, each an ASCII character (e.g. the number 123 is sent as the three characters '1', '2', '3').

Syntax

file.println() 
file.println(data) 
file.print(data, BASE)

Parameters

file: an instance of the File class (returned by SD.open())

data (optional): the data to print (char, byte, int, long, or string)

BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).

Returns

byte
println() will return the number of bytes written, though reading that number is optional
 

seek()

Seek to a new position in the file, which must be between 0 and the size of the file (inclusive).

Syntax

file.seek(pos)

Parameters

file: an instance of the File class (returned by SD.open())

pos: the position to which to seek (unsigned long)

Returns

true for success, false for failure (boolean)
 

size()

Get the size of the file.

Syntax

file.size()

Parameters

file: an instance of the File class (returned by SD.open())

Returns

the size of the file in bytes (unsigned long)
 

read()

Read a byte from the file.

read() inherits from the Stream utility class.

Syntax

file.read()

Parameters

file: an instance of the File class (returned by SD.open())

Returns

The next byte (or character), or -1 if none is available.
 

write()

Description

Write data to the file.

Syntax

file.write(data) 
file.write(buf, len)

Parameters

file: an instance of the File class (returned by SD.open())

data: the byte, char, or string (char *) to write

buf: an array of characters or bytes

len: the number of elements in buf

Returns

byte
write() will return the number of bytes written, though reading that number is optional
 

isDirectory()

Directories (or folders) are special kinds of files, this function reports if the current file is a directory or not.

Syntax

file.isDirectory()

Parameters

file: an instance of the File class (returned by file.open()

Returns

boolean

Example


#include <SD.h>

File root;

void setup()
{
  Serial.begin(9600);
  pinMode(10, OUTPUT);

  SD.begin(10);

  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
}

void loop()
{
  // nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs) {
   while(true) {

     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       //Serial.println("**nomorefiles**");
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
   }
}


openNextFile()

Reports the next file or folder in a directory.

Syntax

file.openNextFile()

Parameters

file: an instance of the File class that is a directory

Returns

char : the next file or folder in the path

Example


#include <SD.h>

File root;
void setup()
{
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  SD.begin(10);
  root = SD.open("/");
  printDirectory(root, 0);
  delay(2000);
  Serial.println();
  Serial.println("Rewinding, and repeating below:" );
  Serial.println();
  delay(2000);
  root.rewindDirectory();
  printDirectory(root, 0);
  root.close();
}

void loop()
{
  // nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs)
{
  while (true)
  {
    File entry = dir.openNextFile();
    if (! entry)
    {
      if (numTabs == 0)
        Serial.println("** Done **");
      return;
    }
    for (uint8_t i = 0; i < numTabs; i++)
      Serial.print('\t');
    Serial.print(entry.name());
    if (entry.isDirectory())
    {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    }
    else
    {
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

rewindDirectory()

rewindDirectory() will bring you back to the first file in the directory, used in conjunction with openNextFile().

Syntax

file.rewindDirectory()

Parameters

file: an instance of the File class.

Returns

None

#include <SD.h>

File root;

void setup()
{
  Serial.begin(9600);
  pinMode(10, OUTPUT);

  SD.begin(10);

  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
}

void loop()
{
  // nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs) {
   while(true) {

     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       // return to the first file in the directory
       dir.rewindDirectory();
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
   }
}

如需更详细资料,如有好资源分享,请加入QQ交流群:214461008

欢迎阅读,欢迎转载,不用注明出处!本站只提供入门和进阶资料,做您学习的小伙伴!

老司机也欢迎光临指导!有好内容请加群:214461008 不要看声明了,看内容吧!

相关文章
  • Arduino软串口的使用

    Arduino软串口的使用

    2016-05-04 15:27

  • [2016.05.04]Arduino串口从基础到高级最全面教程

    [2016.05.04]Arduino串口从基础到高级最全面教程

    2016-05-05 14:14

  • 手机APP通过蓝牙控制Arduino上的通讯实验及完整例程

    手机APP通过蓝牙控制Arduino上的通讯实验及完整例程

    2016-05-02 22:13

  • Arduino串口高级用法

    Arduino串口高级用法

    2016-05-17 09:24

网友点评
Arduino