2018年6月8日 星期五

模組介紹 : faya SD卡模組



學習目標 : 認識faya SD卡模組的功能及使用方式
學習時間 : 35mins
示範模組 : (1) faya brickNano
                   (2) faya SD卡模組


====================功能介紹====================
faya SD卡模組透過SPI接線,從Arduino的函式下指令,就能讀取讀取SD卡資料,也能建立檔案並將資料記錄在檔案裡。



faya SD卡模組的右邊有八個埠,分別是CS、MOSI、SCLK、MISO、DAT1、DAT2、DETECT、PROTECT,其中CS、MOSI、SCLK、MISO是SPI通訊協定,用來和Arduion互相溝通。

在Arduino brickNano中SPI通訊有固定的腳位,如下所示:

SD卡模組             Arduino brickNano
CS                ==>              D10
MOSI           ==>              D11
MISO           ==>              D12
SCLK           ==>              D13

====================原理知識====================
以下解釋提供給有需要知道背後原理的人:


上圖為SD卡的腳位定義,SD卡支援兩種匯流排方式:SD方式與SPI方式。其中SD方式採用6線制進行資料通信,其特點是資料位元數寬(4位元)、速度快。SPI方式採用4線制進行資料通信,其特點是速度要比SD方式慢,但匯流排簡單、不需要進行CRC校驗,比較適合單晶片採用這種方式對SD卡進行讀寫操作。

SD卡只能使用3.3V 的IO電壓,所以MCU一定要能夠支援3.3V的IO埠輸出。 注意:在SPI模式下,CS/MOSI/MISO/CLK 都需要加10~100K 左右的提升電阻。 SD 卡要進入SPI 模式很簡單,CS 為(低電壓)則SPI 模式被啟用。

電路圖如下所示:


====================範例實作(一)====================
了解模組功能(原理)後,我們用以下範例來展示模組的功能:

目標:
(1)使用Serial Monitor,查看SD卡的規格以及所儲存的檔案資訊

接線:
(1) 電源線連接如下圖所示,連接的說明請看這篇文章簡易版


(2) 訊號線連接
                   Arduino_D10 ===> SD卡模組_CS               
                   Arduino_D11 ===> SD卡模組_MOSI
                   Arduino_D12 ===> SD卡模組_MISO
                   Arduino_D13 ===> SD卡模組_SCLK
                  *注意D12和D13並未按照順序



範例程式:

// 2018/06/08
// Faya-Nugget 範例程式 (SDCard_1.ino)
// 單元: 模組介紹-faya SD卡模組
// 網址: https://fayalab.blogspot.com/2018/06/sdcard.html
// 目標: (1)在Serial Monitor端觀看SD卡裡的資料
// 接線: Arduino ==> faya模組
// D10 ==> CS (SD卡模組)
// D11 ==> MOSI (SD卡模組)
// D12 ==> MISO (SD卡模組)
// D13 ==> SCLK (SD卡模組)
// 匯入SD卡 所需library
#include <SPI.h>
#include <SD.h>
// set up variables using the SD utility library functions: 定義類別
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 10; //定義CS腳位
void setup() {
// 開啟串列通訊,9600 baud rate
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("\nInitializing SD card...");
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
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");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
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);
// list all files in the card with date and size
// 顯示檔案日期和大小
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop(void) {
}
view raw SDCard_1.ino hosted with ❤ by GitHub
備註:
- 此範例程式直接使用Arduino IDE的SD卡範例程式 (CardInfo.ino),通常讀取記憶卡和檔案資訊的程式格式都一致的,我們只要把修改硬體腳位即可。

範例結果:

插入SD卡,下載程式後,開啟Serial Monitor,SD卡和檔案資訊會顯示在視窗中,如下所示。


====================範例實作(二)====================

目標:
(1)利用Arduino在SD卡裡建立檔案(test.txt)並儲存

接線:
和範例實作(一)的接法相同

範例程式:
// 2018/06/08
// Faya-Nugget 範例程式 (SDCard_2.ino)
// 單元: 模組介紹-faya SD卡模組
// 網址: https://fayalab.blogspot.com/2018/06/sdcard.html
// 目標: (1)在SD卡裡建立TXT檔案並寫入
// 接線: Arduino ==> faya模組
// D10 ==> CS (SD卡模組)
// D11 ==> MOSI (SD卡模組)
// D12 ==> MISO (SD卡模組)
// D13 ==> SCLK (SD卡模組)
// 匯入SD卡 所需library
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
delay(1000);
Serial.print("Writing to test.txt...");
myFile.println("Hello Fayalab");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
view raw SDCard_2.ino hosted with ❤ by GitHub

備註:
- 此範例程式直接使用Arduino IDE的SD卡範例程式 (Files.ino),會在SD卡內建立一個檔案[test.txt],並且在裡面寫入內容。
- L41: 寫入 [Hello Fayalab ]
- L42: 寫入 [testing 1, 2, 3. ]


範例結果:

打開Serial Monitor,MCU會建立檔案[text.txt]在SD卡中



將SD卡插入電腦讀卡機,用電腦記事本開啟檔案,可看到剛剛在程式裡建立的內容。


討論:
  • SD卡模組通常都會搭配數種模組做搭配以利做為紀錄用,當成一個小型紀錄器,該模組是必不可缺的模組,只是看使用者如何搭配與運用,這模組有多種可能性就等著你去實現它。
  • 使用者可以在Arduino的介面上選檔案>範例>SD裡都有許多參考範例可以用讓SD卡使用更廣。
歡迎大家在底下留言或到我們的粉絲團留言喔!
====================================
fayalab 粉絲團
FB本篇留言版


沒有留言:

張貼留言