2014-10-03 3 views
1

내가 거기에있을 이유가 없다는 것을 알았습니다.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); 
} 

아직 파일을 생성합니다. 이것은 정말 혼란 스럽습니다.

왜 프로그램에서 읽기 섹션이 제거 되었는가? 두 인스턴스 모두에서 파일을 닫아야하므로 중요하지 않습니다.

+0

너무 어려움을 겪고 있습니다. 무엇이 잘못 되었는가? – user1464409

답변

0

아마 카드에 전원이 디렉토리와 FAT 데이터가 close() 명령으로 카드를 어떻게 작성했다 전에

{ 
digitalWrite(3,LOW); 
} 

을 제거하고있다. 카드를 끄기 전에 1 초 지연 지연을 시도하십시오. 작업 예제의 읽기 데이터 루프는 파일이 제대로 닫혔는지 확인합니다 .-

+1

비트 편집 필요 – Billa