Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

78 lines
1.8KB

  1. // A simple read/write example for SD.h.
  2. // Mostly from the SD.h ReadWrite example.
  3. //
  4. // Your SD must be formatted FAT16/FAT32.
  5. //
  6. // Set USE_SD_H nonzero to use SD.h.
  7. // Set USE_SD_H zero to use SdFat.h.
  8. //
  9. #define USE_SD_H 0
  10. //
  11. #if USE_SD_H
  12. #include <SD.h>
  13. #else // USE_SD_H
  14. #include "SdFat.h"
  15. SdFat SD;
  16. #endif // USE_SD_H
  17. // Modify SD_CS_PIN for your board.
  18. // For Teensy 3.6 and SdFat.h use BUILTIN_SDCARD.
  19. #define SD_CS_PIN SS
  20. File myFile;
  21. void setup() {
  22. Serial.begin(9600);
  23. while (!Serial) {}
  24. #if USE_SD_H
  25. Serial.println(F("Using SD.h. Set USE_SD_H zero to use SdFat.h."));
  26. #else // USE_SD_H
  27. Serial.println(F("Using SdFat.h. Set USE_SD_H nonzero to use SD.h."));
  28. #endif // USE_SD_H
  29. Serial.println(F("\nType any character to begin."));
  30. while (!Serial.available()) {
  31. yield();
  32. }
  33. Serial.print("Initializing SD card...");
  34. if (!SD.begin(SD_CS_PIN)) {
  35. Serial.println("initialization failed!");
  36. return;
  37. }
  38. Serial.println("initialization done.");
  39. // open the file.
  40. myFile = SD.open("test.txt", FILE_WRITE);
  41. // if the file opened okay, write to it:
  42. if (myFile) {
  43. Serial.print("Writing to test.txt...");
  44. myFile.println("testing 1, 2, 3.");
  45. // close the file:
  46. myFile.close();
  47. Serial.println("done.");
  48. } else {
  49. // if the file didn't open, print an error:
  50. Serial.println("error opening test.txt");
  51. }
  52. // re-open the file for reading:
  53. myFile = SD.open("test.txt");
  54. if (myFile) {
  55. Serial.println("test.txt:");
  56. // read from the file until there's nothing else in it:
  57. while (myFile.available()) {
  58. Serial.write(myFile.read());
  59. }
  60. // close the file:
  61. myFile.close();
  62. } else {
  63. // if the file didn't open, print an error:
  64. Serial.println("error opening test.txt");
  65. }
  66. }
  67. void loop() {
  68. // nothing happens after setup
  69. }