Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

90 lines
2.0KB

  1. /*
  2. SD card read/write
  3. This example shows how to read and write data to and from an SD card file
  4. The circuit:
  5. * SD card attached to SPI bus as follows:
  6. ** MOSI - pin 11
  7. ** MISO - pin 12
  8. ** CLK - pin 13
  9. ** CS - pin 4
  10. created Nov 2010
  11. by David A. Mellis
  12. modified 9 Apr 2012
  13. by Tom Igoe
  14. This example code is in the public domain.
  15. */
  16. #define SD_CS_PIN SS
  17. #include <SPI.h>
  18. //#include <SD.h>
  19. #include "SdFat.h"
  20. SdFat SD;
  21. File myFile;
  22. void setup()
  23. {
  24. // Open serial communications and wait for port to open:
  25. Serial.begin(9600);
  26. // Wait for USB Serial
  27. while (!Serial) {
  28. SysCall::yield();
  29. }
  30. Serial.print("Initializing SD card...");
  31. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  32. // Note that even if it's not used as the CS pin, the hardware SS pin
  33. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  34. // or the SD library functions will not work.
  35. pinMode(10, OUTPUT);
  36. if (!SD.begin(SD_CS_PIN)) {
  37. Serial.println("initialization failed!");
  38. return;
  39. }
  40. Serial.println("initialization done.");
  41. // open the file. note that only one file can be open at a time,
  42. // so you have to close this one before opening another.
  43. myFile = SD.open("test.txt", FILE_WRITE);
  44. // if the file opened okay, write to it:
  45. if (myFile) {
  46. Serial.print("Writing to test.txt...");
  47. myFile.println("testing 1, 2, 3.");
  48. // close the file:
  49. myFile.close();
  50. Serial.println("done.");
  51. } else {
  52. // if the file didn't open, print an error:
  53. Serial.println("error opening test.txt");
  54. }
  55. // re-open the file for reading:
  56. myFile = SD.open("test.txt");
  57. if (myFile) {
  58. Serial.println("test.txt:");
  59. // read from the file until there's nothing else in it:
  60. while (myFile.available()) {
  61. Serial.write(myFile.read());
  62. }
  63. // close the file:
  64. myFile.close();
  65. } else {
  66. // if the file didn't open, print an error:
  67. Serial.println("error opening test.txt");
  68. }
  69. }
  70. void loop()
  71. {
  72. // nothing happens after setup
  73. }