greenfort 发表于 2012-8-25 23:56:56

关于W5100读取SD卡问题

本帖最后由 greenfort 于 2012-8-26 00:00 编辑

新入手一个W5100网络扩展卡,带有TF卡槽,但是发现无法使用系统库中的SD卡示例读写TF卡,请问哪位有示例程序,给一个我测试一下?
也试过Web读写SD卡的那个程序,无法工作,是不是板子是坏的

测试用的程序如下/*
* USERS OF ARDUINO 0023 AND EARLIER: use the 'SDWebBrowse.pde' sketch...
* 'SDWebBrowse.ino' can be ignored.
* USERS OF ARDUINO 1.0 AND LATER: **DELETE** the 'SDWebBrowse.pde' sketch
* and use ONLY the 'SDWebBrowse.ino' file.By default, BOTH files will
* load when using the Sketchbook menu, and the .pde version will cause
* compiler errors in 1.0.Delete the .pde, then load the sketch.
*
* I can't explain WHY this is necessary, but something among the various
* libraries here appears to be wreaking inexplicable havoc with the
* 'ARDUINO' definition, making the usual version test unusable (BOTH
* cases evaluate as true).FML.
*/

/*
* This sketch uses the microSD card slot on the Arduino Ethernet shield
* to serve up files over a very minimal browsing interface
*
* Some code is from Bill Greiman's SdFatLib examples, some is from the
* Arduino Ethernet WebServer example and the rest is from Limor Fried
* (Adafruit) so its probably under GPL
*
* Tutorial is at http://www.ladyada.net/learn/arduino/ethfiles.html
* Pull requests should go to http://github.com/adafruit/SDWebBrowse
*/

#include <SdFat.h>
#include <SdFatUtil.h>
#include <Ethernet.h>
#include <SPI.h>

/************ ETHERNET STUFF ************/
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 11 };
EthernetServer server(80);

/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))

void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
}
while(1);
}

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

PgmPrint("Free RAM: ");
Serial.println(FreeRam());

// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards.use SPI_FULL_SPEED for better performance.
pinMode(10, OUTPUT);                     // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH);                  // but turn off the W5100 chip!

if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");

// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");

PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();

if (!root.openRoot(&volume)) error("openRoot failed");

// list file in root with date and size
PgmPrintln("Files found in root:");
root.ls(LS_DATE | LS_SIZE);
Serial.println();

// Recursive list of all directories
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);

Serial.println();
PgmPrintln("Done");

// Debugging complete, we start the server!
Ethernet.begin(mac, ip);
server.begin();
}

void ListFiles(EthernetClient client, uint8_t flags) {
// This code is just copied from SdFile.cpp in the SDFat library
// and tweaked to print to the client output in html!
dir_t p;

root.rewind();
client.println("<ul>");
while (root.readDir(p) > 0) {
    // done if past last used entry
    if (p.name == DIR_NAME_FREE) break;

    // skip deleted entry and entries for . and..
    if (p.name == DIR_NAME_DELETED || p.name == '.') continue;

    // only list subdirectories and files
    if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;

    // print any indent spaces
    client.print("<li><a href=\"");
    for (uint8_t i = 0; i < 11; i++) {
      if (p.name == ' ') continue;
      if (i == 8) {
      client.print('.');
      }
      client.print((char)p.name);
    }
    client.print("\">");
   
    // print file name with possible blank fill
    for (uint8_t i = 0; i < 11; i++) {
      if (p.name == ' ') continue;
      if (i == 8) {
      client.print('.');
      }
      client.print((char)p.name);
    }
   
    client.print("</a>");
   
    if (DIR_IS_SUBDIR(&p)) {
      client.print('/');
    }

    // print modify date/time if requested
    if (flags & LS_DATE) {
       root.printFatDate(p.lastWriteDate);
       client.print(' ');
       root.printFatTime(p.lastWriteTime);
    }
    // print size if requested
    if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
      client.print(' ');
      client.print(p.fileSize);
    }
    client.println("</li>");
}
client.println("</ul>");
}

// How big our line buffer should be. 100 is plenty!
#define BUFSIZ 100

void loop()
{
char clientline;
int index = 0;

EthernetClient client = server.available();
if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
   
    // reset the input buffer
    index = 0;
   
    while (client.connected()) {
      if (client.available()) {
      char c = client.read();
      
      // If it isn't a new line, add the character to the buffer
      if (c != '\n' && c != '\r') {
          clientline = c;
          index++;
          // are we too big for the buffer? start tossing out data
          if (index >= BUFSIZ)
            index = BUFSIZ -1;
         
          // continue to read more data!
          continue;
      }
      
      // got a \n or \r new line, which means the string is done
      clientline = 0;
      
      // Print it out for debugging
      Serial.println(clientline);
      
      // Look for substring such as a request to get the root file
      if (strstr(clientline, "GET / ") != 0) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
         
          // print all the files, use a helper to keep it clean
          client.println("<h2>Files:</h2>");
          ListFiles(client, LS_SIZE);
      } else if (strstr(clientline, "GET /") != 0) {
          // this time no space after the /, so a sub-file!
          char *filename;
         
          filename = clientline + 5; // look after the "GET /" (5 chars)
          // a little trick, look for the " HTTP/1.1" string and
          // turn the first character of the substring into a 0 to clear it out.
          (strstr(clientline, " HTTP")) = 0;
         
          // print the file we want
          Serial.println(filename);

          if (! file.open(&root, filename, O_READ)) {
            client.println("HTTP/1.1 404 Not Found");
            client.println("Content-Type: text/html");
            client.println();
            client.println("<h2>File Not Found!</h2>");
            break;
          }
         
          Serial.println("Opened!");
                  
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/plain");
          client.println();
         
          int16_t c;
          while ((c = file.read()) > 0) {
            // uncomment the serial to debug (slow!)
            //Serial.print((char)c);
            client.print((char)c);
          }
          file.close();
      } else {
          // everything else is a 404
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>File Not Found!</h2>");
      }
      break;
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
}
}Web页面打不开,串口提示错误如下


Free RAM: 810
error: card.init failed!
SD error: 1,0

Muller_r 发表于 2012-8-26 08:23:20

同问,arduino接W5100后能否下载文件到SD卡?

greenfort 发表于 2012-8-26 21:57:10

这个问题没人知道吗?

Chenmin 发表于 2012-8-27 14:46:25

:'( w5100SD 和 网络不能同时用。

沧海笑1122 发表于 2012-8-27 18:01:08

W5100和SD当然可以同时用,通过SS可以控制,请搜索坛子里的相关帖子和代码。(用w5100+sd做一个小的webserver,访问sd上的文件系统)。

greenfort 发表于 2012-8-27 19:44:17

本帖最后由 greenfort 于 2012-8-27 23:07 编辑

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

依然无效

沧海笑1122 发表于 2012-8-28 09:21:57

这款扩展板并非所有的卡都支持,哪怕外观完全一样,我自己买了一块就不能用,最后把手机里的卡拔下来反而能认。建议你可以换卡试试。

GeekNapster 发表于 2012-9-11 21:52:36

这是个不错的问题:L

迷你强 发表于 2012-9-11 22:20:53

arduino读写sd卡需要先将网络部分关闭,然后读写完SD卡再打开网络部分, 具体可以参考论坛“沧海笑”大神的帖子。。。。额虽然我忘了叫啥名字,,,,,搜索看看呗

电源插头 发表于 2012-12-26 11:02:31

请问楼主的这个库在哪里下载的,我下载了一个但是都是c++的

瘦网虫 发表于 2013-3-14 11:20:56

“沧海笑”大神的帖子是指这个吗?:P

关于W5100+SD的冲突及解决 - Powered by Discuz!
http://www.geek-workshop.com/thread-694-1-1.html
页: [1]
查看完整版本: 关于W5100读取SD卡问题