I bought this shield microSD Shield Retail - RTL-09899 - SparkFun Electronics to use with a mega 2560 and I am having trouble figuring out where to map the ports to (that is if I even have to change them).
I am using some modified code from the sparkfun site.
/*
Basic example reading and writing to the microSD shield
Based on the SdFat Library by Bill Greiman
SparkFun Electronics
This example creates a file (or opens it if the file already exists) named 'Test.txt.'
A string with the value 'Millis: (number of millis)' is appended to the end of the file.
After writing to the file, the contents of the entire file are printed to the serial port.
*/
//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>
#include <ctype.h>
//Create the variables to be used by SdFat Library
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
char name[] = "Test.txt"; //Create an array that contains the name of our file.
char contents[256]; //This will be a data buffer for writing contents to the file.
char in_char=0;
int index=0; //Index will keep track of our position within the contents buffer.
void setup(void)
{
Serial.begin(9600); //Start a serial connection.
pinMode(10, OUTPUT); //Pin 10 must be set as an output for the SD communication to work.
card.init(); //Initialize the SD card and configure the I/O pins.
volume.init(card); //Initialize a volume on the SD card.
root.openRoot(volume); //Open the root directory in the volume.
Serial.println("SDcard Test Program V0.0.1");
}
void loop(void){
file.open(root, name, O_WRITE); //Open or create the file 'name' in 'root' for writing to the end of the file.
sprintf(contents, "Test"); //Copy the letters 'Millis: ' followed by the integer value of the millis() function into the 'contents' array.
file.print(contents); //Write the 'contents' array to the end of the file.
Serial.println("SDcard Written");
file.close(); //Close the file.
Serial.println(contents);
file.open(root, name, O_READ); //Open the file in read mode.
in_char=file.read(); //Get the first byte in the file.
//Keep reading characters from the file until we get an error or reach the end of the file. (This will output the entire contents of the file).
while(in_char >=0){ //If the value of the character is less than 0 we've reached the end of the file.
Serial.print(in_char); //Print the current character
in_char=file.read(); //Get the next character
}
file.close(); //Close the file
delay(1000); //Wait 1 second before repeating the process.
}
I am currently using the sdfatlib20111205 library (this is the only version that didn't give me a ton of errors).
Right now it isn't working at all. Any guidance as to how to wire this up would be much appreciated.