mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-30 16:24:09 +03:00
Combining MemoryCard and SdFat libraries into SD library.
Defaulting to CS pin 4 (for Arduino Ethernet Shield). Holding the SS pin high (for disabling W5100 on the Ethernet Shield).
This commit is contained in:
71
libraries/SD/utility/examples/SdFatPrint/SdFatPrint.pde
Executable file
71
libraries/SD/utility/examples/SdFatPrint/SdFatPrint.pde
Executable file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Print Example
|
||||
*
|
||||
* This sketch shows how to use the Arduino Print class with SdFat.
|
||||
*/
|
||||
#include <SdFat.h>
|
||||
#include <SdFatUtil.h> // use functions to print strings from flash memory
|
||||
|
||||
Sd2Card card;
|
||||
SdVolume volume;
|
||||
SdFile root;
|
||||
SdFile file;
|
||||
|
||||
// store error strings in flash to save RAM
|
||||
#define error(s) error_P(PSTR(s))
|
||||
|
||||
void error_P(const char* str) {
|
||||
PgmPrint("error: ");
|
||||
SerialPrintln_P(str);
|
||||
if (card.errorCode()) {
|
||||
PgmPrint("SD error: ");
|
||||
Serial.print(card.errorCode(), HEX);
|
||||
Serial.print(',');
|
||||
Serial.println(card.errorData(), HEX);
|
||||
}
|
||||
while(1);
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(9600);
|
||||
Serial.println();
|
||||
PgmPrintln("Type any character to start");
|
||||
while (!Serial.available());
|
||||
|
||||
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
|
||||
// breadboards. use SPI_FULL_SPEED for better performance.
|
||||
if (!card.init(SPI_HALF_SPEED)) error("card.init failed");
|
||||
|
||||
// initialize a FAT volume
|
||||
if (!volume.init(&card)) error("volume.init failed");
|
||||
|
||||
// open the root directory
|
||||
if (!root.openRoot(&volume)) error("openRoot failed");
|
||||
|
||||
// create a new file
|
||||
char name[] = "PRINT00.TXT";
|
||||
for (uint8_t i = 0; i < 100; i++) {
|
||||
name[5] = i/10 + '0';
|
||||
name[6] = i%10 + '0';
|
||||
// only create new file for write
|
||||
if (file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)) break;
|
||||
}
|
||||
if (!file.isOpen()) error ("file.create");
|
||||
PgmPrintln("Printing to: ");
|
||||
Serial.println(name);
|
||||
|
||||
// clear print error
|
||||
file.writeError = 0;
|
||||
|
||||
// print 100 line to file
|
||||
for (uint8_t i = 0; i < 100; i++) {
|
||||
file.print("line ");
|
||||
file.print(i, DEC);
|
||||
file.print(" millis = ");
|
||||
file.println(millis());
|
||||
}
|
||||
// force write of all data to the SD card
|
||||
if (file.writeError || !file.sync()) error ("print or sync");
|
||||
PgmPrintln("Done");
|
||||
}
|
||||
void loop(void){}
|
Reference in New Issue
Block a user