<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Arduino中国 &#187; SD卡</title>
	<atom:link href="http://blog.flamingoeda.com/tag/sd%e5%8d%a1/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flamingoeda.com</link>
	<description>Flamingo EDA</description>
	<lastBuildDate>Sat, 13 Aug 2011 11:41:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Arduino 电子积木 SD卡存储模块</title>
		<link>http://blog.flamingoeda.com/2009/12/25/arduino-%e7%94%b5%e5%ad%90%e7%a7%af%e6%9c%a8-sd%e5%8d%a1%e5%ad%98%e5%82%a8%e6%a8%a1%e5%9d%97/</link>
		<comments>http://blog.flamingoeda.com/2009/12/25/arduino-%e7%94%b5%e5%ad%90%e7%a7%af%e6%9c%a8-sd%e5%8d%a1%e5%ad%98%e5%82%a8%e6%a8%a1%e5%9d%97/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 16:08:30 +0000</pubDate>
		<dc:creator>flamingoeda</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[电子积木]]></category>
		<category><![CDATA[SD卡]]></category>

		<guid isPermaLink="false">http://blog.flamingoeda.com/?p=549</guid>
		<description><![CDATA[SD卡是我们经常使用到的一种存储设备，像数码相机，MP3，阅读器，GPS导航仪等都会使用到它。在使用Arduino的时候，如何我们要将一些数据记录下来，就可以使用到SD卡。不过要让SD卡能够同Arduino配合使用可不是一件简单的事情，首先在硬件上我们要解决其与Arduino连接的问题，其次软件上我们还要解决文件的存储问题，以便使用读卡器之样的设备能够很方便地将保存在SD卡中的数据读出。 针对硬件上的需求，我们开发了SD卡模块，它通过SPI接口与Arduino进行通信： 同时，为了方便同Arduino的SPI接口进行连接，提供了相应的IDC扩展板，该扩展板上有一个专门为SPI设计的六芯插座： 电路的连接上则依然沿用了电子积木所一向强调的即插即用方式，我们只需要用6芯的IDC连接线，将IDC扩展板上的SPI插座与SD卡模块上的相应插座连接起来，就完成了电路部分的连接： 回到软件部分，我们的目标是要让SD卡上记录的数据能够在电脑上顺利地读出，因此在向SD卡上记录相应数据的时候需要遵循一定的规定，即文件系统的类型。对于像Arduino这样的小系统来讲，相对简单的FAT16是一个比较不错的选择。 在这里我们要用到读卡器来对SD卡进行相应的处理，将其格式化成FAT文件系统： Arduino上使用的软件则是基于SparkFun提供的FAT库，但根据硬件差异做了一定的修改，你可以从这里下载到修改过的FAT库。该库需要被解压缩到Arduino的hardware\libraries子目录中。 测试时使用的时FAT库里自带的示例程序： //Include all the libraries necessary for FAT32 #include &#60;byteordering.h&#62; #include &#60;fat.h&#62; #include &#60;FAT16.h&#62; #include &#60;fat_config.h&#62; #include &#60;partition.h&#62; #include &#60;partition_config.h&#62; #include &#60;sd-reader_config.h&#62; #include &#60;sd_raw.h&#62; #include &#60;sd_raw_config.h&#62; FAT TestFile; //This will be the file we manipulate in the sketch char buffer[512]; //Data will be temporarily stored to this buffer [...]]]></description>
			<content:encoded><![CDATA[<p>SD卡是我们经常使用到的一种存储设备，像数码相机，MP3，阅读器，GPS导航仪等都会使用到它。在使用Arduino的时候，如何我们要将一些数据记录下来，就可以使用到SD卡。不过要让SD卡能够同Arduino配合使用可不是一件简单的事情，首先在硬件上我们要解决其与Arduino连接的问题，其次软件上我们还要解决文件的存储问题，以便使用读卡器之样的设备能够很方便地将保存在SD卡中的数据读出。</p>
<p>针对硬件上的需求，我们开发了SD卡模块，它通过SPI接口与Arduino进行通信：<br />
<img alt="" src="http://image.flamingoeda.com/albums/userpics/febb_sd_card.JPG" title="SD卡" class="aligncenter" width="460" height="345" /><br />
同时，为了方便同Arduino的SPI接口进行连接，提供了相应的IDC扩展板，该扩展板上有一个专门为SPI设计的六芯插座：<br />
<img alt="" src="http://image.flamingoeda.com/albums/userpics/arduino_shield_idc.JPG" title="SD卡" class="aligncenter" width="460" height="345" /><br />
电路的连接上则依然沿用了电子积木所一向强调的即插即用方式，我们只需要用6芯的IDC连接线，将IDC扩展板上的SPI插座与SD卡模块上的相应插座连接起来，就完成了电路部分的连接：<br />
<img alt="" src="http://image.flamingoeda.com/albums/userpics/febb_sd_card_1.JPG" title="SD卡" class="aligncenter" width="460" height="345" /></p>
<p>回到软件部分，我们的目标是要让SD卡上记录的数据能够在电脑上顺利地读出，因此在向SD卡上记录相应数据的时候需要遵循一定的规定，即文件系统的类型。对于像Arduino这样的小系统来讲，相对简单的FAT16是一个比较不错的选择。</p>
<p>在这里我们要用到读卡器来对SD卡进行相应的处理，将其格式化成FAT文件系统：</p>
<p><img alt="" src="http://image.flamingoeda.com/albums/userpics/febb_sd_card_2.JPG" title="SD卡" class="aligncenter" width="460" height="345" /><br />
<img alt="" src="http://image.flamingoeda.com/albums/userpics/febb_sd_card_3.JPG" title="SD卡" class="aligncenter" width="460" height="345" /><br />
<img alt="" src="http://image.flamingoeda.com/albums/userpics/febb_sd_card_4.JPG" title="SD卡" class="aligncenter" width="460" height="345" /></p>
<p>Arduino上使用的软件则是基于SparkFun提供的<a href="http://www.sparkfun.com/Code/FAT.zip">FAT库</a>，但根据硬件差异做了一定的修改，你可以从这里下载到<a href="http://blog.flamingoeda.com/wp-content/uploads/2009/12/FAT.zip">修改过的FAT库</a>。该库需要被解压缩到Arduino的hardware\libraries子目录中。</p>
<p>测试时使用的时FAT库里自带的示例程序：</p>
<pre>
<span style="color: #777755;">//Include all the libraries necessary for FAT32</span>
#include &lt;byteordering.h&gt;
#include &lt;fat.h&gt;
#include &lt;FAT16.h&gt;
#include &lt;fat_config.h&gt;
#include &lt;partition.h&gt;
#include &lt;partition_config.h&gt;
#include &lt;sd-reader_config.h&gt;
#include &lt;sd_raw.h&gt;
#include &lt;sd_raw_config.h&gt;

FAT TestFile;      <span style="color: #777755;">//This will be the file we manipulate in the sketch</span>
<span style="color: #996600;">char</span> buffer[512];  <span style="color: #777755;">//Data will be temporarily stored to this buffer before being written to the file</span>
<span style="color: #996600;">int</span> read_size=0;   <span style="color: #777755;">//Used as an indicator for how many characters are read from the file</span>
<span style="color: #996600;">int</span> count=0;       <span style="color: #777755;">//Miscellaneous variable</span>

<span style="color: #CC6600;">void</span> <span style="color: #993300;"><b>setup</b></span>()
{
  <span style="color: #996600;">Serial</span>.<span style="color: #996600;">begin</span>(9600);  <span style="color: #777755;">//Initiate serial communication at 9600 bps</span>

  TestFile.initialize();  <span style="color: #777755;">//Initialize the SD card and the FAT file system.</span>
  <span style="color: #996600;">Serial</span>.<span style="color: #996600;">println</span>(<span style="color: #CC0000;">"Starting..."</span>);
  TestFile.create_file(<span style="color: #CC0000;">"Sample.txt"</span>);  <span style="color: #777755;">//Create a file on the SD card named "Read_File_Test.txt"</span>
                                       <span style="color: #777755;">//NOTE: This function will return a 0 value if it was unable to create the file.</span>
  TestFile.open();  <span style="color: #777755;">//Now that the file has been created, open it so we can write to it.</span>
}

<span style="color: #CC6600;">void</span> <span style="color: #993300;"><b>loop</b></span>()
{
  TestFile.<span style="color: #996600;">write</span>(<span style="color: #CC0000;">"This is test data."</span>);  <span style="color: #777755;">//using the write function will always write to the beginning of the file. </span>
                                         <span style="color: #777755;">// Here we add some text to the file.</span>
  TestFile.close();  <span style="color: #777755;">//We are done writing to the file for now. Close it for later use.</span>

  <span style="color: #CC6600;">while</span>(1){
    TestFile.open();  <span style="color: #777755;">//Open the file. When the file is opened we will be looking at the beginning of the file.</span>
    read_size=TestFile.<span style="color: #996600;">read</span>(buffer); <span style="color: #777755;">//Read the contents of the file. This will only read the amount of data specified</span>
                                    <span style="color: #777755;">// by the size of 'buffer.'</span>

    <span style="color: #996600;">Serial</span>.<span style="color: #996600;">println</span>(read_size, <span style="color: #CC0000;">DEC</span>);  <span style="color: #777755;">//Print the number of characters read by the read function.</span>
    <span style="color: #CC6600;">for</span>(<span style="color: #996600;">int</span> i=0; i<read_size; i++)
    {
      <span style="color: #996600;">Serial</span>.<span style="color: #996600;">print</span>(buffer[i], <span style="color: #CC0000;">BYTE</span>);  <span style="color: #777755;">//Print out the contents of the buffer.</span>
    }
    <span style="color: #996600;">Serial</span>.<span style="color: #996600;">println</span>();

    sprintf(buffer, <span style="color: #CC0000;">"%d"</span>, count++); <span style="color: #777755;">//Now we'll use the buffer to write data back to the file. </span>
                                   <span style="color: #777755;">// Here's we'll only add one value to buffer, the 'count' variable. </span>
    TestFile.<span style="color: #996600;">write</span>(buffer);         <span style="color: #777755;">//Write the new buffer to the end of the file</span>
    TestFile.close();               <span style="color: #777755;">//Close the file for later use.</span>

    <span style="color: #996600;">delay</span>(1000);  <span style="color: #777755;">//Wait one second before repeating the loop.</span>
  }
}
</pre>
<p>这样Arduino在运行的时候，会不断地向SD卡模块上的Sample.txt文件中写入相应的数据，随后我们同样可以通过读卡器读出该文件中记录的内容。</p>
<p><img alt="" src="http://image.flamingoeda.com/albums/userpics/febb_sd_card_5.JPG" title="SD卡" class="aligncenter" width="460" height="345" /></p>
<p>需要注意的是，由于用来操作SD卡和文件系统的代码相对较多，目前该模块只能运行在带有ATmega328P的Arduino上面。另外，目前市场上可选的SD卡种类繁多，可能不是每种SD卡都能够被很好的兼容，目前我测试过通过的SD卡有：</p>
<ul>
<li>SanDisk 2GB SD2 Card</li>
<li>Apacer 60X 1GB SD Card</li>
</ul>
<p>测试没有通过的SD卡有：</p>
<ul>
<li>SanDisk 16MB SD Card</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.flamingoeda.com/2009/12/25/arduino-%e7%94%b5%e5%ad%90%e7%a7%af%e6%9c%a8-sd%e5%8d%a1%e5%ad%98%e5%82%a8%e6%a8%a1%e5%9d%97/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

