ArduinoTFTLCD應用
- ArduinoTFTLCD應用
- 硬件連接
- 軟件
- 導入庫
- 顯示數字、字符
- 顯示漢字
- 方案1
- 方案2
- 顯示圖片
- 總結
ArduinoTFTLCD應用
對于手工喜歡DIY的人來說,Arduino驅動的TFTLCD被很多人使用,此處就總結一下,使用的是VScode的PlatformIO插件驅動的Arduino,芯片使用的是ESP32。
硬件連接
這里采用的是10PIN的ST7789驅動的TFTLCD屏幕,是硬件SPI驅動的。
????????TFTLCD硬件管腳
????????ESP32硬件管腳
我們連接的是ESP32的HSPI
引腳。
功能 | TFTLCD引腳 | ESP32引腳 |
---|---|---|
數據輸出 | MOSI | 13 |
時鐘 | SCLK | 14 |
片選 | CS | 15 |
選擇端口 | DC | 2 |
復位引腳 | RST | 4 |
背光正 | BL | 12 |
電源地 | GND | GND |
背光負 | GND | GND |
電壓正 | VCC | VCC |
有些SPI是這樣標注的,引腳都是一樣的。
字符1 | 字符2 | 功能 |
---|---|---|
CS | CS | 片選,低電平使能 |
DC | RS | 數據/命令選擇端口,DC端口為低時,寫入的是命令;為高時,寫入的是數據 |
RST | RESET | 復位信號,低電平復位 |
MISO | SDA | 從設備數據輸出 |
MOSI | SDI | 主設備數據輸出 |
SCLK | SCL | 時鐘,由主設備發出 |
按照如圖所示進行硬件連接。
軟件
????????使用的是VScode的PlatformIO插件驅動的Arduino,芯片使用的是ESP32。
導入庫
????????安裝TFT_eSPI
庫:TFT_eSPI 是一個用于 ESP32 和 ESP8266 上的基于圖形顯示控制器的開源軟件庫。它提供了一種簡單且高效的方式來與 TFT LCD 顯示器進行交互并創建令人驚嘆的圖形界面。
在左側文件目錄中\.pio\libdeps\esp32dev\TFT_eSPI\User_Setup.h
文件中。
驅動要選擇
// Only define one driver, the other ones must be commented out
//#define ILI9341_DRIVER // Generic driver for common displays
//#define ILI9341_2_DRIVER // Alternative ILI9341 driver, see https://github.com/Bodmer/TFT_eSPI/issues/1172
//#define ST7735_DRIVER // Define additional parameters below for this display
//#define ILI9163_DRIVER // Define additional parameters below for this display
//#define S6D02A1_DRIVER
//#define RPI_ILI9486_DRIVER // 20MHz maximum SPI
//#define HX8357D_DRIVER
//#define ILI9481_DRIVER
//#define ILI9486_DRIVER
//#define ILI9488_DRIVER // WARNING: Do not connect ILI9488 display SDO to MISO if other devices share the SPI bus (TFT SDO does NOT tristate when CS is high)
#define ST7789_DRIVER // Full configuration option, define additional parameters below for this display
//#define ST7789_2_DRIVER // Minimal configuration option, define additional parameters below for this display
//#define R61581_DRIVER
//#define RM68140_DRIVER
//#define ST7796_DRIVER
//#define SSD1351_DRIVER
//#define SSD1963_480_DRIVER
//#define SSD1963_800_DRIVER
//#define SSD1963_800ALT_DRIVER
//#define ILI9225_DRIVER
//#define GC9A01_DRIVER
屏幕尺寸
// For ST7789, ST7735, ILI9163 and GC9A01 ONLY, define the pixel width and height in portrait orientation
// #define TFT_WIDTH 80
// #define TFT_WIDTH 128
// #define TFT_WIDTH 172 // ST7789 172 x 320
// #define TFT_WIDTH 170 // ST7789 170 x 320
#define TFT_WIDTH 240 // ST7789 240 x 240 and 240 x 320
// #define TFT_HEIGHT 160
// #define TFT_HEIGHT 128
// #define TFT_HEIGHT 240 // ST7789 240 x 240
#define TFT_HEIGHT 320 // ST7789 240 x 320
// #define TFT_HEIGHT 240 // GC9A01 240 x 240
IO口
// For ESP32 Dev board (only tested with GC9A01 display)
// The hardware SPI can be mapped to any pins
#define TFT_MOSI 13 // In some display driver board, it might be written as "SDA" and so on.
#define TFT_SCLK 14
#define TFT_CS 15 // Chip select control pin
#define TFT_DC 2 // Data Command control pin
#define TFT_RST 4 // Reset pin (could connect to Arduino RESET pin)
#define TFT_BL 12 // LED back-light
其他不做修改。
在main.app
文件中。導入頭文件在這里插入代碼片
。#include <TFT_eSPI.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI(); // 創建TFT對象
void setup()
{Serial.begin(115200);tft.init(); // 初始化// tft.fillScreen(TFT_BLACK); // 設置屏幕背景顏色// analogWrite(BLK, 150); // 調節屏幕亮度,0最亮,255最暗
}
void loop() {}
顯示數字、字符
tft.setTextColor(TFT_WHITE, TFT_BLACK); // 參數1:字體顏色,參數2:背景色tft.setTextFont(2); // 字體大小16*16
tft.println("Hello,world");
tft.drawString("I want to eat something", 0, 50, 2);tft.setTextFont(4); // 字體大小26*26
tft.drawNumber(1234, 0, 70);
tft.drawFloat(3.14159, 5, 0, 90);
顯示漢字
方案1
利用OLED顯示漢字方法,去驅動TFTLCD顯示漢字。利用
PCtoLCD2002.exe
進行取模,然后導入文件中,利用點陣進行顯示。如圖所示取模方式。
新建文件Chinese_32.h
并導入數組。
/***************************16*16的點陣字體取模方式:共陰——列行式——逆向輸出*********/
unsigned char character[] = "第二一三四五";
uint8_t F16x16[] ={0x08, 0x04, 0x93, 0x92, 0x96, 0x9A, 0x92, 0xFA, 0x94, 0x93, 0x92, 0x96, 0xFA, 0x02, 0x02, 0x00,0x40, 0x40, 0x47, 0x24, 0x24, 0x14, 0x0C, 0xFF, 0x04, 0x04, 0x24, 0x44, 0x24, 0x1C, 0x00, 0x00, // 第",0/* (16 X 16 , 宋體 )*/0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00,0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, // 二",2/* (16 X 16 , 宋體 )*/0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 一",1/* (16 X 16 , 宋體 )*/0x00, 0x04, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x04, 0x00, 0x00,0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, // 三",3/* (16 X 16 , 宋體 )*/0x00, 0xFC, 0x04, 0x04, 0x04, 0xFC, 0x04, 0x04, 0x04, 0xFC, 0x04, 0x04, 0x04, 0xFC, 0x00, 0x00,0x00, 0x7F, 0x28, 0x24, 0x23, 0x20, 0x20, 0x20, 0x20, 0x21, 0x22, 0x22, 0x22, 0x7F, 0x00, 0x00, // 四",4/* (16 X 16 , 宋體 )*/0x00, 0x02, 0x42, 0x42, 0x42, 0xC2, 0x7E, 0x42, 0x42, 0x42, 0x42, 0xC2, 0x02, 0x02, 0x00, 0x00,0x40, 0x40, 0x40, 0x40, 0x78, 0x47, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7F, 0x40, 0x40, 0x40, 0x00, // 五",5/* (16 X 16 , 宋體 )*/
};
在main.app函數
#include <TFT_eSPI.h>
#include "Chinese_32.h"TFT_eSPI tft = TFT_eSPI(); // 創建TFT對象
#include <TJpg_Decoder.h>#define BLK 5 // 亮度控制引腳//--------------------------------------------------------------
// Prototype : void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
// Calls :
// Parameters : x,y -- 起始點坐標(x:0~127, y:0~7); N:漢字在codetab.h中的索引
// Description : 顯示codetab.h中的漢字,16*16點陣
//--------------------------------------------------------------// 顯示漢字
void OLED_ShowCN(uint8_t x, uint8_t y, uint8_t no)
{uint8_t t, j;uint16_t k;for (t = 0; t < 16; t++){k = F16x16[no * 32 + t];for (j = 0; j < 8; j++){if ((k >> j) & 0x01)tft.drawPixel(x + t, y + j, TFT_WHITE);}}for (t = 0; t < 16; t++){k = F16x16[no * 32 + 16 + t];for (j = 0; j < 8; j++){if ((k >> j) & 0x01)tft.drawPixel(x + t, y + j + 8, TFT_WHITE);}}
}uint8_t OLED_findoneCN(uint8_t ch1, uint8_t ch2, uint8_t ch3)
{uint8_t j = 0;while (character[j] != '\0'){if (ch1 == character[j] && ch2 == character[j + 1] && ch3 == character[j + 2])return j / 3 + 1;j += 3;}return 0;
}void OLED_Show(unsigned char x, unsigned char y, char ch[])
{int j = 0, k;while (ch[j] != '\0'){Serial.println(ch[j]);if (ch[j] > 0x80) // 漢字{k = OLED_findoneCN(ch[j], ch[j + 1], ch[j + 2]);if (k != 0)OLED_ShowCN((j / 3 * 2) * 8 + x, y, k - 1);j += 3;}else // ASCLL{tft.drawString((String)ch[j], (j / 3 * 2) * 8 + x, y, 2);j++;}}
}void TFT_Init()
{tft.init(); // 初始化// tft.fillScreen(TFT_BLACK); // 設置屏幕背景顏色// analogWrite(BLK, 150); // 調節屏幕亮度,0最亮,255最暗
}void TFT_Display()
{tft.setTextColor(TFT_WHITE, TFT_BLACK); // 參數1:字體顏色,參數2:背景色tft.setTextFont(2); // 字體大小16*16tft.println("Hello,world");tft.drawString("I want to eat something", 0, 50, 2);tft.setTextFont(4); // 字體大小26*26tft.drawNumber(1234, 0, 70);tft.drawFloat(3.14159, 5, 0, 90);tft.setTextColor(TFT_WHITE, TFT_BLACK); // 參數1:字體顏色,參數2:背景色OLED_Show(0, 120, (char*)"第二一三四五");
}void setup()
{Serial.begin(115200);TFT_Init();TFT_Display();
}void loop() {}
如圖所示結果。
方案2
????????導入字庫。
目錄\.pio\libdeps\esp32dev\TFT_eSPI\Tools\Create_Smooth_Font
文件下有三個文件。
其中Create_font.pde
:代碼,通過該代碼來制作字庫文件。
FontFiles : 存放我們制作出來的字庫文件,制作出來后是vlw結尾的。
data : 存放我們的字體文件,用ttf結尾的。- 去https://processing.org/ 下載
processing
軟件(資料中都放有)。 - 去自己的電腦中C:\Windows\Font文件中找到你想要的字體,放到data文件夾下。
- 在https://www.osgeo.cn/app/sa906網站中,將需要轉化的漢字轉成unicode編碼,然后將
\u
轉化成0x
。 - 使用processing打開Create_font.pde文件。
- 修改以下內容:將需要轉化的的漢字的
unicode
編碼放到specificUnicodes
數組中。String fontName = "simkai"; // 你的字庫的文件名字。 在130行 String fontType = ".ttf"; //String fontType = ".otf";// Define the font size in points for the TFT_eSPI font file int fontSize = 20; // Font size to use in the Processing sketch display window that pops up (can be different to above) int displayFontSize = 20; static final int[] unicodeBlocks = {//0x0030, 0x0039, //Example custom range (numbers 0-9)//0x0041, 0x005A, //Example custom range (Upper case A-Z)//0x0061, 0x007A, //Example custom range (Lower case a-z) }; static final int[] specificUnicodes = { 0x56e0,0x6709,0x7740,0x4f60,0x8ddf,0x5728,0x4e00,0x8d77,0x6211,0x7231,0x683e,0x8c46,0x5f20,0x5b66,0x806a };
- 點擊運行,正確的話會彈出一個對話框,然后會在
FontFiles
文件夾中生成一個.h的字庫。 - 將生成的字庫文件放入
src
文件夾中。 - 然后在主函數中:
#include "KT_20_A.h" //導入字庫。 tft.loadFont(KT_20_A); //指定tft屏幕對象載入font_12字庫,KT_20_A為生成字庫的數組名字。 tft.drawString("武漢",0,0) //在坐標0,0位置處寫武漢2個字,就可以在tft顯示出來了。 tft.unloadFont(); //釋放字庫文件,節省資源。
顯示圖片
- 用
Image2Lcd
打開一張BMP或者JPG格式的圖片。 - 設置成如圖所示,點擊保存,注意輸出圖像位置的數字
(238,320)
。
- 程序中加入:
#include <TFT_eSPI.h> #include "Chinese_32.h" #include "BMP1.h"TFT_eSPI tft = TFT_eSPI(); // 創建TFT對象 #include <TJpg_Decoder.h>void TFT_Display() {tft.pushImage(0, 0, 238, 320, (uint16_t*)gImage_demo_image1); }void setup() {tft.init(); // 初始化// tft.fillScreen(TFT_BLACK); // 設置屏幕背景顏色// analogWrite(BLK, 150); // 調節屏幕亮度,0最亮,255最暗tft.fillScreen(TFT_BLACK);TFT_Display(); } void loop() {}
- 如圖所示的效果。
總結
????????以上所有資料和應用我都放在文件中了。
資料地址:https://download.csdn.net/download/weixin_42320020/88901719
禁止轉載!