You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.8KB

  1. // Ported to SdFat from the native Arduino SD library example by Bill Greiman
  2. // On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
  3. const int chipSelect = 4;
  4. /*
  5. SD card read/write
  6. This example shows how to read and write data to and from an SD card file
  7. The circuit:
  8. * SD card attached to SPI bus as follows:
  9. ** MOSI - pin 11
  10. ** MISO - pin 12
  11. ** CLK - pin 13
  12. ** CS - pin 4
  13. created Nov 2010
  14. by David A. Mellis
  15. updated 2 Dec 2010
  16. by Tom Igoe
  17. modified by Bill Greiman 11 Apr 2011
  18. This example code is in the public domain.
  19. */
  20. #include <SdFat.h>
  21. SdFat sd;
  22. SdFile myFile;
  23. void setup() {
  24. Serial.begin(9600);
  25. while (!Serial) {} // wait for Leonardo
  26. Serial.println("Type any character to start");
  27. while (Serial.read() <= 0) {}
  28. delay(400); // catch Due reset problem
  29. // Initialize SdFat or print a detailed error message and halt
  30. // Use half speed like the native library.
  31. // change to SPI_FULL_SPEED for more performance.
  32. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  33. // open the file for write at end like the Native SD library
  34. if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
  35. sd.errorHalt("opening test.txt for write failed");
  36. }
  37. // if the file opened okay, write to it:
  38. Serial.print("Writing to test.txt...");
  39. myFile.println("testing 1, 2, 3.");
  40. // close the file:
  41. myFile.close();
  42. Serial.println("done.");
  43. // re-open the file for reading:
  44. if (!myFile.open("test.txt", O_READ)) {
  45. sd.errorHalt("opening test.txt for read failed");
  46. }
  47. Serial.println("test.txt:");
  48. // read from the file until there's nothing else in it:
  49. int data;
  50. while ((data = myFile.read()) >= 0) Serial.write(data);
  51. // close the file:
  52. myFile.close();
  53. }
  54. void loop() {
  55. // nothing happens after setup
  56. }