Arduino 2.4寸直插TFT彩色液晶触摸屏模块(ILI9431)

Arduiuo 2.4寸直插TFT彩色液晶触摸屏模块(ILI9431)

实验效果

简单实现中文字模提取的数据显示,字体各种参数设置,排版功能。

元件说明

2.4-tft-uno-display

产品特点 :

  • 支持ArduinoUNO 和Mega2560等开发板直插使用,无需接线,简单方便
  • 320X240分辨率,显示效果清晰,支持触摸功能
  • 支持16位RGB 65K颜色显示,显示色彩丰富
  • 采用8位并行总线,比串口SPI刷新快
  • 板载 5V/3.3V 电平转换 IC,兼容 5V/3.3V 工作电压
  • 带SD卡槽方便扩展实验
  • 提供Arduino库,提供丰富的示例程序
  • 军工级工艺标准,长期稳定工作
  • 提供底层驱动技术支持
名称 参数
显示颜色 RGB 65K彩色
SKU MAR2406
尺寸 2.4(inch)
类型 TFT
驱动芯片 ILI9341
分辨率 320*240 (Pixel)
模块接口 8-bit parallel interface
有效显示区域 48.96*36.72(mm)
模块PCB尺寸 72.20*52.7(mm)
工作温度 -20℃~60℃
存储温度 -30℃~70℃
工作电压 5V/3.3V
产品重量(含包装) 39(g)

引脚说明

Pin Label Pin Description
LCD_RST LCD bus reset signal, low level reset
LCD总线复位信号,低电平复位
LCD_CS LCD bus chip select signal, low level enable
LCD总线片选信号,低电平使能
LCD_RS LCD bus command / data selection signal,low level: command, high level: data
LCD总线命令/数据选择信号,低电平:命令,高电平:数据
LCD_WR LCD bus write signal
LCD总线写信号
LCD_RD LCD bus read signal
LCD总线写信号
GND Power ground
电源地
5V 5V power input
5V电源输入
3V3 3.3V power input, this pin can be disconnected
3.3V电源输入,此引脚可不接
LCD_D0 LCD 8-bit data Bit0
LCD 8位数据Bit0
LCD_D1 LCD 8-bit data Bit1
LCD 8位数据Bit1
LCD_D2 LCD 8-bit data Bit2
LCD 8位数据Bit2
LCD_D3 LCD 8-bit data Bit3
LCD 8位数据Bit3
LCD_D4 LCD 8-bit data Bit4
LCD 8位数据Bit4
LCD_D5 LCD 8-bit data Bit5
LCD 8位数据Bit5
LCD_D6 LCD 8-bit data Bit6
LCD 8位数据Bit6
LCD_D7 LCD 8-bit data Bit7
LCD 8位数据Bit7
SD_SS SD card SPI bus chip select signal, low level enable
SD卡SPI总线片选信号,低电平使能
SD_DI SD card SPI bus MOSI signal
SD卡SPI总线MOSI信号
SD_DO SD card SPI bus MISO signal
SD卡SPI总线MISO信号
SD_SCK SD card SPI bus clock signal
SD卡SPI总线时钟信号

BOM表

名称 数量
Arduino Uno x1
2.4寸直插TFT彩色液晶触摸屏模块(ILI9431) x1

接线方式

直接对准Arduino Uno 或者Arduino Mega2560 对接,如图:

2.4-tft-uno-display-wiring

程序提点

下载字体取模软件

本示例中用到的 「字体取模」软件(只能在WIN系统使用)下载地址如下:

城通网盘:https://u16460183.ctfile.com/fs/16460183-296052219

安装MCUFRIEND_kbv库

MCUFRIEND_kbv是我测试过,刷新率比较快的一个库,就是为这样的直插的屏幕而做的。

下载地址:https://github.com/prenticedavid/MCUFRIEND_kbv

下载后,解压,把文件放进Arduino IDE 的 library文件夹中即可。

** 曾测试在Arduino IDE 中的「库管理」中查找并安装,但安装后无法使用(也许是虚拟机的问题或者什么的),非常玄学。这样的安装方式是最简单的,大家可以先试试在「库管理」中安装。

tft.drawBitmap()函数

主要说一下个这个函数可以绘制BItmap图片取模的数据,例子中取模中文字也是一样的原理,把单个文字转成bitmap图片然后再取模,得到数据

tft.drawBitmap(x, y, data, width, high, color);

x: 在屏幕的坐标x绘制 y:在屏幕的坐标y绘制 data:取模数据 width:bitmap的宽度 high:bitmap的高度 color:设置显示的颜色

程序代码

// welcome  to lingshunlab.com

#include <Adafruit_GFX.h>    // Core graphics library
#include <MCUFRIEND_kbv.h>   // Hardware-specific library
MCUFRIEND_kbv tft; // 定义名为 tft 的 MCUFRIEND_kbv 实例

#include <Fonts/FreeSans9pt7b.h> // 加载字体
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>

#include <FreeDefaultFonts.h>

#define BLACK   0x0000 // 定义颜色 黑色
#define BLUE    0x001F // 定义颜色 蓝色
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

// 复制字模数据
// 中文字模 凌
// 字体大小40x60像素
static const unsigned char PROGMEM ling[] = { 
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x30,0x00,0x0F,
  0x00,0x00,0x78,0x00,0x0F,0x00,0x00,0x78,0x00,0x0F,0x00,0x00,0x7C,0x3F,0xFF,0xFF,
  0x80,0x3E,0x3F,0xFF,0xFF,0x80,0x1E,0x1F,0xFF,0xFF,0x80,0x1F,0x00,0x0F,0x00,0x00,
  0x0F,0x80,0x0F,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0xC0,0x07,0x7F,0xFF,0xFF,0xC0,0x00,
  0x7F,0xFF,0xFF,0xC0,0x00,0x01,0x80,0x60,0x00,0x00,0x03,0xC0,0x78,0x00,0x00,0x0F,
  0xC0,0xFE,0x00,0x00,0x1F,0x80,0x7F,0x80,0x00,0x7F,0xF0,0x1F,0xE0,0x0E,0xFD,0xF0,
  0x07,0xC0,0x0F,0xF9,0xFF,0xFD,0xC0,0x0F,0x63,0xFF,0xFE,0x00,0x0F,0x07,0xFF,0xFE,
  0x00,0x1F,0x0F,0xC0,0x3C,0x00,0x1E,0x1F,0xE0,0x7C,0x00,0x1E,0x7F,0xF0,0xF8,0x00,
  0x3E,0xFC,0xF9,0xF0,0x00,0x3E,0x78,0x7F,0xE0,0x00,0x3C,0x30,0x3F,0xC0,0x00,0x7C,
  0x00,0x1F,0x80,0x00,0x7C,0x00,0xFF,0xE0,0x00,0x78,0x03,0xFF,0xFC,0x00,0xF8,0x3F,
  0xF1,0xFF,0xC0,0x78,0xFF,0xC0,0x7F,0xE0,0x38,0xFF,0x00,0x0F,0xC0,0x00,0x70,0x00,
  0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,
};

// 中文字模 顺
// 字体大小40x60像素
static const unsigned char PROGMEM shun[] = { 
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x3C,0x1E,0x00,
  0x00,0x00,0x3D,0xFF,0xFF,0xFF,0xE0,0x3D,0xFF,0xFF,0xFF,0xE0,0x3D,0xFE,0xFF,0xFF,
  0xE0,0x3D,0xFE,0x03,0xE0,0x00,0x3D,0xFE,0x03,0xC0,0x00,0x3D,0xFE,0x03,0xC0,0x00,
  0x3D,0xFE,0x7F,0xFF,0x80,0x3D,0xFE,0x7F,0xFF,0xC0,0x3D,0xFE,0x7F,0xFF,0xC0,0x3D,
  0xFE,0x78,0x03,0xC0,0x3D,0xFE,0x78,0x03,0xC0,0x3D,0xFE,0x79,0xE3,0xC0,0x3D,0xFE,
  0x79,0xE3,0xC0,0x3D,0xFE,0x79,0xE3,0xC0,0x3D,0xFE,0x79,0xE3,0xC0,0x3D,0xFE,0x79,
  0xE3,0xC0,0x3D,0xFE,0x79,0xE3,0xC0,0x3D,0xFE,0x79,0xE3,0xC0,0x3D,0xFE,0x79,0xE3,
  0xC0,0x3D,0xFE,0x79,0xE3,0xC0,0x3D,0xFE,0x79,0xE3,0xC0,0x3D,0xFE,0x79,0xE3,0xC0,
  0x39,0xFE,0x7B,0xC3,0xC0,0x79,0xFE,0x7B,0xE3,0xC0,0x79,0xFE,0x07,0xF8,0x00,0x79,
  0xFE,0x0F,0xFC,0x00,0x79,0xFE,0x1F,0xFF,0x00,0xF1,0xFE,0x3F,0x1F,0x80,0xF1,0xFE,
  0xFE,0x0F,0xC0,0xF0,0x1F,0xFC,0x07,0xE0,0xE0,0x0F,0xF0,0x01,0xE0,0x60,0x00,0xE0,
  0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,
};

// 中文字模 实
// 字体大小40x60像素
static const unsigned char PROGMEM shi_1[] = { 
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xE0,0x00,0x00,0x00,0x01,0xF0,
  0x00,0x00,0x00,0x01,0xF0,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x3F,0xFF,0xFF,0xFF,
  0x80,0x7F,0xFF,0xFF,0xFF,0xC0,0x7F,0xFF,0xFF,0xFF,0xC0,0x7F,0xFF,0xFF,0xFF,0xC0,
  0x78,0x00,0x1C,0x03,0xC0,0x78,0xF0,0x3E,0x03,0xC0,0x79,0xFC,0x3E,0x03,0xC0,0x00,
  0xFE,0x3E,0x00,0x00,0x00,0x3F,0xBE,0x00,0x00,0x00,0x1F,0xBE,0x00,0x00,0x07,0x07,
  0x3E,0x00,0x00,0x0F,0xC0,0x3E,0x00,0x00,0x0F,0xF0,0x3E,0x00,0x00,0x03,0xFC,0x3C,
  0x00,0x00,0x00,0xFC,0x3C,0x00,0x00,0x00,0x38,0x3C,0x00,0x00,0x00,0x00,0x3C,0x00,
  0x00,0xFF,0xFF,0xFF,0xFF,0xE0,0xFF,0xFF,0xFF,0xFF,0xE0,0xFF,0xFF,0xFF,0xFF,0xE0,
  0x00,0x01,0xF0,0x00,0x00,0x00,0x03,0xFC,0x00,0x00,0x00,0x07,0xFF,0x80,0x00,0x00,
  0x1F,0xFF,0xE0,0x00,0x00,0x7F,0x8F,0xF8,0x00,0x03,0xFE,0x03,0xFE,0x00,0x3F,0xFC,
  0x00,0xFF,0x80,0xFF,0xE0,0x00,0x1F,0xC0,0x7F,0x80,0x00,0x07,0xC0,0x78,0x00,0x00,
  0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,
};

// 中文字模 验
// 字体大小40x60像素
static const unsigned char PROGMEM yan[] = {
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x07,
  0x80,0x00,0xFF,0xF0,0x0F,0xC0,0x00,0xFF,0xF0,0x0F,0xE0,0x00,0xFF,0xF0,0x1F,0xE0,
  0x00,0x00,0xF0,0x3E,0xF0,0x00,0x3C,0xF0,0x7C,0xF8,0x00,0x3C,0xF0,0x78,0x7C,0x00,
  0x3C,0xF0,0xF8,0x3E,0x00,0x3C,0xF1,0xF0,0x1F,0x00,0x3C,0xF3,0xE0,0x0F,0xC0,0x3C,
  0xEF,0xC0,0x07,0xE0,0x3D,0xFF,0xFF,0xFF,0xE0,0x39,0xEF,0xFF,0xFF,0xC0,0x79,0xE6,
  0xFF,0xFE,0x00,0x7F,0xF8,0x00,0x00,0x00,0x7F,0xF8,0x07,0x00,0x00,0x7F,0xF8,0x0F,
  0x07,0x80,0x3F,0xFB,0xCF,0x0F,0x80,0x00,0x7B,0xCF,0x0F,0x00,0x00,0x7B,0xE7,0x8F,
  0x00,0x01,0xF9,0xE7,0x9F,0x00,0x3F,0xF9,0xE7,0x9E,0x00,0xFF,0xF9,0xF7,0x9E,0x00,
  0xFF,0x78,0xF3,0xFC,0x00,0xF0,0x78,0xF3,0xFC,0x00,0x00,0x78,0xF0,0x3C,0x00,0x00,
  0x78,0x40,0x78,0x00,0x00,0x78,0x00,0x78,0x00,0x00,0xFF,0xFF,0xFF,0xC0,0x3F,0xFF,
  0xFF,0xFF,0xE0,0x3F,0xFF,0xFF,0xFF,0xE0,0x3F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,
};

// 中文字模 室
// 字体大小40x60像素
static const unsigned char PROGMEM shi_2[] = {
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x03,0xE0,0x00,0x00,0x00,0x01,0xE0,
  0x00,0x00,0x00,0x01,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x7F,0xFF,0xFF,0xFF,
  0xC0,0x7F,0xFF,0xFF,0xFF,0xC0,0x7F,0xFF,0xFF,0xFF,0xC0,0x78,0x00,0x00,0x03,0xC0,
  0x78,0x00,0x00,0x03,0xC0,0x78,0x00,0x00,0x03,0xC0,0x7F,0xFF,0xFF,0xFF,0xC0,0x07,
  0xFF,0xFF,0xFC,0x00,0x07,0xFF,0xFF,0xFC,0x00,0x00,0x3F,0x01,0x80,0x00,0x00,0xFC,
  0x03,0xE0,0x00,0x01,0xF8,0x03,0xF0,0x00,0x07,0xF0,0x07,0xF8,0x00,0x0F,0xFF,0xFF,
  0xFC,0x00,0x0F,0xFF,0xFF,0xFE,0x00,0x07,0xFF,0xFF,0xFF,0x00,0x02,0x01,0xF0,0x1E,
  0x00,0x00,0x01,0xF0,0x0C,0x00,0x00,0x01,0xF0,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0x00,
  0x1F,0xFF,0xFF,0xFF,0x00,0x1F,0xFF,0xFF,0xFF,0x00,0x00,0x01,0xF0,0x00,0x00,0x00,
  0x01,0xF0,0x00,0x00,0x00,0x01,0xF0,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xC0,0xFF,0xFF,
  0xFF,0xFF,0xE0,0xFF,0xFF,0xFF,0xFF,0xE0,0x7F,0xFF,0xFF,0xFF,0xC0,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,
};

void setup(void) {
  Serial.begin(9600);
  uint16_t ID = tft.readID(); // 获取 tft ID
  Serial.println("Example: Font_simple");
  Serial.print("found ID = 0x");
  Serial.println(ID, HEX);
  if (ID == 0xD3D3) ID = 0x9481; //force ID if write-only display
  tft.begin(ID); // 根据ID初始化屏幕

  tft.setRotation(1); // 设置屏幕方向
  tft.fillScreen(BLACK); // 设置全屏填充色
}

void loop(void) {
  uint16_t wid, ht, fontw;
  wid = tft.width(); // 获取屏幕宽度
  ht = tft.height(); // 获取屏幕高度
  fontw = 40; // 定义字体宽度
  // 绘制字模数据 X坐标 Y坐标 字模数据 字体宽度字体高度颜色
  tft.drawBitmap(60,    30,     ling,     40,      46,   YELLOW);  
  tft.drawBitmap(60+fontw, 30, shun, 40, 46, CYAN);
  tft.drawBitmap(60+fontw*2, 30, shi_1, 40, 46, MAGENTA);
  tft.drawBitmap(60+fontw*3, 30, yan, 40, 46, BLUE);
  tft.drawBitmap(60+fontw*4, 30, shi_2, 40, 46, RED);
  tft.setTextSize(1); // 设置字体大小
  tft.setCursor(0, 220); // 设置指针位置
  tft.setFont(&FreeSevenSegNumFont); // 设置字体
  tft.setTextColor(GREEN);  // 设置字体颜色
  tft.println("0123456789"); // 显示的内容

  tft.setFont(NULL); // 设置字体为NULL这样在同一行中切换可以完全覆盖
  tft.setTextSize(2); // 设置字体大小
  tft.setTextColor(YELLOW, BLACK); // 设置字体颜色和背景颜色
  tft.setCursor(60, 86); // 设置指针位置
  tft.println("  Hello World!!   "); // 显示的内容
  delay(1000); 
  tft.setCursor(60, 86); // 设置指针位置
  tft.println("LingShunLAB.com     "); // 显示的内容
  delay(1000);
}

参考:

http://www.lcdwiki.com/2.4inch_Arduino_Display