Advertisement
mikroavr

hspi_sd_card

Jul 4th, 2024
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "PCF8574.h"
  2. PCF8574 pcf8574(0x20);
  3. byte pinWDT = P7;
  4. byte pinLED = P4;
  5.  
  6. bool stateWDT = 0;
  7.  
  8. #include "FS.h"
  9. #include "SD.h"
  10. #include "SPI.h"
  11. SPIClass spiSD(HSPI);
  12. #define SD_CS 14
  13. #define SDSPEED 27000000
  14. File root;
  15.  
  16. unsigned long cur_time, old_time ;
  17. bool stateLed = 0;
  18. const byte wdtPin =  12;
  19. const byte ledPin =  13;
  20.  
  21. bool wdtState = LOW;
  22. bool ledState = LOW;
  23.  
  24. void setup() {
  25.   Serial.begin(115200);
  26.   Serial.println("init pcf: ");
  27.   pcf8574.pinMode(pinWDT, OUTPUT);
  28.   pcf8574.pinMode(pinLED, OUTPUT);
  29.   if (pcf8574.begin()) {
  30.     Serial.println("OK");
  31.   } else {
  32.     Serial.println("KO");
  33.   }
  34.   delay(100);
  35.  
  36.   spiSD.begin(12, 2, 15, SD_CS); //SCK,MISO,MOSI,ss
  37.   while (!SD.begin( SD_CS, spiSD, SDSPEED)) {
  38.     // if(!SD.begin()){
  39.     Serial.println("Card Mount Failed");
  40.     delay(1000);
  41.   }
  42.   Serial.println("found SD card");
  43.   root = SD.open("/");
  44.   printDirectory(root, 0);
  45.   Serial.println("SD card Done");
  46.  
  47. }
  48.  
  49. void loop() {
  50.   cur_time = millis();
  51.   if (cur_time - old_time >= 1000) {
  52.     Serial.println("loop function");
  53.     seed_wdt();
  54.     old_time = cur_time;
  55.   }
  56. }
  57.  
  58. void printDirectory(File dir, int numTabs) {
  59.   while (true) {
  60.  
  61.     File entry =  dir.openNextFile();
  62.     if (! entry) {
  63.       // no more files
  64.       break;
  65.     }
  66.     for (uint8_t i = 0; i < numTabs; i++) {
  67.       Serial.print('\t');
  68.     }
  69.     Serial.print(entry.name());
  70.     if (entry.isDirectory()) {
  71.       Serial.println("/");
  72.       printDirectory(entry, numTabs + 1);
  73.     } else {
  74.       // files have sizes, directories do not
  75.       Serial.print("\t\t");
  76.       Serial.println(entry.size(), DEC);
  77.     }
  78.     entry.close();
  79.   }
  80. }
  81.  
  82.  
  83. void seed_wdt() {
  84.   stateWDT = !stateWDT;
  85.   pcf8574.digitalWrite(pinWDT, stateWDT);
  86.   pcf8574.digitalWrite(pinLED, stateWDT);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement