摘要:本文主要介绍在Arduino IDE开发环境下,如何快速上手驱动不同的LCD屏幕;
一、关于 Arduino_GFX
在Arduino IDE开发环境中,驱动屏幕的相关库有:TFT_eSPI / Arduino_GFX 等;
目前TFT_eSPI库对新开发板(ESP32C/P4)等适配支持不是很好,而且更新速度也没有其它库快;
因此选择目前芯片支持相对比较多的Arduino_GFX库;
而且Arduino_GFX库相对更加 "简洁 / 清晰";
关于Arduino_GFX库
一个支持各种屏幕显示界面和数据总线接口的Arduino图形库:该库基于Adafruit_GFX、LovyanGFX、TFT_eSPI 和 Ucglib 等库重写。
支持:GC9A01 round display, HX8347C, ILI9225, ST7735, ST7789, ST7796等(软/硬SPI)以及ESP32(9-bit SPI / 8-bit / 16-bit并行接口)等;
支持:Arduino / ESP32 / Raspberry Pi Pico等系列开发板;

二、Arduino_GFX库安装
1、下载开发环境
下载开发环境 Arduino IDE;

2、安装库
1、安装Arduino_GFX库
在库管理中的名称叫【GFX Library for Arduino】

2、复制相关配置头文件
库路径:...\Arduino IDE\Documents\libraries\GFX_Library_for_Arduino\examples\PDQgraphicstest

将以下文件复制到自己的工程项目中


3、关于头文件功能
Arduino_GFX_dev_device.h:定义不同带屏的开发板默认配置选项;
例:WAVESHARE_ESP32_C6_* / ESP32_S3_BOX_3 / XIAO_ESP32C3_ROUND_DISPLAY 等开发板;
Arduino_GFX_pins.h:定义不同MCU默认相关引脚配置;
例:TFT_CS、TFT_DC、TFT_RST、GFX_BL等引脚的默认配置;
Arduino_GFX_databus.h:定义不同MCU的软/硬SPI(8/9/16位并行)接口配置选项;
(当屏幕没有某个引脚功能时,可以把对应宏设为 GFX_NOT_DEFINED)
例:
Arduino_DataBus *bus = new Arduino_HWSPI(TFT_DC, TFT_CS);
[Arduino_HWSPI(int8_t dc, int8_t cs = GFX_NOT_DEFINED, SPIClass *spi = &SPI, bool is_shared_interface = true)]
Arduino_DataBus *bus = new Arduino_SWSPI(TFT_DC, TFT_CS, 18 /* SCK */, 23 /* MOSI */, GFX_NOT_DEFINED /* MISO */);

Arduino_GFX_display.h:定义不同型号屏幕驱动的配置选项;
例:
Arduino_GFX *gfx = new Arduino_ILI9341(bus, TFT_RST, 0 /* rotation */, false /* IPS */);
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 0 /* rotation */, true /* IPS */, 172 /* width */, 320 /* height */, 34 /* col offset 1 */, 0 /* row offset 1 */, 34 /* col offset 2 */, 0 /* row offset 2 */);
三、使用方法

当前开发板为:WAVESHARE_ESP32_C6_LCD_1_47
开发方式一
指定开发板:#include "Arduino_GFX_dev_device.h"
(若当前的开发板在该头文件有定义:取消当前开发板注释即可)
例: #define WAVESHARE_ESP32_C6_LCD_1_47


示例代码:
#include <Arduino_GFX_Library.h>
// 开发板选择
#include "Arduino_GFX_dev_device.h"
#define GFX_BL 22 //背光控制引脚 (背光亮度控制【可选】)
void setup(void)
{
DEV_DEVICE_INIT(); // 指定开发板设备初始化
Serial.begin(115200);
while(!Serial);
Serial.println("指定开发板 使用方法");
// 显示初始化
if (!gfx->begin())
{
Serial.println("gfx->begin() failed!");
}
gfx->fillScreen(RGB565_BLACK);
#ifdef GFX_BL
pinMode(GFX_BL, OUTPUT);
analogWrite(GFX_BL, 32); // PWM 亮度调节
#endif
// 在指定位置生成红色 Hello World!
gfx->setCursor(50, 50);
gfx->setTextColor(RGB565_RED);
gfx->println("Hello World!");
delay(3000);
}
void loop()
{
// 每隔1s在屏幕上生成随机的 Hello World
gfx->setCursor(random(gfx->width()), random(gfx->height()));
gfx->setTextColor(random(0xffff), random(0xffff));
gfx->setTextSize(random(6), random(6), random(2));
gfx->println("Hello World!");
delay(1000);
}开发方式二
通用配置:其他开发MCU / 屏幕驱动
1、SPI引脚配置:Arduino_GFX_databus.h
在该头文件中,找到当前的开发芯片(例:ESP32C6);
Arduino_DataBus *bus = new Arduino_ESP32SPI(TFT_DC, TFT_CS, 21 /* SCK */, 19 /* MOSI */, GFX_NOT_DEFINED /* MISO */, FSPI /* spi_num */);
若没有当前的开发芯片,则使用通用SPI接口:
硬件:Arduino_DataBus *bus = new Arduino_HWSPI(TFT_DC, TFT_CS); 【接口为芯片默认硬件&SPI接口】
(Arduino_HWSPI(int8_t dc, int8_t cs = GFX_NOT_DEFINED, SPIClass *spi = &SPI, bool is_shared_interface = true);
软件:Arduino_DataBus *bus = new Arduino_SWSPI(TFT_DC, TFT_CS, 18 /* SCK */, 23 /* MOSI */, GFX_NOT_DEFINED /* MISO */);


将相关参数替换为当前屏幕连接的引脚
例:
TFT_CS:14
TFT_DC:15
TFT_MOSI:6
TFT_MISO:GFX_NOT_DEFINED
TFT_SCK:7
Arduino_DataBus *bus = new Arduino_ESP32SPI(15 /* DC */, 14 /* CS */, 7 /* SCK */, 6 /* MOSI */, GFX_NOT_DEFINED /* MISO */, FSPI);

2、屏幕驱动配置:Arduino_GFX_display.h
在该头文件中,找到当前屏幕的驱动配置(例:ST7789_1.47);
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 0 /* rotation */, true /* IPS */, 172 /* width */, 320 /* height */, 34 /* col offset 1 */, 0 /* row offset 1 */, 34 /* col offset 2 */, 0 /* row offset 2 */);

将TFT_RST参数替换为实际的引脚
例:TFT_RST:21
Arduino_GFX *gfx = new Arduino_ST7789(bus, 21 /* RST */, 0 /* rotation */, true /* IPS */, 172 /* width */, 320 /* height */,34 /* col offset 1 */, 0 /* row offset 1 */, 34 /* col offset 2 */, 0 /* row offset 2 */);
示例代码:
#include <Arduino_GFX_Library.h>
#define GFX_BL 22 //屏幕背光引脚 (背光亮度控制 / 可选)
// 1、SPI引脚配置
Arduino_DataBus *bus = new Arduino_ESP32SPI(15 /* DC */, 14 /* CS */, 7 /* SCK */, 6 /* MOSI */,
GFX_NOT_DEFINED /* MISO */, FSPI);
// 2、屏幕驱动配置
Arduino_GFX *gfx = new Arduino_ST7789(
bus, 21 /* RST */, 0 /* rotation */, true /* IPS */, 172 /* width */, 320 /* height */,
34 /* col offset 1 */, 0 /* row offset 1 */, 34 /* col offset 2 */, 0 /* row offset 2 */);
void setup(void)
{
Serial.begin(115200);
while(!Serial);
Serial.println("自定义开发板 使用方法");
// 显示初始化
if (!gfx->begin())
{
Serial.println("gfx->begin() failed!");
}
gfx->fillScreen(RGB565_BLACK);
#ifdef GFX_BL
pinMode(GFX_BL, OUTPUT);
analogWrite(GFX_BL, 32); // PWM 亮度调节
#endif
// 在指定位置生成红色 Hello World!
gfx->setCursor(50, 50);
gfx->setTextColor(RGB565_RED);
gfx->println("Hello World!");
delay(3000);
}
void loop()
{
// 每隔1s在屏幕上生成随机的 Hello World
gfx->setCursor(random(gfx->width()), random(gfx->height()));
gfx->setTextColor(random(0xffff), random(0xffff));
gfx->setTextSize(random(6), random(6), random(2));
gfx->println("Hello World!");
delay(1000);
}四、效果演示
在屏幕界面上 显示不同效果的“Hello World”字符;


我要赚赏金
