2.8寸TFT ILI9341 多平台代码例程

知识库
知识库文档
/firmware/显示屏/2.8寸 TFT ILI9341/2.8寸TFT ILI9341 多平台代码例程.md

文档

2.8寸TFT ILI9341 多平台代码例程


Arduino UNO (Adafruit ILI9341 + 触摸)

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>

#define TFT_CS   10
#define TFT_DC   9
#define TFT_RST  8
#define TOUCH_CS 5
#define TOUCH_IRQ 2

Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
XPT2046_Touchscreen touch(TOUCH_CS, TOUCH_IRQ);

void setup() {
  Serial.begin(9600);
  tft.begin();
  tft.setRotation(1);
  touch.begin();
  drawUI();
}

void drawUI() {
  tft.fillScreen(ILI9341_BLACK);
  tft.fillRect(0, 0, 240, 40, ILI9341_NAVY);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.setCursor(50, 10);
  tft.println("2.8 TFT");
  
  // 按钮区域
  tft.fillRoundRect(20, 120, 80, 60, 8, ILI9341_GREEN);
  tft.fillRoundRect(140, 120, 80, 60, 8, ILI9341_RED);
  tft.setTextSize(1);
  tft.setCursor(36, 145); tft.print("ON");
  tft.setCursor(156, 145); tft.print("OFF");
}

void loop() {
  if (touch.touched()) {
    TS_Point p = touch.getPoint();
    // 映射坐标 (触摸原始坐标→屏幕坐标)
    uint16_t x = map(p.x, 250, 3800, 0, 240);
    uint16_t y = map(p.y, 350, 3700, 0, 320);
    
    tft.setCursor(0, 290);
    tft.fillRect(0, 290, 120, 20, ILI9341_BLACK);
    tft.printf("X:%d Y:%d", x, y);
    
    // 判断按钮区域
    if(x>20 && x<100 && y>120 && y<180)
      tft.fillScreen(ILI9341_GREEN);
    else if(x>140 && x<220 && y>120 && y<180)
      tft.fillScreen(ILI9341_RED);
  }
}

ESP32 (TFT_eSPI + 触摸)

#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();

// 触摸坐标(需校准参数)
uint16_t calData[5] = { 350, 3500, 250, 3800, 1 };
#define TOUCH_CS 33

void setup() {
  Serial.begin(115200);
  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  
  // 配色仪表盘
  tft.fillRoundRect(10, 10, 220, 100, 15, TFT_DARKGREY);
  tft.drawRoundRect(10, 10, 220, 100, 15, TFT_WHITE);
  tft.setTextColor(TFT_CYAN, TFT_DARKGREY);
  tft.drawString("Temperature", 20, 25, 2);
  tft.setTextColor(TFT_WHITE, TFT_DARKGREY);
  tft.drawString("25.6 C", 20, 55, 4);
  tft.setTextColor(TFT_GREEN, TFT_DARKGREY);
  tft.drawString("Humidity: 48%", 20, 85, 2);
}

void loop() {
  uint16_t x, y;
  if (tft.getTouch(&x, &y)) {
    Serial.printf("Touch: %d, %d\n", x, y);
    tft.fillCircle(x, y, 3, TFT_RED);
    delay(50);
  }
}

STM32 + 触摸校准流程

// XPT2046 读取原始ADC值
uint16_t xpt2046_read(uint8_t cmd) {
    uint16_t val;
    XPT_CS_L();
    HAL_SPI_TransmitReceive(&hspi1, &cmd, (uint8_t*)&val, 1, 100);
    // 发送0x00接收第二个字节
    uint8_t dummy = 0x00, high, low;
    HAL_SPI_TransmitReceive(&hspi1, &dummy, &high, 1, 100);
    HAL_SPI_TransmitReceive(&hspi1, &dummy, &low, 1, 100);
    XPT_CS_H();
    return ((high << 8) | low) >> 3; // 12-bit
}

// 三点校准法
typedef struct {
    float alpha_x, beta_x, delta_x;
    float alpha_y, beta_y, delta_y;
} TouchCalibration;

TouchCalibration calibrate_touch(void) {
    // 在屏幕三个已知点采样,解线性方程组...
    // 返回映射系数
}

MicroPython (全功能)

from ili9341 import ILI9341
from xpt2046 import XPT2046
from machine import Pin, SPI, PWM
import time

spi_tft = SPI(1, baudrate=40000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
spi_touch = SPI(1, baudrate=2000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))

tft = ILI9341(spi_tft, cs=Pin(15), dc=Pin(2), rst=Pin(4), w=240, h=320)
touch = XPT2046(spi_touch, cs=Pin(33), irq=Pin(36))

# 背光PWM
bl = PWM(Pin(5), freq=5000, duty=512)

tft.erase()
tft.set_font(tft.FONT_DejaVu18)

# 绘制UI
tft.fill_rectangle(0, 0, 240, 50, 0x000F)  # 标题栏
tft.set_pos(30, 12)
tft.print("2.8 ILI9341")

# 触摸循环
while True:
    if touch.is_pressed():
        x, y = touch.get_position()
        tft.fill_circle(x, y, 4, 0xF800)  # 红点标记
        print(f"({x},{y})")
    time.sleep_ms(50)

信息

路径
/firmware/显示屏/2.8寸 TFT ILI9341/2.8寸TFT ILI9341 多平台代码例程.md
更新时间
2026/5/26