ArduinoLearn/SDCard/SDCard.ino

144 lines
3.5 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* ...
*
* Author: Alexander Popov
* License: Unlicense
*/
// Необходимо выбрать одну из программ
// #define PROGRAM_SERVICE
// #define PROGRAM_WRITE
// #define PROGRAM_READ
#include <SPI.h>
#include <SD.h>
Sd2Card card;
SdVolume volume;
SdFile root;
// Пин CS
const int chipSelect = 10;
void setup() {
#ifdef PROGRAM_SERVICE
Serial.begin(9600);
Serial.print("\nInitializing SD card...");
// Неверное подключение или карта неисправна
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// Считываем тип карты и выводим его в COM-порт
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Инициализация файловой системы
if (!volume.init(card)) {
// Неверная файловая система
Serial.println("Could not find FAT16/FAT32 partition.");
return;
}
// Считываем тип и вычисляем размер первого раздела
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // блоков на кластер
volumesize *= volume.clusterCount(); // кластеров
volumesize *= 512; // 512 байтов в блоке, итого байт..
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// Выводим список файлов
root.ls(LS_R | LS_DATE | LS_SIZE);
#endif
#ifdef PROGRAM_WRITE
Serial.begin(9600);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present.");
return;
}
// Строка, которую мы запишем в файл
String dataString = "Hello, i'm Alexander Popov";
// Открываем файл, в который будет записана строка
File dataFile = SD.open("test.txt", FILE_WRITE);
if (dataFile) {
// Записываем строку в файл
dataFile.println(dataString);
dataFile.close();
Serial.println("Success!");
} else {
// Выводим ошибку если не удалось открыть файл
Serial.println("Error opening file.");
}
#endif
#ifdef PROGRAM_READ
Serial.begin(9600);
if(!SD.begin( chipSelect )) {
Serial.println("Initialization failed!");
return;
}
// Открываем файл для чтения
File myFile = SD.open("test.txt");
if (myFile) {
// Считываем все байты из файла и выводим их в COM-порт
while (myFile.available()) {
Serial.write(myFile.read());
}
// Закрываем файл
myFile.close();
}
else {
// Выводим ошибку если не удалось открыть файл
Serial.println("Error opening test_file.txt");
}
#endif
}
void loop(void) {
// ...
}