极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 23139|回复: 10

关于W5100读取SD卡问题

[复制链接]
发表于 2012-8-25 23:56:56 | 显示全部楼层 |阅读模式
本帖最后由 greenfort 于 2012-8-26 00:00 编辑

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

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

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

  25. #include <SdFat.h>
  26. #include <SdFatUtil.h>
  27. #include <Ethernet.h>
  28. #include <SPI.h>

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

  33. /************ SDCARD STUFF ************/
  34. Sd2Card card;
  35. SdVolume volume;
  36. SdFile root;
  37. SdFile file;

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

  40. void error_P(const char* str) {
  41.   PgmPrint("error: ");
  42.   SerialPrintln_P(str);
  43.   if (card.errorCode()) {
  44.     PgmPrint("SD error: ");
  45.     Serial.print(card.errorCode(), HEX);
  46.     Serial.print(',');
  47.     Serial.println(card.errorData(), HEX);
  48.   }
  49.   while(1);
  50. }

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

  53.   PgmPrint("Free RAM: ");
  54.   Serial.println(FreeRam());  
  55.   
  56.   // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  57.   // breadboards.  use SPI_FULL_SPEED for better performance.
  58.   pinMode(10, OUTPUT);                       // set the SS pin as an output (necessary!)
  59.   digitalWrite(10, HIGH);                    // but turn off the W5100 chip!

  60.   if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
  61.   
  62.   // initialize a FAT volume
  63.   if (!volume.init(&card)) error("vol.init failed!");

  64.   PgmPrint("Volume is FAT");
  65.   Serial.println(volume.fatType(),DEC);
  66.   Serial.println();
  67.   
  68.   if (!root.openRoot(&volume)) error("openRoot failed");

  69.   // list file in root with date and size
  70.   PgmPrintln("Files found in root:");
  71.   root.ls(LS_DATE | LS_SIZE);
  72.   Serial.println();
  73.   
  74.   // Recursive list of all directories
  75.   PgmPrintln("Files found in all dirs:");
  76.   root.ls(LS_R);
  77.   
  78.   Serial.println();
  79.   PgmPrintln("Done");
  80.   
  81.   // Debugging complete, we start the server!
  82.   Ethernet.begin(mac, ip);
  83.   server.begin();
  84. }

  85. void ListFiles(EthernetClient client, uint8_t flags) {
  86.   // This code is just copied from SdFile.cpp in the SDFat library
  87.   // and tweaked to print to the client output in html!
  88.   dir_t p;
  89.   
  90.   root.rewind();
  91.   client.println("<ul>");
  92.   while (root.readDir(p) > 0) {
  93.     // done if past last used entry
  94.     if (p.name[0] == DIR_NAME_FREE) break;

  95.     // skip deleted entry and entries for . and  ..
  96.     if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;

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

  99.     // print any indent spaces
  100.     client.print("<li><a href="");
  101.     for (uint8_t i = 0; i < 11; i++) {
  102.       if (p.name[i] == ' ') continue;
  103.       if (i == 8) {
  104.         client.print('.');
  105.       }
  106.       client.print((char)p.name[i]);
  107.     }
  108.     client.print("">");
  109.    
  110.     // print file name with possible blank fill
  111.     for (uint8_t i = 0; i < 11; i++) {
  112.       if (p.name[i] == ' ') continue;
  113.       if (i == 8) {
  114.         client.print('.');
  115.       }
  116.       client.print((char)p.name[i]);
  117.     }
  118.    
  119.     client.print("</a>");
  120.    
  121.     if (DIR_IS_SUBDIR(&p)) {
  122.       client.print('/');
  123.     }

  124.     // print modify date/time if requested
  125.     if (flags & LS_DATE) {
  126.        root.printFatDate(p.lastWriteDate);
  127.        client.print(' ');
  128.        root.printFatTime(p.lastWriteTime);
  129.     }
  130.     // print size if requested
  131.     if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
  132.       client.print(' ');
  133.       client.print(p.fileSize);
  134.     }
  135.     client.println("</li>");
  136.   }
  137.   client.println("</ul>");
  138. }

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

  141. void loop()
  142. {
  143.   char clientline[BUFSIZ];
  144.   int index = 0;
  145.   
  146.   EthernetClient client = server.available();
  147.   if (client) {
  148.     // an http request ends with a blank line
  149.     boolean current_line_is_blank = true;
  150.    
  151.     // reset the input buffer
  152.     index = 0;
  153.    
  154.     while (client.connected()) {
  155.       if (client.available()) {
  156.         char c = client.read();
  157.         
  158.         // If it isn't a new line, add the character to the buffer
  159.         if (c != '\n' && c != '\r') {
  160.           clientline[index] = c;
  161.           index++;
  162.           // are we too big for the buffer? start tossing out data
  163.           if (index >= BUFSIZ)
  164.             index = BUFSIZ -1;
  165.          
  166.           // continue to read more data!
  167.           continue;
  168.         }
  169.         
  170.         // got a \n or \r new line, which means the string is done
  171.         clientline[index] = 0;
  172.         
  173.         // Print it out for debugging
  174.         Serial.println(clientline);
  175.         
  176.         // Look for substring such as a request to get the root file
  177.         if (strstr(clientline, "GET / ") != 0) {
  178.           // send a standard http response header
  179.           client.println("HTTP/1.1 200 OK");
  180.           client.println("Content-Type: text/html");
  181.           client.println();
  182.          
  183.           // print all the files, use a helper to keep it clean
  184.           client.println("<h2>Files:</h2>");
  185.           ListFiles(client, LS_SIZE);
  186.         } else if (strstr(clientline, "GET /") != 0) {
  187.           // this time no space after the /, so a sub-file!
  188.           char *filename;
  189.          
  190.           filename = clientline + 5; // look after the "GET /" (5 chars)
  191.           // a little trick, look for the " HTTP/1.1" string and
  192.           // turn the first character of the substring into a 0 to clear it out.
  193.           (strstr(clientline, " HTTP"))[0] = 0;
  194.          
  195.           // print the file we want
  196.           Serial.println(filename);

  197.           if (! file.open(&root, filename, O_READ)) {
  198.             client.println("HTTP/1.1 404 Not Found");
  199.             client.println("Content-Type: text/html");
  200.             client.println();
  201.             client.println("<h2>File Not Found!</h2>");
  202.             break;
  203.           }
  204.          
  205.           Serial.println("Opened!");
  206.                     
  207.           client.println("HTTP/1.1 200 OK");
  208.           client.println("Content-Type: text/plain");
  209.           client.println();
  210.          
  211.           int16_t c;
  212.           while ((c = file.read()) > 0) {
  213.               // uncomment the serial to debug (slow!)
  214.               //Serial.print((char)c);
  215.               client.print((char)c);
  216.           }
  217.           file.close();
  218.         } else {
  219.           // everything else is a 404
  220.           client.println("HTTP/1.1 404 Not Found");
  221.           client.println("Content-Type: text/html");
  222.           client.println();
  223.           client.println("<h2>File Not Found!</h2>");
  224.         }
  225.         break;
  226.       }
  227.     }
  228.     // give the web browser time to receive the data
  229.     delay(1);
  230.     client.stop();
  231.   }
  232. }
复制代码
Web页面打不开,串口提示错误如下


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

使用道具 举报

发表于 2012-8-26 08:23:20 | 显示全部楼层
同问,arduino接W5100后能否下载文件到SD卡?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-8-26 21:57:10 | 显示全部楼层
这个问题没人知道吗?
回复 支持 反对

使用道具 举报

发表于 2012-8-27 14:46:25 | 显示全部楼层
w5100  SD 和 网络不能同时用。
回复 支持 反对

使用道具 举报

发表于 2012-8-27 18:01:08 | 显示全部楼层
W5100和SD当然可以同时用,通过SS可以控制,请搜索坛子里的相关帖子和代码。(用w5100+sd做一个小的webserver,访问sd上的文件系统)。
回复 支持 反对

使用道具 举报

 楼主| 发表于 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

依然无效
回复 支持 反对

使用道具 举报

发表于 2012-8-28 09:21:57 | 显示全部楼层
这款扩展板并非所有的卡都支持,哪怕外观完全一样,我自己买了一块就不能用,最后把手机里的卡拔下来反而能认。建议你可以换卡试试。
回复 支持 反对

使用道具 举报

发表于 2012-9-11 21:52:36 | 显示全部楼层
这是个不错的问题
回复 支持 反对

使用道具 举报

发表于 2012-9-11 22:20:53 | 显示全部楼层
arduino读写sd卡需要先将网络部分关闭,然后读写完SD卡再打开网络部分, 具体可以参考论坛“沧海笑”大神的帖子。。。。额虽然我忘了叫啥名字,,,,,搜索看看呗
回复 支持 反对

使用道具 举报

发表于 2012-12-26 11:02:31 | 显示全部楼层
请问楼主的这个库在哪里下载的,我下载了一个但是都是c++的
回复 支持 反对

使用道具 举报

发表于 2013-3-14 11:20:56 | 显示全部楼层
“沧海笑”大神的帖子是指这个吗?

关于W5100+SD的冲突及解决 - Powered by Discuz!
http://www.geek-workshop.com/thread-694-1-1.html
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-29 14:29 , Processed in 0.055088 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表