Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

94 lines
2.2KB

  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. #include <SD.h>
  17. File myFile;
  18. // change this to match your SD shield or module;
  19. // Arduino Ethernet shield: pin 4
  20. // Adafruit SD shields and modules: pin 10
  21. // Sparkfun SD shield: pin 8
  22. // Teensy 2.0: pin 0
  23. // Teensy++ 2.0: pin 20
  24. const int chipSelect = 10;
  25. void setup()
  26. {
  27. // Open serial communications and wait for port to open:
  28. Serial.begin(9600);
  29. while (!Serial) {
  30. ; // wait for serial port to connect. Needed for Leonardo only
  31. }
  32. Serial.print("Initializing SD card...");
  33. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  34. // Note that even if it's not used as the CS pin, the hardware SS pin
  35. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  36. // or the SD library functions will not work.
  37. pinMode(10, OUTPUT);
  38. if (!SD.begin(chipSelect)) {
  39. Serial.println("initialization failed!");
  40. return;
  41. }
  42. Serial.println("initialization done.");
  43. // open the file. note that only one file can be open at a time,
  44. // so you have to close this one before opening another.
  45. myFile = SD.open("test.txt", FILE_WRITE);
  46. // if the file opened okay, write to it:
  47. if (myFile) {
  48. Serial.print("Writing to test.txt...");
  49. myFile.println("testing 1, 2, 3.");
  50. // close the file:
  51. myFile.close();
  52. Serial.println("done.");
  53. } else {
  54. // if the file didn't open, print an error:
  55. Serial.println("error opening test.txt");
  56. }
  57. // re-open the file for reading:
  58. myFile = SD.open("test.txt");
  59. if (myFile) {
  60. Serial.println("test.txt:");
  61. // read from the file until there's nothing else in it:
  62. while (myFile.available()) {
  63. Serial.write(myFile.read());
  64. }
  65. // close the file:
  66. myFile.close();
  67. } else {
  68. // if the file didn't open, print an error:
  69. Serial.println("error opening test.txt");
  70. }
  71. }
  72. void loop()
  73. {
  74. // nothing happens after setup
  75. }