No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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