关于屏幕
安装库
TFT_eSPI U8g2
线路连接
配置TFT_eSPI库文件
修改TFT_eSPI目录下User_Setup.h
文件,内容如下:
#define USER_SETUP_INFO "User_Setup"
#define ST7789_DRIVER
// 设置显示屏的分辨率
#define TFT_WIDTH 240
#define TFT_HEIGHT 240
// 定义ESP8266的引脚连接
#define TFT_MISO -1 // 不使用MISO
#define TFT_MOSI 13 // GPIO13 (D7)
#define TFT_SCLK 14 // GPIO14 (D5)
#define TFT_CS 15 // GPIO15 (D8)
#define TFT_DC 2 // GPIO2 (D4)
#define TFT_RST 4 // GPIO4 (D2)
#define TFT_BL 5 // GPIO5 (D1) - 背光控制
// 其他配置
#define LOAD_GLCD
#define LOAD_FONT2
#define LOAD_FONT4
#define LOAD_FONT6
#define LOAD_FONT7
#define LOAD_FONT8
#define LOAD_GFXFF
#define SMOOTH_FONT
// 色彩深度设置
#define SPI_FREQUENCY 27000000
#define SPI_READ_FREQUENCY 20000000
#define SPI_TOUCH_FREQUENCY 2500000
ESP8266上传代码
#include <TFT_eSPI.h>
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI();
void setup() {
Serial.begin(115200);
// 初始化显示屏
tft.init();
tft.setRotation(0); // 设置屏幕方向
tft.fillScreen(TFT_BLACK); // 清屏,设置为黑色背景
// 设置文本颜色和大小
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(3);
// 显示文本
tft.setCursor(50, 100);
tft.println("Hello World");
}
void loop() {
// 主循环为空
}
显示图形
#include<TFT_eSPI.h>
#include<SPI.h>
TFT_eSPI tft = TFT_eSPI();
voidsetup(){
Serial.begin(115200);
Serial.println("Starting...");
// 初始化显示屏
tft.init();
tft.setRotation(0);
// 打开背光
pinMode(5, OUTPUT); // BLK引脚
digitalWrite(5, HIGH);
// 清屏,设置为黑色背景
tft.fillScreen(TFT_BLACK);
// 设置文本颜色和大小
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(2);
// 显示文本
tft.setCursor(50, 100);
tft.println("Hello World!");
// 测试显示效果
testDisplay();
}
voidloop(){
// 主循环为空
}
// 测试显示函数
voidtestDisplay(){
// 显示一些基本图形以测试显示是否正常
tft.drawRect(10, 10, 50, 50, TFT_RED);
tft.fillRect(70, 10, 50, 50, TFT_GREEN);
tft.drawCircle(150, 35, 25, TFT_BLUE);
// 显示一些文本
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setCursor(10, 150);
tft.println("Test Pattern");
// 在串口输出调试信息
Serial.println("Display test completed");
}
显示汉字
需要利用字模软件,提取汉字编码。
#include<TFT_eSPI.h>
#include<SPI.h>
TFT_eSPI tft = TFT_eSPI();
// 定义16x16点阵的汉字字模数据
// 逍
constunsignedchar str_xiao[] = {
0x00,0x40,0x22,0x48,0x11,0x48,0x11,0x50,0x00,0x40,0x03,0xF8,0xF2,0x08,0x12,0x08,
0x13,0xF8,0x12,0x08,0x12,0x08,0x13,0xF8,0x12,0x08,0x12,0x28,0x2A,0x10,0x47,0xFE
};
// 遥
constunsignedchar str_yao[] = {
0x00,0x3C,0x23,0xC0,0x10,0x04,0x12,0x44,0x01,0x28,0x01,0xFC,0xF2,0x20,0x10,0x20,
0x13,0xFE,0x10,0x20,0x11,0x24,0x11,0x24,0x11,0x24,0x11,0xFC,0x28,0x00,0x47,0xFE};
// 子
constunsignedchar str_zi[] = {
0x00,0x00,0x7F,0xF8,0x00,0x10,0x00,0x20,0x00,0x40,0x01,0x80,0x01,0x00,0xFF,0xFE,
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00
};
// 大
constunsignedchar str_da[] = {
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,
0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06
};
// 表
constunsignedchar str_biao[] = {
0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,
0xFF,0xFE,0x05,0x00,0x08,0x88,0x18,0x50,0x28,0x20,0xC9,0x18,0x0A,0x06,0x0C,0x00
};
// 哥
constunsignedchar str_ge[] = {
0x00,0x00,0x7F,0xFC,0x00,0x10,0x1F,0x90,0x10,0x90,0x10,0x90,0x1F,0x90,0x00,0x00,
0xFF,0xFE,0x00,0x10,0x1F,0x90,0x10,0x90,0x10,0x90,0x1F,0x90,0x00,0x50,0x00,0x20
};
// 显示16x16的汉字(支持缩放)
voidshowChineseScaled(int32_t x, int32_t y, constunsignedchar *c, uint16_t color, uint8_t size){
int i, j;
for(j = 0; j < 16; j++) {
for(i = 0; i < 8; i++) {
if(c[j * 2] & (0x80 >> i)) {
tft.fillRect(x + i*size, y + j*size, size, size, color);
}
if(c[j * 2 + 1] & (0x80 >> i)) {
tft.fillRect(x + (i+8)*size, y + j*size, size, size, color);
}
}
}
}
voidsetup(){
Serial.begin(115200);
Serial.println("Starting...");
// 初始化显示屏
tft.init();
tft.setRotation(0);
// 打开背光
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
// 清屏,设置为黑色背景
tft.fillScreen(TFT_BLACK);
// 显示中文,每行两个字,不同颜色和大小
uint8_t size = 3; // 设置缩放倍数,可以随时调整
int spacing = 16 * size + 10; // 字间距
// 第一行
showChineseScaled(20, 20, str_xiao, TFT_RED, size); // 小-红色
showChineseScaled(20 + spacing, 20, str_yao, TFT_GREEN, size); // 要-绿色
// 第二行
showChineseScaled(20, 20 + spacing, str_zi, TFT_BLUE, size); // 字-蓝色
showChineseScaled(20 + spacing, 20 + spacing, str_da, TFT_YELLOW, size); // 大-黄色
// 第三行
showChineseScaled(20, 20 + spacing*2, str_biao, TFT_MAGENTA, size); // 表-紫色
showChineseScaled(20 + spacing, 20 + spacing*2, str_ge, TFT_CYAN, size); // 格-青色
}
voidloop(){
// 主循环为空
}
推荐站内搜索:最好用的开发软件、免费开源系统、渗透测试工具云盘下载、最新渗透测试资料、最新黑客工具下载……
还没有评论,来说两句吧...