我們用到的庫 TFT_eSPI 這里我們使用了中景園的ST7789 一般屏幕的引腳定義如下: 接線: 我們直接用VSPI接線 如何在TFT_eSPI中設置引腳?? 首先, 我們打開 User_Setup.h, 具體位置在(platformIO平臺): 然后根據文件中的提示設置就可以了, 對于ESP32 + ST7789來說, 具體修改了如下內容: 在眾多的驅動文件中,選擇適合自己屏幕的, 注釋掉不用的 對ST7789 ST7735 ILI9163來說, 要設置寬高 注釋掉其他的定義, 定義自己的引腳 第三步是配置字庫, ESP32內存足夠, 不用配置了,都帶著就行 因為上面我們的設置是自定義設置, 所以在User_Setup_Select.h中, 應啟用自定義配置, 注釋其他配置文件 二. 顏色和字體 1.關于顏色 關于顏色值, TFT一般都使用16位的RGB565顏色,在本庫中, 典型顏色已經定義好了: /*************************************************************************************** * Section 6: Colour enumeration ***************************************************************************************/// Default color definitions#define TFT_BLACK 0x0000 /* 0, 0, 0 */#define TFT_NAVY 0x000F /* 0, 0, 128 */#define TFT_DARKGREEN 0x03E0 /* 0, 128, 0 */#define TFT_DARKCYAN 0x03EF /* 0, 128, 128 */#define TFT_MAROON 0x7800 /* 128, 0, 0 */#define TFT_PURPLE 0x780F /* 128, 0, 128 */#define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */#define TFT_LIGHTGREY 0xD69A /* 211, 211, 211 */#define TFT_DARKGREY 0x7BEF /* 128, 128, 128 */#define TFT_BLUE 0x001F /* 0, 0, 255 */#define TFT_GREEN 0x07E0 /* 0, 255, 0 */#define TFT_CYAN 0x07FF /* 0, 255, 255 */#define TFT_RED 0xF800 /* 255, 0, 0 */#define TFT_MAGENTA 0xF81F /* 255, 0, 255 */#define TFT_YELLOW 0xFFE0 /* 255, 255, 0 */#define TFT_WHITE 0xFFFF /* 255, 255, 255 */#define TFT_ORANGE 0xFDA0 /* 255, 180, 0 */#define TFT_GREENYELLOW 0xB7E0 /* 180, 255, 0 */#define TFT_PINK 0xFE19 /* 255, 192, 203 */ //Lighter pink, was 0xFC9F #define TFT_BROWN 0x9A60 /* 150, 75, 0 */#define TFT_GOLD 0xFEA0 /* 255, 215, 0 */#define TFT_SILVER 0xC618 /* 192, 192, 192 */#define TFT_SKYBLUE 0x867D /* 135, 206, 235 */#define TFT_VIOLET 0x915C /* 180, 46, 226 */ 2. RGB顏色轉565顏色 tft.color565() uint16_t red = tft.color565(255, 0, 0);uint16_t green = tft.color565(0, 255, 0);uint16_t blue = tft.color565(0, 0, 255);uint16_t yellow = tft.color565(255, 255, 0);tft.fillScreen(tft.color565(128, 0, 128)); //使用 3. 關于半透明 tft.alphaBlend() 在填入顏色的地方填入此函數可以開啟alpha半透明通道 for (uint16_t a = 0; a < 255; a++) // Alpha 0 = 100% background, alpha 255 = 100% foreground { //tft.drawFastHLine(192, a, 12, tft.alphaBlend(a, TFT_BLACK, TFT_WHITE)); tft.drawFastHLine(204, a, 12, tft.alphaBlend(a, TFT_RED, TFT_WHITE)); tft.drawFastHLine(216, a, 12, tft.alphaBlend(a, TFT_GREEN, TFT_WHITE)); tft.drawFastHLine(228, a, 12, tft.alphaBlend(a, TFT_BLUE, TFT_WHITE)); } 4. 關于默認字體 編號范圍是 1、2、4、6、7、8,不同的編號代表不同的字體, 不同的字體由于分辨率不同, 基本大小不同 // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH#define LOAD_GLCD// Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters#define LOAD_FONT2// Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters#define LOAD_FONT4// Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm#define LOAD_FONT6// Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-.#define LOAD_FONT7// Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.#define LOAD_FONT8// Font 8. Alternative to Font 8 above, slightly narrower, so 3 digits fit a 160 pixel TFT//#define LOAD_FONT8N// FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts#define LOAD_GFXFF// Comment out the #define below to stop the SPIFFS filing system and smooth font code being loaded// this will save ~20kbytes of FLASH#define SMOOTH_FONT 5. 關于自定義字體 TFT_eSPI自帶了很多自定義庫, 而且也可以自己去生成新的自定義庫. 默認的自定義字體庫在: 如果想學習自定義字庫用法, 請參看例程: 三. 相關API 1. tft.init(); //初始化 初始化屏幕, 如果是ST7735,可以往里面傳一個參數, 具體用到時再看 2. tft.fillScreen(TFT_BLACK); //填充全屏幕 填充全屏幕, 后面是顏色值, tft.fillScreen(uint32_t color); 3. 屏幕旋轉 // 設置屏幕顯示的旋轉角度,參數為:0, 1, 2, 3// 分別代表 0°、90°、180°、270°void setRotation(uint8_t r); 4. 屏幕反色 //反轉顯示顏色i = 1反轉,i = 0正常tft.invertDisplay(bool i); 四. 文字相關API 1. tft.setCursor(20, 10, 4); //設置打字起始坐標位置和字號 // 設置文本顯示坐標,默認以文本左上角為參考點,可以改變參考點void setCursor(int16_t x, int16_t y);// 設置文本顯示坐標,和文本的字體void setCursor(int16_t x, int16_t y, uint8_t font); 2. tft.setTextColor(2); //設置字體顏色 // 設置文本顏色void setTextColor(uint16_t color);// 設置文本顏色與背景色void setTextColor(uint16_t fgcolor, uint16_t bgcolor); 設置背景顏色可以有效的防止數字疊在一起 3. tft.setTextSize(2); //設置字體大小 設置文本大小可以放大字體的顯示,但是字體的'分辨率'是不會變的 // 設置文本大小,文本大小范圍為 1~7 的整數void setTextSize(uint8_t size); 4. tft.print('Hello World!'); // 顯示字體 tft.print('Hello World!'); 5. tft.printf, tft.println //顯示字體 特別注意: 字庫7是仿7段數碼屏的樣式 五. 繪制文字相關API 1. 繪制字符串(居左) int16_t drawString(const String &string, int32_t x, int32_t y)int16_t drawString(const char *string, int32_t x, int32_t y)int16_t drawString(const String &string, int32_t x, int32_t y, uint8_t font)int16_t drawString(const char *string, int32_t x, int32_t y, uint8_t font) 2. 繪制字符串(居中) int16_t drawCentreString(const char *string, int32_t x, int32_t y, uint8_t font)int16_t drawCentreString(const String &string, int32_t x, int32_t y, uint8_t font) 3. 繪制字符串(居右) int16_t drawRightString(const char *string, int32_t x, int32_t y, uint8_t font)int16_t drawRightString(const String &string, int32_t x, int32_t y, uint8_t font) 4. 繪制字符 int16_t drawChar(uint16_t uniCode, int32_t x, int32_t y)int16_t drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font)void drawChar(int32_t x, int32_t y, uint16_t c, uint32_t color, uint32_t bg, uint8_t size) 5. 繪制浮點數 int16_t TFT_eSPI::drawFloat(float floatNumber, uint8_t decimal, int32_t x, int32_t y)int16_t TFT_eSPI::drawFloat(float floatNumber, uint8_t decimal, int32_t x, int32_t y, uint8_t font) tft.drawFloat(3.124, 4, 0,0,4); 6. 繪制數字 int16_t drawNumber(long intNumber, int32_t x, int32_t y)int16_t drawNumber(long intNumber, int32_t x, int32_t y, uint8_t font) 7. 繪制 六. 繪制幾何圖形 1. 畫點 void drawPixel(int32_t x, int32_t y, uint32_t color) 2.畫線 void drawLine(int32_t xs, int32_t ys, int32_t xe, int32_t ye, uint32_t color) 3.畫橫線(快速) void drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color) 4.畫豎線(快速) void drawFastVLine(int32_t x, int32_t y, int32_t h, uint32_t color) 5. 畫空心圓 tft.drawCircle(100,100,50,TFT_RED); 6. 畫實心圓 void fillCircle(int32_t x, int32_t y, int32_t r, uint32_t color) 7. 畫空心橢圓 tft.drawEllipse(100,100,100,60,TFT_GREENYELLOW); 8. 畫實心橢圓 void drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color) 9. 畫空心矩形 void drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color) 10. 畫實心矩形 void fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color) 11. 畫空心圓角矩形 void drawRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t radius, uint32_t color) 12. 畫實心圓角矩形 void fillRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t radius, uint32_t color) 13. 畫空心三角形 void drawTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, uint32_t color) 14. 畫實心三角形 void fillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, uint32_t color) 七. 圖片顯示相關 1. 顯示BMP圖片 void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor)void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor, uint16_t bgcolor) 2. XBM xbm是一種簡單的雙色圖片位圖格式,在早期的cgi中運用較多,目前多用于計數器上 這里TFT_eSPI推薦了一個在線XBM制作工具: https://www.online-utility.org/image/convert/to/XBM 實測非常好用 void drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor)void drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor, uint16_t bgcolor) 3. 顯示圖片 void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data)void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data)void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data, uint16_t transparent)void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data, uint16_t transparent)void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint8_t *data, bool bpp8 = true, uint16_t *cmap = (uint16_t *)nullptr)void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint8_t *data, uint8_t transparent, bool bpp8 = true, uint16_t *cmap = (uint16_t *)nullptr)一. 硬件接線
ESP32引腳 ST7789引腳 功能 GND GND 接地 3V3 VCC 電源 (VCLK)18 SCL SPI時鐘線 (VMOSI)23 SDA SPI主出從入線 26 RES 復位引腳 27 DC 數據/命令選擇線 (VCS0)5 CS SPI片選線 沒接 BLK 背光控制線 第一步: 修改自定義驅動文件
設置寬高
第二步: 引腳定義
第三步.第四步保持默認, 需要時再修改就可以
第四步是 配置SPI的頻率 / 配置用VSPI(默認)還是HSPI /額外的一步: User_Setup_Select.h中選擇用戶自定義配置
上一篇:玩轉 ESP32 + Arduino(二十九) TFT_eSPI庫驅動ST7789之動畫專題
下一篇:玩轉 ESP32 + Arduino(二十七) ESP對象
推薦閱讀最新更新時間:2025-04-09 10:05



