风筝
发表于: 2018-5-15 11:29:41 | 显示全部楼层

在一些Arduino应用程序中,能够实现在本地存储和检索信息是非常有优势的。您可以使用一张Secure Digital或SD卡实现此功能。 SD卡是一种非易失性存储卡,广泛应用于手机、数码相机、GPS导航设备、手持控制台和平板电脑等便携式设备中。另一种类型的SD卡是Micro SD卡。尺寸仅为15毫米x 11毫米x 1毫米,是最小的存储卡。它大小约为普通尺寸的SD卡的四分之一,大约为一个指甲的大小。

SD1.jpg


为了将Micro SD卡连接到Arduino Mega开发板,我们使用带有Micro SD卡插槽的以太网扩展板。另外,市场上有许多其他的扩展板可用于不同类型的SD卡。

sd_card_pinouts.jpg


如上图所示,一张micro SD卡有8个引脚。下表描述了每个引脚的功能。

引脚序号
名称
功能说明
1
NC
未连接
2
CS
片选/从选择(SS)
3
DI
主输出/从输入(MOSI)
4
VDD
电源电压
5
CLK
时钟(SCK)
6
VSS
电源电压地
7
DO
主输入/从输出(MISO)
8
RSV
保留

如果您要自己尝试连接到此SD卡,则必须确保将SD卡的引脚连接到Arduino的相应的引脚。由于我们使用的是商用的扩展板,所以这并不是个问题。我们所需要做的就是将Arduino的默认CS(片选)引脚声明为OUTPUT。它位于Arduino MEGA开发板上的第53引脚。在以太网扩展板上,CS引脚是第4引脚。您需要在代码中指定该引脚才能使SD卡正常工作。


实验1

在这个实验中,我们将学习如何从SD卡上读取文件。


所需的硬件材料

●    1张Micro SD卡

●    1个以太网扩展模块

●    1个Arduino Mega2560开发板

SD3.jpg


代码

要从SD卡上读取文件,我们需要使用SD.h库。以下代码假定文件“ourfile.txt”已经写入到SD卡中。

  1. #include “SD.h”

  2. const int cs = 4;

  3. void setup()
  4. {
  5.   Serial.begin(9600);
  6.   



  7.   Serial.print("Initializing card...");
  8.   // make sure that the default chip select pin is declared OUTPUT
  9.   
  10.   pinMode(53, OUTPUT);
  11.   
  12.   // see if the card is present
  13.   if (!SD.begin(cs))
  14.   {
  15.     Serial.println("Card failed to initialize, or not present");
  16.   
  17.     return;
  18.   }
  19.   Serial.println("card initialized.");
  20.   
  21.   // open the file named ourfile.txt
  22.   
  23.   File myfile = SD.open("ourfile.txt");

  24.   // if the file is available, read the file
  25.   if (myfile)
  26.   {
  27.     while (myfile.available())
  28.     {
  29.       Serial.write(myfile.read());
  30.     }
  31.     myfile.close();
  32.   }  
  33.   // if the file cannot be opened give error report
  34.   else {
  35.     Serial.println("error opening the text file");
  36.   }
  37. }

  38. void loop()
  39. {
  40. }
复制代码


实验2

在这个实验中,我们将学习如何在SD卡中创建一个文件,写入数据,然后从SD卡中读取数据。


所需的硬件

我们将使用与之前实验相同的硬件


代码

要将文件写入SD卡并读取该文件,我们需要再次使用SD.h库。

  1. #include “SD.h”

  2. File myfile;

  3. void setup()
  4. {

  5.   Serial.begin(9600);
  6.   
  7.   Serial.print("Initializing card...");
  8.   
  9.   // declare default CS pin as OUTPUT
  10.    pinMode(53, OUTPUT);
  11.    
  12.   if (!SD.begin(4)) {
  13.     Serial.println("initialization of the SD card failed!");
  14.     return;
  15.   }
  16.   Serial.println("initialization of the SDcard is done.");
  17.   

  18.   myfile = SD.open("textFile.txt", FILE_WRITE);
  19.   
  20.   
  21.   if (myfile)
  22.   {
  23.     Serial.print("Writing to the text file...");
  24.     myfile.println("Congratulations! You have successfully wrote on the text file.");
  25.         
  26.     myfile.close(); // close the file:
  27.     Serial.println("done closing.");
  28.   } else
  29.   {
  30.     // if the file didn't open, report an error:
  31.     Serial.println("error opening the text file!");
  32.   }
  33.   
  34.   // re-open the text file for reading:
  35.   myfile = SD.open("textFile.txt");
  36.   if (myfile)
  37.   {
  38.     Serial.println("textFile.txt:");
  39.    
  40.     // read all the text written on the file
  41.     while (myfile.available())
  42.     {
  43.             Serial.write(myfile.read());
  44.     }
  45.     // close the file:
  46.     myfile.close();
  47.   } else
  48.   {
  49.           // if the file didn't open, report an error:
  50.     Serial.println("error opening the text file!");
  51.   }
  52. }

  53. void loop()
  54. {
  55. }
复制代码
跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-26 15:47 , Processed in 0.040446 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

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