Using SdFat library:
#include <SdFat.h>
#include <SdFatUtil.h>
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
const int SDChipSelect = 9; // For my setup.
void setup(void)
{
Serial.begin(9600);
pinMode(SS_PIN, OUTPUT); // Actually the Sd2Card constructor does this
digitalWrite(SS_PIN, HIGH); // Make sure Ethernet SPI is not selected
Serial.println("Type any character to start");
while (!Serial.available())
;
Serial.println();
//
// If SPI_FULL_SPEED doesn't work with your setup, try SPI_HALF_SPEED
//
if (!card.init(SPI_FULL_SPEED, SDChipSelect)) {
Serial.println("card.init failed");
return;
}
// initialize a FAT volume
if (!volume.init(&card)) {
Serial.println("volume.init failed");
return;
}
Serial.print("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
// open the root directory
if (!root.openRoot(&volume))
{
Serial.println("openRoot failed");
return;
}
// open a file
if (file.open(&root, "TESTFILE.TXT", O_READ)) {
Serial.println("Opened TESTFILE.TXT");
}
else {
Serial.println("file.open failed");
return;
}
Serial.println();
dir_t d;
if (!file.dirEntry(&d)) {
Serial.println("file.dirEntry failed");
return;
}
Serial.print("Created : ");
file.printFatDate(d.creationDate);
Serial.print(' ');
file.printFatTime(d.creationTime);
Serial.println();
Serial.print("Last Modified: ");
file.printFatDate(d.lastWriteDate);
Serial.print(' ');
file.printFatTime(d.lastWriteTime);
Serial.println();
Serial.print("Last Accessed: ");
file.printFatDate(d.lastAccessDate);
Serial.println();
}
void loop(void) {}
Change the SDChipSelect definition to whatever your SD circuit uses
Note that if your SD circuit is on a separate shield and you don't have a new Ethernet shield plugged into your Arduino, you don't need the pinMode and digitalWrite to SS_PIN, but I put them there "just in case."
Output for that file on my 8 Megabyte SD card:
Volume is FAT16
Opened TESTFILE.TXT
Created : 2011-01-09 12:22:12
Last Modified: 2011-01-09 12:22:12
Last Accessed: 2011-01-21
Regards,
Dave
Footnote:If you have a larger SD card or microSD card formatted as FAT32 there is really, really (
really) no need to reformat it when using the SdFat library. Really. On the other hand if you have a really small card that came as FAT12, then you need to reformat it to FAT16.