Arduino-ILI9341驱动实现TFT屏输出中文四
1.概述
ILI9341驱动在TFT屏幕上不支持直接输出汉字,需要借助提取字模软件将汉字转为十六进制地址,然后通过改地址控制每行和每列点阵的亮和灭实现显示汉字功能。
2.ILI9341显示汉字
2.1.PCtoLCD2002取模软件
在网上搜索PCtoLCD2002取模软件使用教程,就能够获取到汉字生成结果,其结果为每个汉字都是一个十六进制数组。
2.2.显示汉字
使用Adafruit_GFX类
提供的drawBitmap函数
输出位图实现绘制图形和汉字。
函数介绍如下
//int16_t x 和 int16_t y:这两个参数定义了位图在屏幕或绘图表面的左上角应该开始的坐标。x 是水平坐标,y 是垂直坐标。//const uint8_t bitmap[]:这是一个指向位图数据的指针,位图数据通常是一个字节数组,其中每个字节代表一行或多列像素的状态(开或关,通常使用二进制表示)。
//int16_t w 和 int16_t h:这两个参数分别定义了位图的宽度和高度。它们告诉函数位图的大小,以便正确地绘制它。//uint16_t color:这个参数定义了位图应该使用的颜色
void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,int16_t h, uint16_t color);
2.3.显示中文示例
#include "Adafruit_ILI9341.h"// 定义引脚功能
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13// 1.创建Adafruit_ILI9341类的对象tft,传入SPI引脚参数,
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);void setup() {Serial.begin(9600);Serial.println("ILI9341驱动控制显示屏显示内容"); //2.初始化tfttft.begin();//在IDE控制台输出调试信息Serial.print(F("在IDE控制台输出文字内容"));Serial.println(testText());delay(5000);Serial.println(F("Done!"));}void loop(void) {//调用函数输出文本内容testText();
}/************************ 二、显示中文 ***********************/static const unsigned char PROGMEM str1[] =
{0x00,0x00,0x27,0xF8,0x12,0x08,0x12,0x08,0x82,0x08,0x41,0x10,0x49,0x10,0x09,0x10,
0x10,0xA0,0x10,0xA0,0xE0,0x40,0x20,0x40,0x20,0xA0,0x21,0x10,0x22,0x08,0x0C,0x06};/*汉*/
static const unsigned char PROGMEM str2[] =
{0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x80,0x04,0x1F,0xE0,0x00,0x40,0x00,0x80,
0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00};/*"字",1*/// 创建函数定义ILI9341驱动操作屏幕输出字符串的功能
unsigned long testText() {//设置背景颜色tft.fillScreen(ILI9341_BLACK);unsigned long start = micros();/*1.10,20表示x和y轴起点位置2.str1和str2表示汉字数组3.16,16表示显示的宽高4.ILI9341_GREEN设置汉字颜色*/tft.drawBitmap(10,20,str1,16,16,ILI9341_GREEN);tft.drawBitmap(25,20,str2,16,16,ILI9341_GREEN);return micros() - start;
}
2.4.二维数组汉字显示示例
/*********************** 一、初始化设置部分 **********************/#include "Adafruit_ILI9341.h"// 定义引脚功能
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13// 1.创建Adafruit_ILI9341类的对象tft,传入SPI引脚参数,
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);void setup() {Serial.begin(9600);Serial.println("ILI9341驱动控制显示屏显示内容"); //2.初始化tfttft.begin();//在IDE控制台输出调试信息Serial.print(F("在IDE控制台输出文字内容"));Serial.println(testText());delay(3000);Serial.println(F("Done!"));}void loop(void) {//调用函数输出文本内容testText();
}/************************ 二、显示汉字 ***********************/const unsigned char zw16[10][16] = { //一个字16x16,也就是256bit,占32个元素
{0x00,0x00,0x00,0x00,0x3D,0xFE,0x46,0x20,0x44,0x20,0x44,0x20,0x44,0x20,0x44,0x20},
{0x7C,0x20,0x47,0xFC,0x44,0x20,0x44,0x20,0x44,0x20,0x44,0x20,0x44,0x20,0x3B,0xFF},/*"旺",0*/
{0x00,0x00,0x00,0x00,0x3F,0xF8,0x00,0x10,0x00,0x60,0x00,0x80,0x01,0x00,0x00,0x80},
{0x7F,0xFE,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x20,0x00,0x20,0x18,0x40,0x07,0x80},/*"子",1*/
{0x00,0x00,0x00,0x00,0x7F,0xFC,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00},
{0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x7F,0xFE},/*"工",2*/
{0x00,0x00,0x00,0x00,0x12,0x00,0x13,0xFE,0x12,0x80,0x24,0x80,0x24,0x80,0x64,0xFC},
{0x68,0x80,0x20,0x80,0x20,0x80,0x20,0x80,0x20,0xFE,0x20,0x80,0x20,0x80,0x00,0x80},/*"作",3*/
{0x00,0x00,0x02,0x00,0x01,0x00,0x7F,0xFC,0x40,0x02,0x40,0x02,0x7F,0xFE,0x04,0x20},
{0x04,0x10,0x08,0x08,0x07,0xF4,0x01,0x00,0x3F,0xFC,0x01,0x00,0x01,0x00,0x7F,0xFE},/*"室",4*/
};// 创建函数定义ILI9341驱动操作屏幕输出字符串的功能
unsigned long testText() {//设置背景颜色tft.fillScreen(ILI9341_BLACK);unsigned long start = micros();uint8_t zw[32];for (int i = 0; i < 16; i++){zw[i]=zw16[0][i];}for (int i = 0; i < 16; i++){zw[i+16]=zw16[1][i];}tft.drawBitmap(10,20,zw,16,16,ILI9341_GREEN);for (int i = 0; i < 16; i++){zw[i]=zw16[2][i];}for (int i = 0; i < 16; i++){zw[i+16]=zw16[3][i];}tft.drawBitmap(26,20,zw,16,16,ILI9341_RED);for (int i = 0; i < 16; i++){zw[i]=zw16[4][i];}for (int i = 0; i < 16; i++){zw[i+16]=zw16[5][i];}tft.drawBitmap(42,20,zw,16,16,ILI9341_GREEN);for (int i = 0; i < 16; i++){zw[i]=zw16[6][i];}for (int i = 0; i < 16; i++){zw[i+16]=zw16[7][i];}tft.drawBitmap(58,20,zw,16,16,ILI9341_GREEN);for (int i = 0; i < 16; i++){zw[i]=zw16[8][i];}for (int i = 0; i < 16; i++){zw[i+16]=zw16[9][i];}tft.drawBitmap(74,20,zw,16,16,ILI9341_GREEN);return micros() - start;
}