2023/12/28
當使用黑色的 TFT 模組時,背面帶有 microSD 卡槽,此為簡單的 SD 卡讀寫測寫測試範例
需注意: 若用的是紅色的螢幕模組時,背面的大卡是不支援的
// 讀取 micro SD 卡內的 pico_game_test_pic.bmp 做顯示
// 需先準備好要的 bmp 檔,放到 micro SD 卡中
// library :
// Adafruit_GFX.h Core graphics library
// Adafruit_ILI9341.h Hardware-specific library
// SdFat.h SD card & FAT filesystem library
// Adafruit_SPIFlash SPI QSPI flash library
// Adafruit_ImageReader Image-reading functions
// 2023/10/4 by Mason
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ILI9341.h> // Hardware-specific library
#include <SdFat.h> // SD card & FAT filesystem library
#include <Adafruit_SPIFlash.h> // SPI / QSPI flash library
#include <Adafruit_ImageReader.h> // Image-reading functions
// TFT display and SD card share the hardware SPI interface, using
// 'select' pins for each to identify the active device on the bus.
#define SD_CS 22 // SD card select pin
#define TFT_DC 20
#define TFT_CS 17
#define TFT_RST 21
SdFat SD; // SD card filesystem
Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_Image img; // An image loaded into RAM
int32_t width = 0, // BMP image dimensions
height = 0;
void setup(void) {
ImageReturnCode stat; // Status from image-reading functions
Serial.begin(9600);
// while(!Serial); // Wait for Serial Monitor before continuing
tft.begin(); // Initialize screen
tft.invertDisplay(true);
tft.setRotation(1);
// The Adafruit_ImageReader constructor call (above, before setup())
// accepts an uninitialized SdFat or FatVolume object. This MUST
// BE INITIALIZED before using any of the image reader functions!
Serial.print(F("Initializing filesystem..."));
// SD card is pretty straightforward, a single call...
if(!SD.begin(SD_CS, SD_SCK_MHZ(25))) { // Requires 25 MHz limit
Serial.println(F("SD begin() failed"));
for(;;); // Fatal error, do not continue
}
Serial.println(F("OK!"));
// Fill screen blue. Not a required step, this just shows that we're
// successfully communicating with the screen.
tft.fillScreen(ILI9341_RED);
delay(2000);
tft.fillScreen(ILI9341_GREEN);
delay(2000);
tft.fillScreen(ILI9341_BLUE);
delay(2000);
// Load BMP 's1.bmp' into a GFX canvas in RAM.
Serial.print(F("Loading bmp file to canvas..."));
stat = reader.loadBMP("/pico_game_test_pic.bmp", img);
reader.printStatus(stat);
}
void loop() {
for(int r=0; r<4; r++) { // For each of 4 rotations...
tft.setRotation(r); // Set rotation
tft.fillScreen(0); // and clear screen
img.draw(tft, 0, 0); // draw from RAM-resident images:
delay(2000);
}
}