Ethernet Shield mit Mega2560 Zugriff auf SD geht nicht

Hallo,
bin Anfänger und dann gleich ein solches Problem. In einem größeren Sketch (deshalb Mega) will ich auf die SD zugreifen. Dies gelingt nicht.
Steckt das Ethernet Shield auf einem Uno, dann funktioniert es, auf dem Mega nicht. Das gleiche sketch, nur die Board-Einstellung in der IDE angepasst. Welchen dummen Fehler mache ich?
Gruß Gerd

Mein_nue_SD_wechsel.ino (686 Bytes)

Hallo,

ich kann keinen offensichtlichen Fehler feststellen.

Probiere mal "meinen" Code. Nackter Mega 2560 mit WiFi Shield und sd Karte drin.
Die LEDs und Taster mußte nicht beschalten.

/*

 Arduino Mega 2560
 Arduino WiFi Shield
 
 SD card test
   
 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.
   
 The circuit:
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
        Pin 4 used here for consistency with other Arduino examples

 
 created  28 Mar 2011
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
 */
 // include the SD library:
#include <SD.h>

int LED = 5;          // an Pin 5
int analogPin = 0;     // Poti am analog pin A0 
int val = 0;           // Potiwert speichern in val
long Zeitstempel = 0;   // aktueller millis() Wert
int Taster = 44;       // Taster an Pin 44

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
File myFile;

String valString = "";
String ZeitstempelString = "";

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
const int chipSelect = 4;    

void setup()  {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
/*  
  pinMode(LED, OUTPUT);     // LED Pins auf Ausgang setzen
  digitalWrite(LED, HIGH); // kurzer LED Funktionstest
  delay(500);                
  digitalWrite(LED, LOW);  // Ende des LED Funktionstest
  // digitalen Eingang mit PullUp aktivieren  
  pinMode(Taster, INPUT);            // setzt pin als Eingang
  digitalWrite(Taster, HIGH);        // aktiviert den PullUp Widerstand 
*/
 
  Serial.print("\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(53, OUTPUT);     // change this to 53 on a mega, normal 10


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
 
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

 
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
 
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);

val = analogRead(analogPin);
Zeitstempel = millis();

Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
//   pinMode(53, OUTPUT);
   
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  SD.remove("test.txt");    // Datei löschen
  Serial.println("test.txt deleted!");
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);  // Datei wird erzeugt wenn nicht vorhanden
  myFile.close();
  
  // re-open the file for reading:
  Serial.println("setup open");
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("new test.txt done");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
        Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}


void loop(void) { }

Füge mal nach den pinMode()-Aufrufen die folgende Zeile ein:

digitalWrite(10, HIGH);

Damit wird der Ethernet-Chip deaktiviert und er mischt sich nicht mehr in die Kommunikation mit der Karte ein.

digitalWrite(10, HIGH);
Das hat's gebracht. Danke
Ich meine es auch schon mehrmals versucht zu haben. Aber vielleicht nicht so.
OK. Dann bin ich erstmal soweit.
Jetzt muss ich den PC verlassen. Melde mich Morgen vermutlich wieder. Der nächste Schritt ist Zwischen Ethernet und Sd zu wechseln. Vom Ethernet hole ich die Zeit. Das funktioniert sehr gut. Mal sehn wie der Wechsel jetzt funktioniert.
erstmal vielen, vielen Dank
Gruß Gerd

So, nun bin ich wieder da.
Ich schaffe einfach nicht, mit Mega2560 und Ethernet Shield den Wechsel zwischen Ethernet und SD und wieder zurück. Mein Probiersketch ist angehangen.
Bin für jeden hilfreichen Tipp dankbar.
Gruß Gerd

Mein_SD_Ethernet_wechsel_2.ino (3.19 KB)

Beim wechsel der von SD auf Ethernet musst du unbedingt die ChipSelect Leitungungen umschalten und evtl ein bisschen warten bevor du mit Daten feuerst.
Gruß
Der Dani

beide ChipSelect schalt ich um und warte jetzt 1s, aber es ändert sich nichts. Sch...
Gruß Gerd

#include <SD.h>
#include <SPI.h>
#include<Ethernet.h>
#include<EthernetUDP.h>
File myFile;
boolean IstSchon;
const int CS_SD = 4;
const int CS_ETH = 10;
const int CS_MEGA = 53;


void setup() {
  Serial.begin(9600);
  pinMode(CS_SD, OUTPUT); // PIN 4 fuer Kommunikation mit SD-Karte
  pinMode(CS_ETH, OUTPUT); // PIN 10 fuert Kommunikation mit Ethernet
  pinMode(CS_MEGA, OUTPUT); // Nur MEGA: Pin 53 fuer SD-Card
//  IstSchon = false;
}

void loop() {
  
// 00 Ethernet aus, SD aus  
// --------------------
    digitalWrite(CS_ETH, HIGH); // deaktiviere Ethernet
    digitalWrite(CS_SD, HIGH);
    delay(1000);
    Serial.print("beide aus, ");
    if (!SD.begin(CS_SD)) {
//    if (!SD.begin(CS_SD) != IstSchon) {
//      IstSchon = true;  
        Serial.println("SD-Fehler oder nicht vorhanden - 00");  
    }
    else {
        myFile = SD.open("config.txt", FILE_READ); // starting at the beginning of the file
        if (myFile) {
            Serial.print("config geoeffnet, ");
        }
        else {
            Serial.print("config nicht gefunden, ");
        }
        myFile.close();
        Serial.println("config geschlossen - 00");
    }

// 01 Ethernet aus, SD ein  
// --------------------
    digitalWrite(CS_ETH, HIGH); // deaktiviere Ethernet
    digitalWrite(CS_SD, LOW);
    delay(1000);
    Serial.print("SD ein + Ethernet aus, ");
    if (!SD.begin(CS_SD)) {
//    if (!SD.begin(CS_SD) != IstSchon) {
//      IstSchon = true;  
      Serial.println("SD-Fehler oder nicht vorhanden - 01");  
    }
    else {
        myFile = SD.open("config.txt", FILE_READ); // starting at the beginning of the file
        if (myFile) {
            Serial.print("config geoeffnet, ");
        }
        else {
            Serial.print("config nicht gefunden, ");
        }
        myFile.close();
        Serial.println("config geschlossen - 01");
    }

// 10 Ethernet ein, SD aus  
// --------------------
    digitalWrite(CS_ETH, LOW); // aktiviere Ethernet
    digitalWrite(CS_SD, HIGH);
    delay(1000);
    Serial.print("Ethernet ein + SD aus, ");
    if (!SD.begin(CS_SD)) {
//    if (!SD.begin(CS_SD) != IstSchon) {
//      IstSchon = true;  
        Serial.println("SD-Fehler oder nicht vorhanden - 10");  
//        return;
    }
    else {
        myFile = SD.open("config.txt", FILE_READ); // starting at the beginning of the file
        if (myFile) {
            Serial.print("config geoeffnet, ");
        }
        else {
            Serial.print("config nicht gefunden, ");
        }
        myFile.close();
        Serial.println("config geschlossen - 10");
    }
    
// 11 Ethernet ein, SD ein  
// --------------------
    digitalWrite(CS_ETH, LOW); // aktiviere Ethernet
    digitalWrite(CS_SD, LOW);
    delay(1000);
    Serial.print("beide ein, ");
    if (!SD.begin(CS_SD)) {
//    if (!SD.begin(CS_SD) != IstSchon) {
//      IstSchon = true;  
        Serial.println("SD-Fehler oder nicht vorhanden - 11");  
    }
    else {
        myFile = SD.open("config.txt", FILE_READ); // starting at the beginning of the file
        if (myFile) {
            Serial.print("config geoeffnet, ");
        }
        else {
            Serial.print("config nicht gefunden, ");
        }
        myFile.close();
        Serial.println("config geschlossen - 11");
    }

  Serial.print("");
  delay(2000);
}

Du solltest SD.begin() nicht mehr als einmal aufrufen, schon gar nicht mehrmals innerhalb von loop(). Und setze die beiden CS-Leitungen im setup() HIGH, bevor Du die jeweiligen begin()-Methoden aufrufst. Nach den begin()-Aufrufen solltest Du sie nicht mehr anrühren, denn dann sind die Bibliotheken dafür verantwortlich (sie sollten das auch korrekt machen).

Verzweiflung macht sich breit.

Das Ergebnis sind die folgenden drei Zeilen.
Ethernet.begin nicht erfolgt
SD.begin nicht erfolgt
config nicht gefunden

Und der Code

#include <SD.h>
#include <SPI.h>
#include<Ethernet.h>
#include<EthernetUDP.h>
File myFile;
const int CS_SD = 4;
const int CS_ETH = 10;
const int CS_MEGA = 53;
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0xF7, 0x18};

void setup() {
    pinMode(CS_SD, OUTPUT); // PIN 4 fuer Kommunikation mit SD-Karte
    pinMode(CS_ETH, OUTPUT); // PIN 10 fuert Kommunikation mit Ethernet
    pinMode(CS_MEGA, OUTPUT); // Nur MEGA: Pin 53 fuer SD-Card
    digitalWrite(CS_ETH, HIGH); 
    digitalWrite(CS_SD, HIGH);
    Serial.begin(9600);

    if (Ethernet.begin((mac) == 0)) {
        Serial.println("Ethernet.begin nicht erfolgt");
    }
    if (!SD.begin(CS_SD)) {
        Serial.println("SD.begin nicht erfolgt");
    }
    myFile = SD.open("config.txt", FILE_READ); 
    if (myFile) {
        Serial.print("config geoeffnet, ");
        myFile.close();
        Serial.println("config geschlossen");
    }
    else {
        Serial.println("config nicht gefunden");
    }
}

void loop() {}

Irgendwie will es nicht so wie ich will.

Gruß Gerd

Du solltest SD.begin() nicht mehr als einmal aufrufen, schon gar nicht mehrmals innerhalb von loop(). Und setze die beiden CS-Leitungen im setup() HIGH, bevor Du die jeweiligen begin()-Methoden aufrufst. Nach den begin()-Aufrufen solltest Du sie nicht mehr anrühren, denn dann sind die Bibliotheken dafür verantwortlich (sie sollten das auch korrekt machen).

Habe mein Sketch so geändert. Bin sehr skeptisch. Aber bisher funktioniert's. Toll! Danke!
Gruß Gerd

Schau mal hier in dem Thread bei diesem Reply
http://forum.arduino.cc/index.php?topic=217579.msg1590860#msg1590860
da gibt es einen "Hack" der zulässt SD.Begin() mehrmals aufzurufen.

Gruß
Der Dani

Hallo,
ich glaub, die Änderung selbst vorzunehmen überfordert mich. Das traue ich mir nicht zu. Gibt es evtl. eine fertig-geänderte SD-Library?
Gruß Gerd