通信接口

SD卡专题 - arduino如何读写SD卡上的数据

字号+ 作者:PLC工程师 来源:未知 2016-04-13 09:35 我要评论( )

arduino如何读写SD卡上的数据

本示例介绍如何读写SD卡上的数据。

硬件:

  • Arduino或者Genuino
  • Ethernet Shield或者其他带SD卡槽的设备
  • SD卡

电路:

 

 

原理:

代码:

The code below is configured for use with an Ethernet shield, which has an onboard SD slot. In the setup(), we callSD.begin(), naming pin 4 as the CS pin. This pin varies depending on the make of shield or board you are using.

In setup(), create a new file with SD.open() named "test.txt". FILE_WRITE enables read and write access to the file, starting at the end. If a file "test.txt" was already on the card, that file would be opened.

Name the instance of the opened file "myFile".

Once opened, use myFile.println() to write a string to the card, followed by a carriage return. Once the content is written, close the file.

Again, open the file with SD.open(). Once opened, ask the Arduino to read the contents of the file with SD.read()and send them over the serial port. After all the contents of the file are read, close the file with SD.close().

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

 This example code is in the public domain.

 */

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

如需更详细资料,如有好资源分享,请加入QQ交流群:214461008

欢迎阅读,欢迎转载,不用注明出处!本站只提供入门和进阶资料,做您学习的小伙伴!

请加群:214461008

相关文章
  • json在arduino上的应用

    json在arduino上的应用

    2016-06-20 09:45

  • 两块Arduino之间通过SPI通信

    两块Arduino之间通过SPI通信

    2016-06-01 13:02

  • 常见的几种arduino型号的IIC引脚定义

    常见的几种arduino型号的IIC引脚定义

    2016-05-29 14:40

  • Arduino软串口的使用

    Arduino软串口的使用

    2016-05-04 15:27

网友点评
Arduino
热门教程
关注我们
自动化世界