我碰到一個問題,我沒有理由它在那裏。SdFat Arduino SD卡寫作
其基本思想是使用Arduino中的SdFat庫來寫入SD卡以記錄數據。
在日誌記錄之間,設備將進入睡眠狀態並使用MOSFET,我將完全關閉SD卡以節省功耗。
整個代碼的工作原理,但只有當我從我寫的文件讀取數據。 當我拿出代碼從文件中讀取時,它會創建文件,但根本沒有寫入。
現在我正在使用一個延遲,而不是讓微睡來減少需要關注的東西。
下面是與閱讀文件一起使用的代碼,使用示例文件只是爲了讓這件事情現在工作。
const int chipSelect = 4;
/*
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
#include <SdFat.h>
SdFat sd;
SdFile myFile;
char fileName[] = "2468.txt";
int sdPower = 3;
void setup()
{
pinMode(sdPower,OUTPUT);
}
void loop()
{
digitalWrite(sdPower,HIGH);
sd.begin(chipSelect, SPI_HALF_SPEED);
myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END);
myFile.println("Hello World");
myFile.close();
// re-open the file for reading:
if (!myFile.open(fileName, O_READ)) {
sd.errorHalt("opening test.txt for read failed");
}
// read from the file until there's nothing else in it:
int data;
while ((data = myFile.read()) >= 0) Serial.write(data);
// close the file:
myFile.close();
digitalWrite(sdPower,LOW);
delay(1000);
}
上面的工作,但我把最後幾行,做了閱讀它不會寫入文件,像這樣。
const int chipSelect = 4;
/*
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
#include <SdFat.h>
SdFat sd;
SdFile myFile;
char fileName[] = "2468.txt";
int sdPower = 3;
void setup()
{
pinMode(sdPower,OUTPUT);
}
void loop()
{
digitalWrite(3,HIGH);
sd.begin(chipSelect, SPI_HALF_SPEED);
myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END);
myFile.println("Hello World");
myFile.close();
digitalWrite(3,LOW);
delay(1000);
}
然而它仍然創建文件。 這真讓我困惑。
爲什麼程序在讀取部分被刪除時不能寫入?狡猾,因爲我在兩種情況下關閉文件應該沒有關係?
我也很難過。你有沒有弄清楚什麼是錯的? – user1464409 2015-02-17 18:26:41