极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 22330|回复: 6

求助!apm2.5 mega2560连sd卡实验

[复制链接]
发表于 2013-4-1 00:28:48 | 显示全部楼层 |阅读模式
本帖最后由 慕翌光君 于 2013-4-1 14:51 编辑

使用的工具是Arduino IDE 1.0.4以及从http://code.google.com/p/sdfatlib/downloads/list下载的sdfatlib20130313.zip.
板子是APM2.5 MEGA 2560
实验是照着http://arduino.cc/forum/index.php/topic,65391.0.html Reply #7里写的来.
但还是失败了...

  1. // Quick hardware test
  2. #include <SdFat.h>
  3. // Test with reduced SPI speed for breadboards.
  4. // Change spiSpeed to SPI_FULL_SPEED for better performance
  5. // Use SPI_QUARTER_SPEED for even slower SPI bus speed
  6. const uint8_t spiSpeed = SPI_HALF_SPEED;
  7. //------------------------------------------------------------------------------
  8. // Normally SdFat is used in applications in place
  9. // of Sd2Card, SdVolume, and SdFile for root.
  10. Sd2Card card;
  11. SdVolume volume;
  12. SdFile root;

  13. // Serial streams
  14. ArduinoOutStream cout(Serial);

  15. // input buffer for line
  16. char cinBuf[40];
  17. ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));

  18. // SD card chip select
  19. int chipSelect;

  20. void cardOrSpeed() {
  21.   cout << pstr(
  22.     "Try another SD card or reduce the SPI bus speed.\n"
  23.     "The current SPI speed is: ");
  24.   uint8_t divisor = 1;
  25.   for (uint8_t i = 0; i < spiSpeed; i++) divisor *= 2;
  26.   cout << F_CPU * 0.5e-6 / divisor << pstr(" MHz\n");
  27.   cout << pstr("Edit spiSpeed in this sketch to change it.\n");
  28. }

  29. void reformatMsg() {
  30.   cout << pstr("Try reformatting the card.  For best results use\n");
  31.   cout << pstr("the SdFormatter sketch in SdFat/examples or download\n");
  32.   cout << pstr("and use SDFormatter from www.sdcard.org/consumer.\n");
  33. }

  34. void setup() {
  35.   Serial.begin(9600);
  36.   while (!Serial) {}  // wait for Leonardo
  37.   
  38.   cout << pstr(
  39.     "\nSD chip select is the key hardware option.\n"
  40.     "Common values are:\n"
  41.     "Arduino Ethernet shield, pin 4\n"
  42.     "Sparkfun SD shield, pin 8\n"
  43.     "Adafruit SD shields and modules, pin 10\n");
  44. }

  45. bool firstTry = true;
  46. void loop() {
  47.   // read any existing Serial data
  48.   while (Serial.read() >= 0) {}

  49.   if (!firstTry) cout << pstr("\nRestarting\n");
  50.   firstTry = false;

  51.   cout << pstr("\nEnter the chip select pin number: ");
  52.   cin.readline();
  53.   if (cin >> chipSelect) {
  54.     cout << chipSelect << endl;
  55.   } else {
  56.     cout << pstr("\nInvalid pin number\n");
  57.     return;
  58.   }
  59.   if (!card.init(spiSpeed, chipSelect)) {
  60.     cout << pstr(
  61.       "\nSD initialization failed.\n"
  62.       "Do not reformat the card!\n"
  63.       "Is the card correctly inserted?\n"
  64.       "Is chipSelect set to the correct value?\n"
  65.       "Is there a wiring/soldering problem?\n");
  66.     cout << pstr("errorCode: ") << hex << showbase << int(card.errorCode());
  67.     cout << pstr(", errorData: ") << int(card.errorData());
  68.     cout << dec << noshowbase << endl;
  69.     return;
  70.   }
  71.   cout << pstr("\nCard successfully initialized.\n");
  72.   cout << endl;

  73.   uint32_t size = card.cardSize();
  74.   if (size == 0) {
  75.     cout << pstr("Can't determine the card size.\n");
  76.     cardOrSpeed();
  77.     return;
  78.   }
  79.   uint32_t sizeMB = 0.000512 * size + 0.5;
  80.   cout << pstr("Card size: ") << sizeMB;
  81.   cout << pstr(" MB (MB = 1,000,000 bytes)\n");
  82.   cout << endl;

  83.   if (!volume.init(&card)) {
  84.     if (card.errorCode()) {
  85.       cout << pstr("Can't read the card.\n");
  86.       cardOrSpeed();
  87.     } else {
  88.       cout << pstr("Can't find a valid FAT16/FAT32 partition.\n");
  89.       reformatMsg();
  90.     }
  91.     return;
  92.   }
  93.   cout << pstr("Volume is FAT") << int(volume.fatType());
  94.   cout << pstr(", Cluster size (bytes): ") << 512L * volume.blocksPerCluster();
  95.   cout << endl << endl;

  96.   root.close();
  97.   if (!root.openRoot(&volume)) {
  98.     cout << pstr("Can't open root directory.\n");
  99.     reformatMsg();
  100.     return;
  101.   }
  102.   cout << pstr("Files found (name date time size):\n");
  103.   root.ls(LS_R | LS_DATE | LS_SIZE);

  104.   if ((sizeMB > 1100 && volume.blocksPerCluster() < 64)
  105.     || (sizeMB < 2200 && volume.fatType() == 32)) {
  106.     cout << pstr("\nThis card should be reformatted for best performance.\n");
  107.     cout << pstr("Use a cluster size of 32 KB for cards larger than 1 GB.\n");
  108.     cout << pstr("Only cards larger than 2 GB should be formatted FAT32.\n");
  109.     reformatMsg();
  110.     return;
  111.   }
  112.   // read any existing Serial data
  113.   while (Serial.read() >= 0) {}
  114.   cout << pstr("\nSuccess!  Type any character to restart.\n");
  115.   while (Serial.read() < 0) {}
  116. }
复制代码
Enter the chip select pin number: 12

SD initialization failed.
Do not reformat the card!
Is the card correctly inserted?
Is chipSelect set to the correct value?
Is there a wiring/soldering problem?
errorCode: 0x1, errorData: 0xff

跳线的连接
MISO -- PIN 50
MOSI -- PIN 51
SCK   -- PIN 52
CS     -- PIN12
并且pinMode(53, OUTPUT);
哪位大神帮帮忙~~~求指导!
回复

使用道具 举报

 楼主| 发表于 2013-4-1 14:56:11 | 显示全部楼层
代码已贴出~~~求指导~~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-1 19:16:12 | 显示全部楼层
这是APM2.5 Mega2560的原理图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-1 19:19:08 | 显示全部楼层
本帖最后由 慕翌光君 于 2013-4-2 22:18 编辑

连接图~~~~~

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-2 11:26:56 | 显示全部楼层
没有人用过APM2.5 Mega2560么?~~~~
回复 支持 反对

使用道具 举报

发表于 2013-4-10 17:08:33 | 显示全部楼层
有 2.5的 .sch吗?或者知道去哪里找吗?谢谢了!
回复 支持 反对

使用道具 举报

发表于 2013-4-10 17:12:45 | 显示全部楼层
Sd2Card card;
SdVolume volume;
SdFile root;
好像有问题啊!
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-4 21:32 , Processed in 0.042167 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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