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

93 行
2.0KB

  1. /*
  2. SD card basic file example
  3. This example shows how to create and destroy 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 root;
  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. root = SD.open("/");
  44. printDirectory(root, 0);
  45. Serial.println("done!");
  46. }
  47. void loop()
  48. {
  49. // nothing happens after setup finishes.
  50. }
  51. void printDirectory(File dir, int numTabs) {
  52. while(true) {
  53. File entry = dir.openNextFile();
  54. if (! entry) {
  55. // no more files
  56. //Serial.println("**nomorefiles**");
  57. break;
  58. }
  59. for (uint8_t i=0; i<numTabs; i++) {
  60. Serial.print('\t');
  61. }
  62. Serial.print(entry.name());
  63. if (entry.isDirectory()) {
  64. Serial.println("/");
  65. printDirectory(entry, numTabs+1);
  66. } else {
  67. // files have sizes, directories do not
  68. Serial.print("\t\t");
  69. Serial.println(entry.size(), DEC);
  70. }
  71. entry.close();
  72. }
  73. }