This class was written for the Arduino library.
It reads and writes directly to sectors on a sd card.
It has only two functions:
SDCARD.readblock(sector number); //read 512 bytes from this sector
SDCARD.writeblock(sector number); //write 512 bytes to this sector
The 512 bytes are read/write from a buffer which must be declared:
volatile unsigned char buffer[512] ; //contains the 512 bytes to read/write
This class was written for my Arduino 2009 weather station.
I want to log up to 10 sensors for an extended period when the Arduino is not
connected to the computer.
To save a sample every second over 12 months requires:
total bytes = 2 x 10 x 60 x 60 x 24 x 365 = 640 Mb
I use a 1.0Gb card.
The functions can both return a error value:
error = SDCARD.writeblock(sector);
error = SDCARD.readblock(sector);
If the card read/write does not work due to card failure or removal
the error will be greater than 0.
In my application this would mean the program can no longer operate.
In this case I send an error message instead of the normal samples
from the weater station.
This class does not use a FAT. Data is read/write to a sector direct.
The sd card circuit, the library and other files are at the site given below.
Download sdcardlibrary.zip
If you have a 3.3v arduino the level change circuits will not be needed.
An example program to read/write to one sector is:
/* Connect sd card as shown in circuit diagram.
Copy SDCARD folder into arduino-0017/hardware/libraries.
This code will write/read directly to a sector on the sd card
sector number 0 to 1980000 for a 1.0GB card.
*/
#include <SDCARD.h>
volatile unsigned char buffer[512] ; // this 512 bytes read from or written to sd card
unsigned long sector = 99999; // the sector we will write or read from
int error = 0; //the error will be 0 if the sd card works
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // initialize serial communication with computer
}
void loop() // run over and over again
{
if (Serial.available() > 0) // do nothing if have not received a byte by serial
{
int inByte = Serial.read();
if (inByte == 'r') // send a "r" to start the read / write sector process
{
int i = 0; //general purpose counter
for(i=0;i<512;i++)
buffer[i]=199; //fill the buffer with a number between 0 and 255
error = SDCARD.writeblock(sector); //write the buffer to this sector on the sd card
if (error !=0)
{
Serial.print("sd card write error... code = ");
Serial.println(error);
}//end of if we have an error
error = SDCARD.readblock(sector); //read into the buffer this sector in sd card
if (error !=0)
{
Serial.print("sd card read error... code = ");
Serial.println(error);
}//end of if we have an error
else
{
int number =0;
for(i=0; i<512; i++) //read 512 bytes
{
number = buffer[i]; //convert to integer
if(((i % 8) == 0) & ( i != 0))
Serial.println(""); //write 8 then start a new line
Serial.print(number);
Serial.print(" "); //tab to next number
}//end of read 512 bytes
}//end of else we have no error
} //end of received "r"
}//end of serial available
}//end of loop