跳转到主要内容
selina 提交于

<p><br />
TF卡读写储存模块是互动媒体设备最常用存储模块,采用SPI接口。模块可以通过杜邦线与Arduino传感器扩展板连接,编写相应的程序可以实现各种传感器(如温湿度传感器、光线传感器,GPS 等等)数据记录等功能,通过读卡器将TF卡数据读出,便可轻松加以分析利用,模块并带有两个LED灯以指示TF卡工作状态。(指示灯说明:1、当接上电源未插卡时,一个LED常亮,表示模块供电正常。2、插入卡后常亮的LED开始闪速,表示TF卡已经插入。3、另一个LED闪烁是表示正在通信。) </p>
<p>在配合Arduino101的使用中接线方式可以与uno相同:</p>
<p>1、CD:用户可用来检测卡是否插入,不使用可不连接(例程中不使用)</p>
<p>2、CS:TF卡片选,例程中需连接到4号脚(在没有使用其他SPI设备时可不连接)</p>
<p>3、MOSI:连接到arduino的MOSI口,在101上为11号引脚</p>
<p>4、MISO:连接到arduino的MISO口,在101上为12号引脚</p>
<p>5、SCK:连接到arduino的SCLK口,在101上为13号引脚</p>
<p>6、VCC:电源供电正端,连接到5V</p>
<p>7、GND:电源供电负端,连接到电源负极,GND</p>
<center>
<img alt="" src="http://intel.eetrend.com/files/2016-06/wen_zhang_/100001798-5051-010.pn…; />
</center>
<p><br />
硬件连接好以后,打开Arduino IDE找到文件-示例-SD-Cardlnfo。 </p>
<center>
<img alt="" src="http://intel.eetrend.com/files/2016-06/wen_zhang_/100001798-5052-020.pn…; />
</center>
<p>/*<br />
SD card test</p>
<p> This example shows how use the utility libraries on which the'<br />
SD library is based in order to get info about your SD card.<br />
Very useful for testing a card when you're not sure whether its working or not.</p>
<p> The circuit:<br />
* SD card attached to SPI bus as follows:<br />
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila<br />
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila<br />
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila<br />
** CS - depends on your SD card shield or module.<br />
Pin 4 used here for consistency with other Arduino examples</p>
<p> created 28 Mar 2011<br />
by Limor Fried<br />
modified 9 Apr 2012<br />
by Tom Igoe<br />
*/<br />
// include the SD library:<br />
#include <br />
#include </p>
<p>// set up variables using the SD utility library functions:<br />
Sd2Card card;<br />
SdVolume volume;<br />
SdFile root;</p>
<p>// change this to match your SD shield or module;<br />
// Arduino Ethernet shield: pin 4<br />
// Adafruit SD shields and modules: pin 10<br />
// Sparkfun SD shield: pin 8<br />
const int chipSelect = 4;</p>
<p>void setup() {<br />
// Open serial communications and wait for port to open:<br />
Serial.begin(9600);<br />
while (!Serial) {<br />
; // wait for serial port to connect. Needed for native USB port only<br />
}</p>
<p> Serial.print(&quot;\nInitializing SD card...&quot;);</p>
<p> // we'll use the initialization code from the utility libraries<br />
// since we're just testing if the card is working!<br />
if (!card.init(SPI_HALF_SPEED, chipSelect)) {<br />
Serial.println(&quot;initialization failed. Things to check:&quot;);<br />
Serial.println(&quot;* is a card inserted?&quot;);<br />
Serial.println(&quot;* is your wiring correct?&quot;);<br />
Serial.println(&quot;* did you change the chipSelect pin to match your shield or module?&quot;);<br />
return;<br />
} else {<br />
Serial.println(&quot;Wiring is correct and a card is present.&quot;);<br />
}</p>
<p> // print the type of card<br />
Serial.print(&quot;\nCard type: &quot;);<br />
switch (card.type()) {<br />
case SD_CARD_TYPE_SD1:<br />
Serial.println(&quot;SD1&quot;);<br />
break;<br />
case SD_CARD_TYPE_SD2:<br />
Serial.println(&quot;SD2&quot;);<br />
break;<br />
case SD_CARD_TYPE_SDHC:<br />
Serial.println(&quot;SDHC&quot;);<br />
break;<br />
default:<br />
Serial.println(&quot;Unknown&quot;);<br />
}</p>
<p> // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32<br />
if (!volume.init(card)) {<br />
Serial.println(&quot;Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card&quot;);<br />
return;<br />
}</p>
<p> // print the type and size of the first FAT-type volume<br />
uint32_t volumesize;<br />
Serial.print(&quot;\nVolume type is FAT&quot;);<br />
Serial.println(volume.fatType(), DEC);<br />
Serial.println();</p>
<p> volumesize = volume.blocksPerCluster(); // clusters are collections of blocks<br />
volumesize *= volume.clusterCount(); // we'll have a lot of clusters<br />
volumesize *= 512; // SD card blocks are always 512 bytes<br />
Serial.print(&quot;Volume size (bytes): &quot;);<br />
Serial.println(volumesize);<br />
Serial.print(&quot;Volume size (Kbytes): &quot;);<br />
volumesize /= 1024;<br />
Serial.println(volumesize);<br />
Serial.print(&quot;Volume size (Mbytes): &quot;);<br />
volumesize /= 1024;<br />
Serial.println(volumesize);</p>
<p> Serial.println(&quot;\nFiles found on the card (name, date and size in bytes): &quot;);<br />
root.openRoot(volume);</p>
<p> // list all files in the card with date and size<br />
root.ls(LS_R | LS_DATE | LS_SIZE);<br />
}</p>
<p>void loop(void) {</p>
<p>} </p>
<p>烧写官方的例程,上传程序后,等待系统启动(大约5s);</p>
<p>打开串口监视器,可以看到Arduino 101返回的信息,此时,如果TF卡里有内容的话,就会返回目录下的文件信息;</p>
<center>
<img alt="" src="http://intel.eetrend.com/files/2016-06/wen_zhang_/100001798-5053-3.png&…; />
</center>
<p><br />
需要注意的是,TF或SD卡的文件格式必须为FAT32或FAT16,否则系统无法读取内存卡的信息。</p>
<p>如何读取TF卡里的内容,我们已经得到了掌握,那么接下来我们学习另一个重要的功能——在TF卡里创建一个文件,你并写入内容。</p>
<p>准备好硬件以后,打开ArduinoIDE,找到文件-示例-SD-readWrite,如图;</p>
<center>
<img alt="" src="http://intel.eetrend.com/files/2016-06/wen_zhang_/100001798-5054-040.pn…; />
</center>
<p><br />
可以看到如下代码。</p>
<p>/*<br />
SD card read/write</p>
<p> This example shows how to read and write data to and from an SD card file<br />
The circuit:<br />
* SD card attached to SPI bus as follows:<br />
** MOSI - pin 11<br />
** MISO - pin 12<br />
** CLK - pin 13<br />
** CS - pin 4</p>
<p> created Nov 2010<br />
by David A. Mellis<br />
modified 9 Apr 2012<br />
by Tom Igoe</p>
<p> This example code is in the public domain.</p>
<p> */</p>
<p>#include <br />
#include </p>
<p>File myFile;</p>
<p>void setup() {<br />
// Open serial communications and wait for port to open:<br />
Serial.begin(9600);<br />
while (!Serial) {<br />
; // wait for serial port to connect. Needed for native USB port only<br />
}</p>
<p> Serial.print(&quot;Initializing SD card...&quot;);</p>
<p> if (!SD.begin(4)) {<br />
Serial.println(&quot;initialization failed!&quot;);<br />
return;<br />
}<br />
Serial.println(&quot;initialization done.&quot;);</p>
<p> // open the file. note that only one file can be open at a time,<br />
// so you have to close this one before opening another.<br />
myFile = SD.open(&quot;test.txt&quot;, FILE_WRITE);</p>
<p> // if the file opened okay, write to it:<br />
if (myFile) {<br />
Serial.print(&quot;Writing to test.txt...&quot;);<br />
myFile.println(&quot;testing 1, 2, 3.&quot;);<br />
// close the file:<br />
myFile.close();<br />
Serial.println(&quot;done.&quot;);<br />
} else {<br />
// if the file didn't open, print an error:<br />
Serial.println(&quot;error opening test.txt&quot;);<br />
}</p>
<p> // re-open the file for reading:<br />
myFile = SD.open(&quot;test.txt&quot;);<br />
if (myFile) {<br />
Serial.println(&quot;test.txt:&quot;);</p>
<p> // read from the file until there's nothing else in it:<br />
while (myFile.available()) {<br />
Serial.write(myFile.read());<br />
}<br />
// close the file:<br />
myFile.close();<br />
} else {<br />
// if the file didn't open, print an error:<br />
Serial.println(&quot;error opening test.txt&quot;);<br />
}<br />
}</p>
<p>void loop() {<br />
// nothing happens after setup<br />
}</p>
<p>代码的功能,是在其读到TF卡后,使用SD.open(&quot;test.txt&quot;, FILE_WRITE);创建了一个名为test.txt的文件夹,随后使用myFile.println(&quot;testing 1, 2, 3.&quot;);语句向txt文件中写入了testing 1, 2, 3.;<br />
我们下载后,打开串口监视器,可以在屏幕中看到输出的信息;</p>
<center>
<img alt="" src="http://intel.eetrend.com/files/2016-06/wen_zhang_/100001798-5055-050.pn…; />
</center>
<p><br />
此时,我们把TF卡取出来插到电脑上验证一下是否有这个文件夹以及文件;</p>
<center>
<img alt="" src="http://intel.eetrend.com/files/2016-06/wen_zhang_/100001798-5056-060.pn…; />
</center>
<p><br />
可以看到被创建的test.txt以及里面的内容testing 1,2,3,;</p>
<p>文章来源:<a href="http://www.arduino.cn/thread-21592-1-1.html">Aduino中文社区</a></p&gt;