Go Down

Topic: shield ethernet Arduino Mega (Read 592 times) previous topic - next topic

marsangola

Io ho utilizzato Arduino Mega e Ethernet shield,  il progetto è in "produzione" da dicembre e non ho avuto problemi.

bye.

Mauro_Titan

ho montato la Etehrnet Shiled sopra Arduino Mega2560 e ho caricao il prog. di esempio dove accede alla card , crea un files e poi legge il conenuto.

il montaggio e' identico ad arduino 2009 , ma quando lo lancio mi da errore i lettura della card .

ho provato a mettere anche 53 nel listato al posto di 10 ma non funzina
sulla doc ufficiale si parla deipin 51-52 -53 ma come sono collegai con la shiled ?
grazie.
Code: [Select]

/*
  SD card read/write

This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe

This example code is in the public domain.

*/

#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
  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(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  // 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);
 
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
   
    // 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()
{
// nothing happens after setup
}



astrobeed

A colpo d'occhio mi sembra uno sketch per la vecchia versione della Ethernet shield, prova questo che è sicuramente funzionante con la nuova versione, ti fornisce tutte le informazioni sulla microSD inserita nello slot, le legge anche se sono di tipo SDHC.
Attenzione che prima devi installare la nuova libreria sdFAT.

Code: [Select]


/*
* This sketch attempts to initialize a SD card and analyze its structure.
*/
#include <SdFat.h>
#include <SdFatUtil.h>

// offset to partition table
#define PART_OFFSET (512-64-2)

Sd2Card card;
SdVolume vol;

//global for card erase sector size
uint32_t sectorSize;

void sdError(void) {
 PgmPrintln("SD error");
 PgmPrint("errorCode: ");
 Serial.println(card.errorCode(), HEX);
 PgmPrint("errorData: ");
 Serial.println(card.errorData(), HEX);  
 return;
}

uint8_t cidDmp(void) {
 cid_t cid;
 if (!card.readCID(&cid)) {
   PgmPrint("readCID failed");
   sdError();
   return false;
 }
 PgmPrint("\nManufacturer ID: ");
 Serial.println(cid.mid, HEX);
 PgmPrint("OEM ID: ");
 Serial.print(cid.oid[0]);
 Serial.println(cid.oid[1]);
 PgmPrint("Product: ");
 for (uint8_t i = 0; i < 5; i++) {
   Serial.print(cid.pnm[i]);
 }
 PgmPrint("\nVersion: ");
 Serial.print(cid.prv_n, DEC);
 Serial.print('.');
 Serial.println(cid.prv_m, DEC);
 PgmPrint("Serial number: ");
 Serial.println(cid.psn);
 PgmPrint("Manufacturing date: ");
 Serial.print(cid.mdt_month);
 Serial.print('/');
 Serial.println(2000 + cid.mdt_year_low + (cid.mdt_year_high <<4));
 Serial.println();
 return true;
}

uint8_t csdDmp(void) {
 csd_t csd;
 uint8_t eraseSingleBlock;
 uint32_t cardSize = card.cardSize();
 if (cardSize == 0 || !card.readCSD(&csd)) {
   PgmPrintln("readCSD failed");
   sdError();
   return false;
 }
 if (csd.v1.csd_ver == 0) {
   eraseSingleBlock = csd.v1.erase_blk_en;
   sectorSize = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low;
 }
 else if (csd.v2.csd_ver == 1) {
   eraseSingleBlock = csd.v2.erase_blk_en;
   sectorSize = (csd.v2.sector_size_high << 1) | csd.v2.sector_size_low;
 }
 else {
   PgmPrintln("csd version error");
   return false;
 }
 sectorSize++;
 PgmPrint("cardSize: ");
 Serial.print(cardSize);
 PgmPrintln(" (512 byte blocks)");
 PgmPrint("flashEraseSize: ");
 Serial.print(sectorSize, DEC);
 PgmPrintln(" blocks");
 PgmPrint("eraseSingleBlock: ");
 if (eraseSingleBlock) {
   PgmPrintln("true");
 }
 else {
   PgmPrintln("false");
 }
 return true;
}
// print partition table
uint8_t partDmp(void) {
 part_t pt;
 PgmPrintln("\npart,boot,type,start,length");  
 for (uint8_t ip = 1; ip < 5; ip++) {
   if (!card.readData(0, PART_OFFSET + 16*(ip-1), 16, (uint8_t *)&pt)) {
     PgmPrint("read partition table failed");
     sdError();
     return false;
   }
   Serial.print(ip, DEC);
   Serial.print(',');
   Serial.print(pt.boot,HEX);
   Serial.print(',');
   Serial.print(pt.type, HEX);
   Serial.print(',');
   Serial.print(pt.firstSector);
   Serial.print(',');
   Serial.println(pt.totalSectors);
 }
 return true;
}

void volDmp(void) {
 PgmPrint("\nVolume is FAT");
 Serial.println(vol.fatType(), DEC);
 PgmPrint("blocksPerCluster: ");
 Serial.println(vol.blocksPerCluster(), DEC);
 PgmPrint("clusterCount: ");
 Serial.println(vol.clusterCount());
 PgmPrint("fatStartBlock: ");
 Serial.println(vol.fatStartBlock());
 PgmPrint("fatCount: ");
 Serial.println(vol.fatCount(), DEC);
 PgmPrint("blocksPerFat: ");
 Serial.println(vol.blocksPerFat());
 PgmPrint("rootDirStart: ");
 Serial.println(vol.rootDirStart());
 PgmPrint("dataStartBlock: ");
 Serial.println(vol.dataStartBlock());
 if (vol.dataStartBlock()%sectorSize) {
   PgmPrintln("Data area is not aligned on flash erase boundaries!");
 }
}

void setup() {
 Serial.begin(9600);
}

void loop() {
 PgmPrintln("\ntype any character to start");
 while (!Serial.available());
 Serial.flush();
 uint32_t t = millis();
 // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
 // breadboards.  use SPI_FULL_SPEED for better performance.

 // modifica per micro SD
 pinMode(10, OUTPUT);                       // set the SS pin as an output (necessary!)
 digitalWrite(10, HIGH);                    // but turn off the W5100 chip!
 uint8_t r = card.init(SPI_HALF_SPEED, 4);  // Use digital 4 as the SD SS line
 
 //uint8_t r = card.init(SPI_HALF_SPEED);

 t = millis() - t;
 if (!r) {
   PgmPrintln("\ncard.init failed");
   sdError();
   return;
 }
 PgmPrint("\ninit time: ");
 Serial.println(t);
 PgmPrint("\nCard type: ");
 switch(card.type()) {
   case SD_CARD_TYPE_SD1:
     PgmPrintln("SD1");
     break;
   case SD_CARD_TYPE_SD2:
     PgmPrintln("SD2");
     break;
   case SD_CARD_TYPE_SDHC:
     PgmPrintln("SDHC");
     break;
   default:
     PgmPrintln("Unknown");
 }
 if(!cidDmp()) return;
 if(!csdDmp()) return;
 if(!partDmp()) return;
 if (!vol.init(&card)) {
   PgmPrintln("\nvol.init failed");
   sdError();
   return;
 }
 volDmp();
}

Mauro_Titan


A colpo d'occhio mi sembra uno sketch per la vecchia versione della Ethernet shield, prova questo che è


Grazie 1000 Astribeed
ho scaricato la libreria e inserito il tuo rpogramma e in effetti funziona.
Ora cerco di capire bene tutto il listato.

Grazie ancora .. ciaoooo

Go Up