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

84 行
2.0KB

  1. /*
  2. SD card file dump
  3. This example shows how to read a file from the SD card using the
  4. SD library and send it over the serial port.
  5. The circuit:
  6. * SD card attached to SPI bus as follows:
  7. ** MOSI - pin 11, pin 7 on Teensy with audio board
  8. ** MISO - pin 12
  9. ** CLK - pin 13, pin 14 on Teensy with audio board
  10. created 22 December 2010
  11. by Limor Fried
  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. // On the Ethernet Shield, CS is pin 4. Note that even if it's not
  19. // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
  20. // 53 on the Mega) must be left as an output or the SD library
  21. // functions will not work.
  22. // change this to match your SD shield or module;
  23. // Arduino Ethernet shield: pin 4
  24. // Adafruit SD shields and modules: pin 10
  25. // Sparkfun SD shield: pin 8
  26. // Teensy audio board: pin 10
  27. // Wiz820+SD board: pin 4
  28. // Teensy 2.0: pin 0
  29. // Teensy++ 2.0: pin 20
  30. const int chipSelect = 4;
  31. void setup()
  32. {
  33. //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  34. //SPI.setMOSI(7); // Audio shield has MOSI on pin 7
  35. //SPI.setSCK(14); // Audio shield has SCK on pin 14
  36. // Open serial communications and wait for port to open:
  37. Serial.begin(9600);
  38. while (!Serial) {
  39. ; // wait for serial port to connect. Needed for Leonardo only
  40. }
  41. Serial.print("Initializing SD card...");
  42. // see if the card is present and can be initialized:
  43. if (!SD.begin(chipSelect)) {
  44. Serial.println("Card failed, or not present");
  45. // don't do anything more:
  46. return;
  47. }
  48. Serial.println("card initialized.");
  49. // open the file. note that only one file can be open at a time,
  50. // so you have to close this one before opening another.
  51. File dataFile = SD.open("datalog.txt");
  52. // if the file is available, write to it:
  53. if (dataFile) {
  54. while (dataFile.available()) {
  55. Serial.write(dataFile.read());
  56. }
  57. dataFile.close();
  58. }
  59. // if the file isn't open, pop up an error:
  60. else {
  61. Serial.println("error opening datalog.txt");
  62. }
  63. }
  64. void loop()
  65. {
  66. }