您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

74 行
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 <SPI.h>
  21. #include "SdFat.h"
  22. SdFat sd;
  23. SdFile myFile;
  24. void setup() {
  25. Serial.begin(9600);
  26. while (!Serial) {} // wait for Leonardo
  27. Serial.println("Type any character to start");
  28. while (Serial.read() <= 0) {}
  29. delay(400); // catch Due reset problem
  30. // Initialize SdFat or print a detailed error message and halt
  31. // Use half speed like the native library.
  32. // change to SPI_FULL_SPEED for more performance.
  33. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  34. sd.initErrorHalt();
  35. }
  36. // open the file for write at end like the Native SD library
  37. if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
  38. sd.errorHalt("opening test.txt for write failed");
  39. }
  40. // if the file opened okay, write to it:
  41. Serial.print("Writing to test.txt...");
  42. myFile.println("testing 1, 2, 3.");
  43. // close the file:
  44. myFile.close();
  45. Serial.println("done.");
  46. // re-open the file for reading:
  47. if (!myFile.open("test.txt", O_READ)) {
  48. sd.errorHalt("opening test.txt for read failed");
  49. }
  50. Serial.println("test.txt:");
  51. // read from the file until there's nothing else in it:
  52. int data;
  53. while ((data = myFile.read()) >= 0) {
  54. Serial.write(data);
  55. }
  56. // close the file:
  57. myFile.close();
  58. }
  59. void loop() {
  60. // nothing happens after setup
  61. }