SD non funziona su MEGA 2560, tutto ok su UNO

Buongiorno a tutti, ho un problema (credo) con la libreria SD.h usata su Arduino MEGA2560 con SPI bus settato così:
MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 53

Ora per non sbagliare (!) ho usato uno sketch esempio tra quelli proposti dalla libreria stessa, che riporto:



/*
  SD card basic file example
 
 This example shows how to create and destroy an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header

 created   Nov 2010 by David A. Mellis
 modified 9 Apr 2012 by Tom Igoe
 modified 13 June 2012 by Limor Fried
 
 This example code is in the public domain.
 	 
 */
#include <SPI.h>
#include <SD.h>

File root;

// change this to match your SD shield or module;
//     Arduino Ethernet shield: pin 4
//     Adafruit SD shields and modules: pin 10
//     Sparkfun SD shield: pin 8
const int chipSelect = 4;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  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 Arduino Uno boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(SS, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  root = SD.open("/");
  
  printDirectory(root, 0);
  
  Serial.println("done!");
}

void loop()
{
  // nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs) {
  // Begin at the start of the directory
  dir.rewindDirectory();
  
  while(true) {
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       //Serial.println("**nomorefiles**");
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');   // we'll have a nice indentation
     }
     // Print the 8.3 name
     Serial.print(entry.name());
     // Recurse for directories, otherwise print the file size
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
     entry.close();
   }
}

A parte il fatto che GIÀ IL SORGENTE È SBAGLIATO PER IL MEGA in quanto nel primo commento scrive:

«** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
and pin #52 (SS) must be an output»
ripetendo due volte il pin 52 e chiamandolo prima CS e poi SS, poi non capisco se va connesso sul 4, 10, 53, o lasciato scollegato, e se va impostato come uscita e settato o meno!!!

Fatto sta che su UNO va tutto alla grande, stesso codice ma impostando i pin da 11, 12, 13 e 4 a 50, 51 52 53 non va più. Ovviamente i collegamenti sono corretti. Quello che non capisco è la differenza tra SS e CS (sono lo stesso pin?) e perché non va, la libreia è sbagliata?

Sto impazzendo aiuto!

Il reference della libreria SD specifica:

The communication between the microcontroller and the SD card uses SPI, which takes place on digital pins 11, 12, and 13 (on most Arduino boards) or 50, 51, and 52 (Arduino Mega). Additionally, another pin must be used to select the SD card. This can be the hardware SS pin - pin 10 (on most Arduino boards) or pin 53 (on the Mega) - or another pin specified in the call to SD.begin(). Note that even if you don’t use the hardware SS pin, it must be left as an output or the SD library won’t work.

Ora, mentre MISO, MOSI e SCK sono fissi, come pin di SS puoi usare il pin che vuoi (basta che lo passi al metodo begin()) ricordando che COMUNQUE il pin 53 della MEGA, SE non usato come SS, va dichiarato come OUTPUT, messo HIGH e non utilizzato per nessuna altra cosa.

Guglielmo

P.S.: SS (Slave Select) o CS (Chip Select) indicano comunque lo stesso pin di un device slave che se tenuto alto disabilita il bus SPI sullo slave, se portato basso lo attiva.

1 Like

Grazie mille Guglielmo per la celere risposta (ci contavo :D) ma non cambia nulla, dimmi se noti qualche errore, ho impostato CS come pin 4 e SS OUTPUT e HIGH e non usato, ma continua a non andare, ecco il codice:

// Directory of SD

#include <SPI.h>
#include <SD.h>

File root;

const int chipSelect = 4;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(SS, OUTPUT);
  pinMode(SS, HIGH);
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  root = SD.open("/");
   printDirectory(root, 0);
   Serial.println("done!");
}

void loop() {
  }

void printDirectory(File dir, int numTabs) {
  dir.rewindDirectory();
  while(true) {
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       Serial.println("no more files.");
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');   // we'll have a nice indentation
     }
     // Print the 8.3 name
     Serial.print(entry.name());
     // Recurse for directories, otherwise print the file size
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
     entry.close();
   }
}

Cioè, tu come lo scriveresti per Arduino MEGA 2560? Ho sbagliato a capire? Ho provato in tutti i modi... :frowning:

La mia paura è che la libreria su Mega2560 non funzioni, anche i commenti sono errati (scrive due volte il pin 52 con connessioni diverse...)!!!
Qualcuno ci è mai riuscito a usare un MEGA2560 con la libreria SD.h attuale? Se sì, come a fatto, qual è la connessione corretta? Chiedo solo le quattro righe che dichiarano i pin, per il resto mi arrangio.
P.S.: Io uso la IDE 1.8.18.

Ho cancellato e poi ricaricato da zip (scaricato da https://downloads.arduino.cc/libraries/github.com/arduino-libraries/SD-1.2.4.zip) l'ultima versione, riscritto il codice sottoriportato, ed ora funziona! Credo fosse la libreria danneggiata. Grazie a tutti per la collaborazione!

Davide

// Directory of SD
// Arduino MEGA2560 setting by KMZ

#include <SPI.h>
#include <SD.h>

File root;

const int chipSelect = 53;  // SS on MEGA2560

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(SS, OUTPUT);
  if (!SD.begin(chipSelect)) {
    Serial.println("...initialization failed!");
    return;
  }
  Serial.println("...initialization done.");
  root = SD.open("/");
  printDirectory(root, 0);
  Serial.println("Done!");
}

void loop() {
}

void printDirectory(File dir, int numTabs) {
  dir.rewindDirectory();
  while (true) {
    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      Serial.println("no more files.");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');   // we'll have a nice indentation
    }
    // Print the 8.3 name
    Serial.print(entry.name());
    // Recurse for directories, otherwise print the file size
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

Ma la libreria SD è di base e comunque, in generale ... le librerie ufficiali, per evitare problemi, NON vanno installate dallo .zip, ma dal "library manager" del IDE che conosce anche le varie dipendenze !

Guglielmo

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.