LoginSignup
0
0

More than 5 years have passed since last update.

lazuriteでsd-card

Last updated at Posted at 2018-07-30

概要

lazuriteでsd-cardやってみた。

写真

CIMG2667.JPG

回路図

image

サンプルコード

#define SDSPI_SS_PIN        ( 4 )
#define BUF_SIZE            ( 32 )
#define SECOND              ( 0 )
#define MINUTE              ( 0 )
#define HOUR                ( 10 )
#define DAY                 ( 30 )
#define MONTH               ( 7 )
#define YEAR                ( 18 )
void initRTC() 
{
    Serial.println("RTC started.");
    RTC.begin();
    RTC.setTime(HOUR, MINUTE, SECOND);
    RTC.setDate(DAY, MONTH, YEAR);
}
void setup() 
{
    st_File_v myFile;
    static uint8_t writebuf[] = "testing 1, 2, 3.";
    static uint8_t readbuf[BUF_SIZE+1];
    uint16_t remained;
    File.init(&myFile);
    Serial.begin(115200);
    initRTC();
    Serial.println("\nInitializing SD card...");
    if (!SD.begin(SDSPI_SS_PIN)) 
    {
        Serial.println("Initializing failed.");
        return;
    } 
    else 
    {
        Serial.println("\nInitializing successed and card is present.");
    };
    if (SD.open("test.txt", FILE_WRITE_APPEND, &myFile)) 
    {
        Serial.print("Writing to test.txt...");
        File.write(&myFile, writebuf, sizeof(writebuf)-1);
        File.close(&myFile);
        Serial.println("\ndone.");
    } 
    else 
    {
        Serial.println("\nerror opening test.txt");
    }
    if (SD.open("test.txt", FILE_READ, &myFile)) 
    {
        Serial.println("Reading from test.txt:");
        while ((remained = File.available(&myFile)) != 0) 
        {
            if (remained > BUF_SIZE) remained = BUF_SIZE;
            File.read(&myFile, readbuf, remained);
            readbuf[remained] = NULL;
            Serial.print(readbuf);
        }
        File.close(&myFile);
        Serial.println("\ndone.");
    } 
    else 
    {
        Serial.println("\nerror opening test.txt");
    }
}
void loop() 
{
}




結果

RTC started.

Initializing SD card...

Initializing successed and card is present.
Writing to test.txt...
done.
Reading from test.txt:
testing 1, 2, 3.testing 1, 2, 3.testing 1, 2, 3.
done.

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0