MinimumSizeSdReader.ino 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Create a text file on the SD with this path using short 8.3 names.
  2. #define SFN_PATH "/DIR/TEST.TXT"
  3. // Modify CS_PIN for your chip select pin.
  4. #define CS_PIN SS
  5. // Set USE_SD_H to one for SD.h or zero for SdFat.
  6. #define USE_SD_H 0
  7. #if USE_SD_H
  8. #include "SD.h"
  9. File file;
  10. #else
  11. #include "SdFat.h"
  12. // Setting ENABLE_DEDICATED_SPI to zero saves over 200 more bytes.
  13. #if ENABLE_DEDICATED_SPI
  14. #warning "Set ENABLE_DEDICATED_SPI zero in SdFat/src/SdFatConfig.h for minimum size"
  15. #endif // ENABLE_DEDICATED_SPI
  16. // Insure FAT16/FAT32 only.
  17. SdFat32 SD;
  18. // FatFile does not support Stream functions, just simple read/write.
  19. FatFile file;
  20. #endif
  21. void error(const char* msg) {
  22. Serial.println(msg);
  23. while(true);
  24. }
  25. void setup() {
  26. int n;
  27. char buf[4];
  28. Serial.begin(9600);
  29. while (!Serial) {}
  30. Serial.println("Type any character to begin");
  31. while (!Serial.available()) {}
  32. if (!SD.begin(CS_PIN)) error("SD.begin");
  33. #if USE_SD_H
  34. file = SD.open(SFN_PATH);
  35. if (!file) error("open");
  36. #else
  37. // Open existing file with a path of 8.3 names.
  38. // Directories will be opened O_RDONLY files O_RDWR.
  39. if (!file.openExistingSFN(SFN_PATH)) error("open");
  40. #endif
  41. while ((n = file.read(buf, sizeof(buf)))) {
  42. Serial.write(buf, n);
  43. }
  44. // close() is only needed if you write to the file. For example, read
  45. // config data, modify the data, rewind the file and write the data.
  46. // file.close();
  47. }
  48. void loop() {
  49. }