ESP32 + Arduino使用TFT_eSPI库(LCD驱动芯片ST7789)

TFT_eSPI是用于TFT-LCD液晶屏的Arduino图形库,项目地址 Github 可支持下面多种液晶屏驱动芯片: GC9A01 ILI9163 ILI9225 ILI9341 ILI9342 ILI9481 (DMA not supported with SPI) ILI9486 (DMA not supported with SPI) ILI9488 (DMA not supported with SPI) HX8357B (16 bit parallel tested with RP2040) HX8357C (16 bit parallel tested with RP2040) HX8357D R61581 RM68120 (support files added but untested) RM68140 S6D02A1 SSD1351 SSD1963 (this controller only has a parallel interface option) ST7735 ST7789 ST7796

之前在淘宝买了一块2.4寸240×320像素的SPI串口屏(使用的驱动芯片是ST7789),下面尝试使用TFT_eSPI库在Arduino+ESP32上快速点亮这个屏幕。屏幕上有7个引脚,我对应的接线如下:

| ESP32| SPI屏幕|引脚说明|

|GND|GND| 接地|

|3V3|VCC| 电源|

|18|SCL| SPI时钟|

|23|SDA(MOSI)|SPI MOSI|

|4| RES|复位引脚|

|2| DC | 数据/命令选择|

|x|BLC|背光|

在Arduino的库管理器中下载TFTeSPI库,之后在该库的路径下(\Arduino\libraries\TFTeSPI)打开Setup24ST7789.h文件。Setup24ST7789.h中有一些需要配置的宏,需要仔细阅读这个头文件中的注释。几个比较关键的地方如下:

// Generic ESP32 setup
//夏天取消注释
#define TFT_MISO  -1 // Not connected  19
#define TFT_MOSI 23
#define TFT_SCLK 18
#define TFT_CS    -1 // Not connected
#define TFT_DC    2
#define TFT_RST   4  // Connect reset to ensure display initialises

// For NodeMCU - use pin numbers in the form PIN_Dx where Dx is the NodeMCU pin designation
// 夏天 2023-5-24注释掉
//#define TFT_CS   -1      // Define as not used
//#define TFT_DC   PIN_D1  // Data Command control pin
//#define TFT_RST  PIN_D4  // TFT reset pin (could connect to NodeMCU RST, see next line)
//#define TFT_RST  -1    // TFT reset pin connect to NodeMCU RST, must also then add 10K pull down to TFT SCK

主要就是修改了引脚,该配置文件中默认设置了屏幕宽高,如下

#define TFT_WIDTH  240
#define TFT_HEIGHT 240

可根据自己的屏幕像素设置即可

注意到TFTeSPI库下面还有一个UserSetupSelect.h头文件,这个头文件中可以选择预先定义好的用户配置(相关文件位于TFTeSPI/UserSetups文件夹中)。由于这些配置不适用于我买的这个屏幕,因此直接使用Setup24ST7789.h中的定义。 所以只保留Setup24_ST7789.h其他全部注释掉如下:

//#include <User_Setups/Setup22_TTGO_T4.h>           // Setup file for ESP32 and TTGO T4 version 1.2
//#include <User_Setups/Setup22_TTGO_T4_v1.3.h>      // Setup file for ESP32 and TTGO T4 version 1.3
//#include <User_Setups/Setup23_TTGO_TM.h>           // Setup file for ESP32 and TTGO TM ST7789 SPI bus TFT
#include <User_Setups/Setup24_ST7789.h>            // Setup file for DSTIKE/ESP32/ESP8266 configured for ST7789 240 x 240
//#include <User_Setups/Setup25_TTGO_T_Display.h>    // Setup file for ESP32 and TTGO T-Display ST7789V SPI bus TFT
//#include <User_Setups/Setup26_TTGO_T_Wristband.h>  // Setup file for ESP32 and TTGO T-Wristband ST7735 SPI bus TFT

下面的代码在屏幕上测试文字输出,从TFT_eSPI/examples中可以找到各种例子来进行测试。

#include <TFT_eSPI.h> 
#include <SPI.h>

#define TFT_GREY 0x5AEB // New colour

TFT_eSPI tft = TFT_eSPI();  // Invoke library


void setup(void) {
  tft.init();
  tft.setRotation(0);
  tft.invertDisplay(0);
}

void loop() {

  // Fill screen with grey so we can see the effect of printing with and without 
  // a background colour defined
  tft.fillScreen(TFT_GREY);

  // Set "cursor" at top left corner of display (0,0) and select font 2
  // (cursor will move to next line automatically during printing with 'tft.println'
  //  or stay on the line is there is room for the text with tft.print)
  tft.setCursor(0, 0, 2);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  tft.setTextColor(TFT_WHITE,TFT_BLACK);  tft.setTextSize(1);
  // We can now plot text on screen using the "print" class
  tft.println("Hello World!");

  // Set the font colour to be yellow with no background, set to font 7
  tft.setTextColor(TFT_YELLOW,TFT_BLACK); tft.setTextFont(7);
  tft.println(1234.56);

  // Set the font colour to be green with black background, set to font 4
  tft.setTextColor(TFT_GREEN,TFT_BLACK);
  tft.setTextFont(4);
  tft.println("Groop");
  tft.println("I implore thee,");

  // Change to font 2
  tft.setTextFont(2);
  tft.println("my foonting turlingdromes.");
  tft.println("And hooptiously drangle me");
  tft.println("with crinkly bindlewurdles,");
  // This next line is deliberately made too long for the display width to test
  // automatic text wrapping onto the next line
  tft.println("Or I will rend thee in the gobberwarts with my blurglecruncheon, see if I don't!");

  // Test some print formatting functions
  float fnumber = 123.45;
   // Set the font colour to be blue with no background, set to font 4
  tft.setTextColor(TFT_BLUE);    tft.setTextFont(4);
  tft.print("Float = "); tft.println(fnumber);           // Print floating point number
  tft.print("Binary = "); tft.println((int)fnumber, BIN); // Print as integer value in binary
  tft.print("Hexadecimal = "); tft.println((int)fnumber, HEX); // Print as integer number in Hexadecimal
  delay(10000);
}

文档信息

版权声明:可自由转载(请注明转载出处)-非商用-非衍生

发表时间:2023年5月26日 16:37