驱动 2.4'' TFT 串口触摸屏

最近一直在ESP8266上折腾一块从淘宝上找回来的2.4'' TFT 的串口触摸屏,分辨率是240x320的基于 ILI9341驱动,具体型号是 TJCTM24024-SPI 就是下面的这块。

驱动 2.4'' TFT 串口触摸屏_第1张图片
驱动 2.4'' TFT 串口触摸屏_第2张图片

之前买了一块3.2''的,到手后找线路和驱动时才知道是个并口的,要STM32才能正常驱动或者得去个转接块将并口转成SPI,真是坑死哥了!在Google上查了许久发现要在ESP8266上使用彩屏的TFT得选这个小的,找到后果断出手,到货后才发现还是和Google上能快速找到的ESP驱动的资料大相径庭,真是作啊~~~

原因是大多在网上找到的ESP8266接2.8''~2.2''的TFT都是些不带触摸的,问淘宝上的卖家也是一头雾水(丫的根本不懂知道自已卖的是啥)。皇天不负有心人,最终还是让哥给找到了方法!

先来讲讲线路与硬件的连接方法吧,我用的是NodeMCU 以下是它们的连接方式:

驱动 2.4'' TFT 串口触摸屏_第3张图片
MyTouchSPIShield.png

(因为找不到TJCTM24024-SPI的fzz文件,所以只能用找资料找到的连接图了,待以后找到了它的fzz再做一个像样的吧。)

  • AdaFruit_GFX 在Arduino IDE的库管理器中直接安装就行了。
  • XPT2046 这个是触摸屏的驱动,得释放到Arduino 的 library目录里面。
  • http://nailbuster.com/nailcode/tft28esp.zip 这个库是For ESP8266的TFT触摸驱动,也是释放到Arduino 的Library中。

固件

在上面的 tft28esp.zip 文件中有一些示例,我没有直接去写一些应用与示例而是拿现成的,因为被折腾太久了心急于试试这个屏的显示效果所以直接实行拿来主义,

以下是其中的一个触摸的示例,直接在Arduino IDE 打开上传到NodeMCU里面就可以跑了

#include 
#include 

#include 
#include 
#include 

// Modify the following two lines to match your hardware
// Also, update calibration parameters below, as necessary

// For the esp shield, these are the default.
#define TFT_DC 2
#define TFT_CS 15

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
XPT2046 touch(/*cs=*/ 4, /*irq=*/ 5);

Adafruit_GFX_Button button;

void setup() {
  delay(1000);
  
  Serial.begin(115200);
  SPI.setFrequency(ESP_SPI_FREQ);

  tft.begin();
  touch.begin(tft.width(), tft.height());  // Must be done before setting rotation
  Serial.print("tftx ="); Serial.print(tft.width()); Serial.print(" tfty ="); Serial.println(tft.height());
  tft.fillScreen(ILI9341_BLACK);
  // Replace these for your screen module
  touch.setCalibration(209, 1759, 1775, 273);
  button.initButton(&tft, 100, 100, 70, 40, ILI9341_DARKCYAN, ILI9341_BLUE, ILI9341_GREENYELLOW, "Clear", 2);
  button.drawButton();
  
}

static uint16_t prev_x = 0xffff, prev_y = 0xffff;

void loop() {
    uint16_t x, y;
  if (touch.isTouching()) {
    touch.getPosition(x, y);
//  Serial.print("x ="); Serial.print(x); Serial.print(" y ="); Serial.println(y);
    if (prev_x == 0xffff) {
      tft.drawPixel(x, y,ILI9341_BLUE);
    } else {
      tft.drawLine(prev_x, prev_y, x, y,ILI9341_BLUE);
    }
    prev_x = x;
    prev_y = y;
  } else {
    prev_x = prev_y = 0xffff;
  }
  
  
  button.press(button.contains(x, y)); // tell the button it is pressed
  

// now we can ask the buttons if their state has changed
    if (button.justReleased()) {
        tft.fillScreen(ILI9341_BLACK);
        button.drawButton(); // draw normal
    }

    if (button.justPressed()) {
        button.drawButton(true); // draw invert!
    }

  delay(20);
}

运行效果如下:

更简单的办法

如果不想测试触摸功能,也可以直接用Adafruit 自带的示例:

驱动 2.4'' TFT 串口触摸屏_第4张图片

打开这个示例后记得要改一下Pin的声明,因为我用的是NodeMCU不是Uno:

// For the Adafruit shield, these are the default.
#define TFT_DC 2
#define TFT_CS 5

上传后就可以看到更丰富的测试的效果了:

总的来说,对这个屏还是挺满意的价格也只是36加上运费也就40来块钱的样子,显示速度很流畅,触摸也挺准确的是一个可用来做一些高端IoT项目的可选件。

其它参考

  • 2.8″ TOUCH LCD SPI FOR ESP8266 (NODEMCU)
  • TJCTM24024-SPI - ILI9341 - Arduino UNO R3 此板连接Uno的一个讨论分享帖

你可能感兴趣的:(驱动 2.4'' TFT 串口触摸屏)