您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

97 行
2.3KB

  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, pin 7 on Teensy with audio board
  7. ** MISO - pin 12
  8. ** CLK - pin 13, pin 14 on Teensy with audio board
  9. ** CS - pin 4, pin 10 on Teensy with audio board
  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. // Teensy 3.5 & 3.6 on-board: BUILTIN_SDCARD
  25. // Wiz820+SD board: pin 4
  26. // Teensy 2.0: pin 0
  27. // Teensy++ 2.0: pin 20
  28. const int chipSelect = 4;
  29. void setup()
  30. {
  31. //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  32. //SPI.setMOSI(7); // Audio shield has MOSI on pin 7
  33. //SPI.setSCK(14); // Audio shield has SCK on pin 14
  34. // Open serial communications and wait for port to open:
  35. Serial.begin(9600);
  36. while (!Serial) {
  37. ; // wait for serial port to connect. Needed for Leonardo only
  38. }
  39. Serial.print("Initializing SD card...");
  40. if (!SD.begin(chipSelect)) {
  41. Serial.println("initialization failed!");
  42. return;
  43. }
  44. Serial.println("initialization done.");
  45. // open the file. note that only one file can be open at a time,
  46. // so you have to close this one before opening another.
  47. myFile = SD.open("test.txt", FILE_WRITE);
  48. // if the file opened okay, write to it:
  49. if (myFile) {
  50. Serial.print("Writing to test.txt...");
  51. myFile.println("testing 1, 2, 3.");
  52. // close the file:
  53. myFile.close();
  54. Serial.println("done.");
  55. } else {
  56. // if the file didn't open, print an error:
  57. Serial.println("error opening test.txt");
  58. }
  59. // re-open the file for reading:
  60. myFile = SD.open("test.txt");
  61. if (myFile) {
  62. Serial.println("test.txt:");
  63. // read from the file until there's nothing else in it:
  64. while (myFile.available()) {
  65. Serial.write(myFile.read());
  66. }
  67. // close the file:
  68. myFile.close();
  69. } else {
  70. // if the file didn't open, print an error:
  71. Serial.println("error opening test.txt");
  72. }
  73. }
  74. void loop()
  75. {
  76. // nothing happens after setup
  77. }