PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : ArduinoMega2560 und SD-Card Modul bringen mich zur Weissglut... ;)



oderlachs
15.03.2015, 09:51
Hallo Freunde !

Bestimmt hat wer von Euch auch schon von diesem Problem gehört, vielleicht selber gehabt und es gelöst. Ich hatte mich im Web glaube ich ausreichend belesen. So habe ich das Modul bei Ebay erstanden (http://www.ebay.de/itm/SD-Card-Memory-Modul-Karte-slot-Kartenadapter-cardreader-Arduino-/221702873528)

Ageschlossen am Arduino ist es über einen Sparkfun 4Ch-PegelShifter BOB 12009.

Die Pins sind wie folgt beschaltet



SD Arduino Mega
CS = 53
SCK = 52
MOSI = 51
MISO = 50


Ich habe zwei Module getestet und auch mehrere SD Karten, aber nur Fehlermeldungen, das die SD Card nicht Initialisiert werden kann...

Hat wer noch nen Tip für mich ??

Ich werde die Module nochmals am Arduino UNO testen, denn da ging es ja ...komisch :(

Gruss und Danke

Gerhard

Nachtrag :



Arduino UNO

Initializing SD card...Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 1977286656
Volume size (Kbytes): 1930944
Volume size (Mbytes): 1885

Files found on the card (name, date and size in bytes):
LOGGER00.CSV 2000-01-01 01:00:00 1293
APPEND.TXT 2000-01-01 01:00:00 341100
LOGGER01.CSV 2000-01-01 01:00:00 147
LOGGER02.CSV 2000-01-01 01:00:00 1359
LOGGER03.CSV 2000-01-01 01:00:00 18
LOGGER04.CSV 2000-01-01 01:00:00 169
LOGGER05.CSV 2000-01-01 01:00:00 71
LOGGER06.CSV 2000-01-01 01:00:00 18
LOGGER07.CSV 2000-01-01 01:00:00 578
LOGGER08.CSV 2000-01-01 01:00:00 45
LOGGER09.CSV 2000-01-01 01:00:00 4763


ArduinoMega:

Initializing SD card...initialization failed. Things to check:
* is a card is inserted?
* Is your wiring correct?
* did you change the chipSelect pin to match your shield or module?



Hier der geänderte Source von Arduino Beispielen (Cardinfo)


/*
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 51 ArduMega
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila 50 ArduMega
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila 52 ArduMega
** CS - depends on your SD card shield or module. 53 ArduMega
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>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile 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 = 53;

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("\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
digitalWrite(53,HIGH);

// 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);
}


void loop(void) {

}

HaWe
15.03.2015, 18:29
hallo,
auch ich hatte Probleme mit einigen dieser oder ähnlicher Module.
Es gibt allerdings unterschiedliche Hardware-Ausführungen.

Bislang gab es Probleme mit AVR (Megas), mit ARM (Due) funktionierte es immer. Ich habe immer die SPI-Header von Mega und Due verwendet, Uno habe ich nicht getestet.

Woran es früher gelegen haben mag, weiß ich nicht.

Ich habe jetzt auf Sketch 1.6.1 aktualisiert, dabei wurden auch die AVR- und ARM-Treiber aktualisiert - es funktioniert nun.

Hier ist der Testcode, der auch gleich andere Funktionen testet - er wartet auf eine korrekt eingesetzte SD-Karte, man kann auch eine bereits eingesetzte kurz rausnehmen und wieder einsetzen.
Bislang aber keine Probleme mehr.

ps,
edit:
ich habe die SD-Karten nun auch wieder über Windows formatiert, obwohl teilw. etwas anderes empfohlen wird - ich habe den Eindruck, auch deswegen könnte es jetzt zuverlässiger arbeiten.
Getestet habe ich eine 1 GB SD mit FAT- und eine 16GB SD mit FAT32-Formatierung: beide OK.
FATex funktionert NICHT.



/*
SD card: fprintf_() und fgets_()
ver 1.01
*/
#include <stdarg.h>
#include <stdio.h>
#include <SPI.h>
#include <SD.h>


// SD Card
#define SD_CSpin 4
File myFile;
char fname[64];

char sdata[128];
char sbuf[128];



//************************************************** *****************************************
char * fgets_ ( char * str, int32_t num, File * stream ) {
int32_t i = 0;
int16_t chr;

strcpy(str, "");
while (i < (num - 1)) { // got room for newline and terminating null?
chr = stream->read();
if (chr < 0) // end of file
break;
str[i++] = chr;
if ('\n' == chr) // end of line
break;
}

if (i) { // room in buffer for terminating null
str[i] = 0;
return str;
}
else
return NULL; // buffer too small or immediate end of file
// { strcpy(str, ""); return str; } // alternative
}
//************************************************** *****************************************



//************************************************** *****************************************
int32_t fprintf_ ( File * stream, const char fmtstr[], ... ) {
char str[1024];
va_list arguments;
int16_t num;

va_start( arguments, fmtstr);
num = vsnprintf(str, sizeof str, fmtstr, arguments);
// #debug
// Serial.println( "string to write to file="); Serial.println( str);
stream->print(str);
va_end( arguments );

return num;
}
//************************************************** *****************************************


void setup()
{
int16_t p, i, cnt;
float x;
char sval[20];
int16_t ival;
double fval;

pinMode(SD_CSpin, OUTPUT);
Serial.begin(9600);

sprintf(sbuf,"#: SD Initializing... ");
Serial.println(sbuf);

while(!SD.begin(SD_CSpin) ) {
sprintf(sbuf,"#: ...SD init failed ");
Serial.println(sbuf);
delay(1000);
}

sprintf(sbuf,"#: ...SD OK ! ");
Serial.println(sbuf);
strcpy(fname,"test.txt");

if (SD.exists(fname) ) {
sprintf(sbuf,"#: %s exists ",fname);
Serial.println(sbuf);

sprintf(sbuf,"#: Removing %s ",fname);
Serial.println(sbuf);

SD.remove("test.txt");
// removed: success ?
if (SD.exists(fname) ) {
sprintf(sbuf,"#: %s exists ",fname);
Serial.println(sbuf);
}
else {
sprintf(sbuf,"#: %s N/A ",fname);
Serial.println(sbuf);
}
}


// 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(fname, FILE_WRITE);

if (myFile) {
// if the file opened okay, write to it, then close file:
sprintf(sbuf,"#: Writing strings to %s ",fname);
Serial.println(sbuf);

//---------------------------------------------------------------------------------
// write data to file
fprintf_(&myFile, "%s\n%d\n%d\n%f\n%f\n", "Teststring", 1, 2, PI, 4.567890);
//---------------------------------------------------------------------------------

// close the file:
myFile.close();
sprintf(sbuf,"#: %s closed. ",fname);
Serial.println(sbuf);
}
else {
// if the file didn't open, print an error:
sprintf(sbuf,"#: error opening %s ",fname);
Serial.println(sbuf);
}

Serial.println();
// re-open the file for reading:
myFile = SD.open(fname);
if (myFile) {
sprintf(sbuf,"#: reading %s ",fname);
Serial.println(sbuf);

// read from the file until there's nothing else in it:
i=0;
cnt=1;

while (myFile.available()) {
strcpy(sdata, "");
fgets_ ( sdata, 20, &myFile );
Serial.print(cnt); Serial.print(": string raw="); Serial.println(sdata);
Serial.println("rueckformatiert:");
if (cnt==1) {Serial.print("str ="); Serial.println(sdata); }
if (cnt==2) {Serial.print("int ="); Serial.println(atoi(sdata) ); }
if (cnt==3) {Serial.print("int ="); Serial.println(atoi(sdata) ); }
if (cnt==4) {Serial.print("float="); Serial.println(atof(sdata) ); }
if (cnt==5) {Serial.print("float="); Serial.println(atof(sdata) ); }
++cnt;
}

// close the file:
myFile.close();
sprintf(sbuf,"#: %s closed. ",fname);
Serial.println(sbuf);
} else {
// if the file didn't open, print an error:
sprintf(sbuf,"#: error opening %s ",fname);
Serial.println(sbuf);

}
}

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

oderlachs
16.03.2015, 08:53
Danke HaWe !!

Zu Pindaten zum ArduinoMega sind selbst auf arduino.cc sehr widersprüchlich...also Würfeln was richtig ist ;) ...und richtig ist jedenfalls der ISP Header !!

Gruss
Gerhard

HaWe
16.03.2015, 09:14
funktioniert es denn jetzt?

BTW, ich verwende keine Levelshifter, meine Module sehen aus wie deine, sind aber 3.3 und 5V kompatibel.
Allerdings hat der SPI-Header immer 5V, auch auf dem Due.

ps,
Pinouts sind hier auf dieser Seite, habe ich gerade gegoogelt:
http://forum.arduino.cc/index.php?topic=146511.0

die Pinnummern sind forlgendermassen zugeordnet:



// SPI header |--|
// SPI pins Due -----------| |------------
//#define MISO 74 // | RES 76_SCK 74_MISO |
//#define MOSI 75 // | -GND 75_MOSI Vc+5V |
//#define SCK 76 // ---------------------------

// SPI pins Mega
//#define MISO 50
//#define MOSI 51
//#define SCK 52

//#define SS 53

oderlachs
17.03.2015, 08:17
Danke HaWe !!
es läuft und läuft jetzt...warum ????? ;) Das mit den Widersprüchen bei den Pinangaben in Sachen IRQs habe ich lösen können...mit Probieren , nicht nach den Angaben auf dem Pinout :mad:;)

Nun ja ich bin erst mal ein grosses stück weitergekommen. Das war für mich am Wichtigsten... :)

Vielen Dank allen für Ihre Hilfe !!!

Gruss Gerhard

Sisor
17.03.2015, 08:24
Was sind denn deine durch Probieren herausgefundenen Pin-Zuordunungen?

oderlachs
17.03.2015, 12:56
Muss ich mal Raussuchen..in Verbindung mit den verschiedenen Angaben macht es erst Sinn das hier aufzulisten.

Das wäre dann was für einen neuen Beitrag..würde hier nicht so ganz in de Ramen passen..hatte das auch nur nebenbei erwähnt.

Gerhard