範例十一

ST7735 80x160 液晶螢幕

此範例用的是 0.96 吋 IPS 80x160 的 ST7735 液晶螢幕,由於液晶螢幕腳位種類很多,請注意擴充板插槽只支援以下這款,若腳位順序不符請自行用杜邦線接.

擴充板插座為七 pins, 此模組為八 pins, 請對齊左邊的 GND腳位,最右邊的 BLK 懸空不接.

使用 TFT_eSPI library 需自行修改 User_Setup.h 內容來對映不同的液晶螢幕,提供本擴充板對映的內容供使用者參考

// User_Setup.h 內容

//Setup for ESP32 and ST7735 80 x 160 TFT


#define ST7735_DRIVER


#define TFT_WIDTH 80

#define TFT_HEIGHT 160


// #define ST7735_GREENTAB160x80

#define ST7735_REDTAB160x80


#define TFT_RGB_ORDER TFT_BGR // Colour order Red-Green-Blue

// #define TFT_RGB_ORDER TFT_RGB // Colour order Red-Green-Blue

// #define TFT_RGB_ORDER TFT_BGR // Colour order Blue-Green-Red


// #define TFT_MISO 12

#define TFT_MOSI 13 // SDA

#define TFT_SCLK 14 // SCL

#define TFT_CS 15 // Chip select control pin

#define TFT_DC 27 // Data Command control pin 2

#define TFT_RST 16 // Reset pin (could connect to RST pin) 4


#define LOAD_GLCD // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH

#define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters

#define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters

#define LOAD_FONT6 // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm

#define LOAD_FONT7 // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:.

#define LOAD_FONT8 // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.

//#define LOAD_FONT8N // Font 8. Alternative to Font 8 above, slightly narrower, so 3 digits fit a 160 pixel TFT

#define LOAD_GFXFF // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts


#define SMOOTH_FONT


#define SPI_FREQUENCY 27000000 // Actually sets it to 26.67MHz = 80/3


// 範例十一:

// 驅動 ST7735 特定腳位的 TFT LCD 全彩液晶螢幕. 需注意腳位是否符合,不符合時,請自行用杜邦線連接

// 使用 HSPI 介面,腳位順序如下

// GND

// VCC 3.3V

// SCLK(SCL) 14

// MOSI(SDA) 13

// RES 16

// DC 27

// CS 15 (or pull low )

// BLK (背光控制,可不接)

// 需要的 library : TFT_eSPI

// FB : https://www.facebook.com/mason.chen.1420


#include <TFT_eSPI.h> // 需設定好 User_Setup.h 內的腳位

TFT_eSPI tft;


// Assign 16-bit color values:

#define BLACK 0x0000

#define BLUE 0x001F

#define RED 0xF800

#define GREEN 0x07E0

#define CYAN 0x07FF

#define MAGENTA 0xF81F

#define YELLOW 0xFFE0

#define WHITE 0xFFFF

#define GRAY 0x8410


void setup()

{

tft.begin();

}


void loop()

{

static uint8_t aspect = 0;

const char *aspectname[] = {

"PORTRAIT", "LANDSCAPE", "PORTRAIT_REV", "LANDSCAPE_REV"

};

const char *colorname[] = { "BLUE", "GREEN", "RED", "GRAY" };

uint16_t colormask[] = { BLUE, GREEN, RED, GRAY };

//uint16_t ID = tft.readID(); // MCUFRIEND_kbv style

uint16_t ID = 0x7735;

tft.setRotation(aspect);

int width = tft.width();

int height = tft.height();

int sz = (width <= 130 || height <= 130) ? 1 : 2;

int mgn = (sz < 2) ? 8 : 32;

int x = mgn + 4 * sz, y = x;

tft.fillScreen(colormask[aspect]);

tft.drawRect(0, 0, width, height, WHITE);

tft.drawRect(mgn, mgn, width - mgn * 2, height - mgn * 2, WHITE);

tft.setTextSize(sz);

tft.setTextColor(BLACK);

tft.setCursor(x, y);

tft.print("ID=0x");

tft.print(ID, HEX);

if (ID == 0xD3D3) tft.print(" w/o");

tft.setCursor(x, y += 10 * sz);

tft.print(aspectname[aspect]);

tft.setCursor(x, y += 10 * sz);

tft.print(width);

tft.print(" x ");

tft.print(height);

tft.setTextColor(WHITE);

tft.setCursor(x, y += 10 * sz);

tft.setTextSize(sz * 2);

tft.print(colorname[aspect]);

if (++aspect > 3) aspect = 0;

delay(5000);

}