Skip to content

Commit e3fc8c5

Browse files
Merge pull request #295 from UniquePete/patch-2
Create ReadWrite.ino
2 parents e0607b0 + 39aa380 commit e3fc8c5

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
SD card read/write
3+
4+
SD card reader attached to SPI1 bus as follows:
5+
** MOSI1 - GPIO1
6+
** MISO1 - GPIO2
7+
** CLK1 - GPIO3
8+
** CS1 - GPIO4
9+
*/
10+
11+
#include <SPI.h>
12+
#include <SD.h>
13+
14+
#define CS1 GPIO4
15+
16+
File myFile;
17+
18+
void setup() {
19+
// Open serial communications and wait for port to open:
20+
Serial.begin(115200);
21+
22+
SPI1.begin(SCK1, MISO1, MOSI1, CS1);
23+
24+
Serial.print("Initializing SD card...");
25+
26+
if (!SD.begin(CS1)) {
27+
Serial.println("failed!");
28+
while (1);
29+
}
30+
Serial.println("done");
31+
32+
// open the file. note that only one file can be open at a time,
33+
// so you have to close this one before opening another.
34+
myFile = SD.open("test.txt", FILE_WRITE);
35+
36+
// if the file opened okay, write to it:
37+
if (myFile) {
38+
Serial.print("Writing to test.txt...");
39+
myFile.println("Testing 1, 2, 3...");
40+
// close the file:
41+
myFile.close();
42+
Serial.println("done");
43+
} else {
44+
// if the file didn't open, print an error:
45+
Serial.println("Error opening test.txt to write!");
46+
}
47+
delay(1000);
48+
// re-open the file for reading:
49+
myFile = SD.open("test.txt");
50+
if (myFile) {
51+
Serial.println("test.txt:");
52+
53+
// read from the file until there's nothing else in it:
54+
while (myFile.available()) {
55+
Serial.write(myFile.read());
56+
}
57+
// close the file:
58+
myFile.close();
59+
} else {
60+
// if the file didn't open, print an error:
61+
Serial.println("Error opening test.txt to read!");
62+
}
63+
}
64+
65+
void loop() {
66+
// nothing happens after setup
67+
}

0 commit comments

Comments
 (0)