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.

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