SD模組腳位接法 |
Code
#include注意:GND腳位要注意不要接錯。建議接在上圖的位置。File myFile; void setup() { Serial.begin(57600); pinMode(4, OUTPUT); // SD模組之CS ->> pin 4 if (!SD.begin(4)) { Serial.println("initialization failed!"); return; } Serial.println("initialization done."); //if (!SD.exists("test")) { // Serial.println("example.txt not exists."); // SD.mkdir("test"); //} Serial.println("write test.txt"); writeFile("test.txt","Arduino Led On"); readFile( "test.txt"); } void loop() { } void writeFile( char* filename, char* content) { myFile = SD.open(filename, FILE_WRITE); if (myFile) { myFile.println(content); myFile.close(); Serial.println("done.."); } } void readFile( char* filename) { // re-open the file for reading: myFile = SD.open(filename); if (myFile) { Serial.println(filename); // 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 file"); } }
SD卡測試結果 |
PS:測試透過藍芽將資料紀錄再SD卡,發現程式單元測試沒問題,但接在一起卻沒反應,還不太清楚為什麼。
參考資料
1.http://blog.oscarliang.net/sd-card-arduino/
2.http://gogoprivateryan.blogspot.tw/2014/08/microsd-arduino-microsd.html
沒有留言:
張貼留言