如何使用Arduino的TFTLCD

在显示器上呈现想法
在电子项目中,在用户和系统之间创建接口非常重要。可以通过显示有用的数据,菜单和易于访问来创建该界面。漂亮的设计也很重要。
有几个组件可以实现这一点。 led,7段,字符和图形显示,以及全彩tft lcd。项目的正确组件取决于要显示的数据量,用户交互类型和处理器容量。
tft lcd是液晶显示器(lcd)的一种变体,它使用薄膜晶体管(tft)技术来改善图像质量,例如可寻址性和对比度。 tft lcd是有源矩阵lcd,与无源矩阵lcd或具有少量段的简单直接驱动lcd相比。
在基于arduino的项目中,处理器频率很低。因此无法显示复杂的高清图像和高速运动。因此,全彩色tft lcd只能用于显示简单的数据和命令。
在本文中,我们使用图书馆和先进的技术,以专业的设计显示数据,图表,菜单等。这可以将您的项目演示文稿移动到更高的级别。
哪个尺寸?哪个控制器?
显示大小会影响项目参数。更大的显示并不总是更好。如果要显示高分辨率图像和标志,则应选择分辨率更高的大尺寸显示器。但它会降低处理速度,需要更多空间,还需要更多电流才能运行。
所以,首先,您应该检查分辨率,运动速度,项目图像,文本和数字的颜色和大小细节。
我们建议流行尺寸的arduino显示屏,如3.5英寸480×320,2.8英寸400×240,2.4英寸320×240和1.8英寸220×176。
选择正确的显示器后,是时候选择合适的控制器了。如果您想显示字符,测试,数字和静态图像以及显示速度并不重要,atmega328 arduino板(如arduino uno)是一个合适的选择。如果代码的大小很大,uno板可能还不够。您可以使用arduino mega2560代替。如果你想高速显示高分辨率图像和动作,你应该使用arm核心arduino板,如arduino due。
司机&库
在电子/计算机硬件中,显示驱动器通常是半导体集成电路(但也可以包括状态)由分立逻辑和其他组件组成的机器,其在微处理器,微控制器,asic或通用外围接口和特定类型的显示设备之间提供接口功能,例如lcd,led,oled,epaper,crt,真空荧光灯或nixie。
显示驱动程序通常会接受命令和数据使用行业标准的通用串行或并行接口,如ttl,cmos,rs232,spi,i2c等,并生成具有适当电压,电流,定时和多路分解的信号,以使显示器显示所需的文本或图像。
lcd制造商在其产品中使用不同的驱动程序。其中一些更受欢迎,其中一些是非常未知的。要轻松运行显示器,您应该使用arduino lcd库并将其添加到您的代码中。否则运行显示器可能非常困难。您可以在互联网上找到许多免费库,但关于库的重点是它们与lcd驱动程序的兼容性。您的液晶显示器的驱动程序必须为您的图书馆所知。在本文中,我们使用adafruit gfx库和mcufriend kbv库和示例代码。您可以从以下链接下载它们。
解压缩mcufriend kbv并打开mcufriend_kbv.cpp。您可以看到mcufriend库支持的驱动程序列表。
打开示例文件夹。 arduino可以运行几个示例代码。连接lcd并测试一些例子。
代码
您必须添加库然后上传代码。如果这是您第一次运行arduino板,请不要担心。只需按照以下步骤操作:
访问www.arduino.cc/en/main/software下载您的操作系统软件。按照说明安装ide软件。
运行arduino ide并清除文本编辑器并在文本编辑器中复制以下代码。
导航到草图并包含库。现在单击添加zip库并添加库
选择工具和板中的板,选择您的arduino板。
将arduino连接到pc并在工具中设置com端口
按上传(箭头标志)按钮。
你已经完成了设置!
上传示例代码后,就可以了解如何在液晶显示屏上创建图像。
打开一个新的草图,以及必要的代码,如以下部分所述。
图书馆
#include “adafruit_gfx.h”
#include “mcufriend_kbv.h”
第一行为显示添加核心图形库(由adafruit编写)。
第二个添加了一个支持mcufriend arduino显示屏蔽驱动程序的库。
#include “touchscreen.h” // only when you want to use touch screen
#include “bitmap_mono.h” // when you want to display a bitmap image from library
#include “bitmap_rgb.h” // when you want to display a bitmap image from library
#include “fonts/freesans9pt7b.h” // when you want other fonts
#include “fonts/freesans12pt7b.h” // when you want other fonts
#include “fonts/freeserif12pt7b.h” // when you want other fonts
#include “freedefaultfonts.h” // when you want other fonts
#include “spi.h” // using sdcard for display bitmap image
#include “sd.h”
这些库不是必需的现在,但你可以添加它们。
基本命令
class&对象
//(int cs=a3, int rs=a2, int wr=a1, int rd=a0, int rst=a4)
mcufriend_kbv tft(a3, a2, a1, a0, a4);
该行从mcufriend_kbv类创建一个名为tft的对象,并在lcd和arduino之间提供spi通信。
运行lcd
uint16_t id = tft.readid();
tft.begin(id);
tft.readid函数从显示中读取id并将其放入id变量中。然后tft.begin函数获取id并且lcd准备工作。
显示器的分辨率
tft.width(); //int16_t width(void);
tft.height(); //int16_t height(void);
通过这两个函数,您可以找到显示器的分辨率。只需将它们添加到代码中,并将输出放在uint16_t变量中。然后通过serial.println();从串口读取它。首先添加serial.begin(9600);在setup()。
屏幕颜色
tft.fillscreen(t); //fillscreen(uint16_t t);
fillscreen功能将屏幕颜色更改为t颜色。 t应该是一个包含utft颜色代码的16位变量。
#define black 0x0000
#define navy 0x000f
#define darkgreen 0x03e0
#define darkcyan 0x03ef
#define maroon 0x7800
#define purple 0x780f
#define olive 0x7be0
#define lightgrey 0xc618
#define darkgrey 0x7bef
#define blue 0x001f
#define green 0x07e0
#define cyan 0x07ff
#define red 0xf800
#define magenta 0xf81f
#define yellow 0xffe0
#define white 0xffff
#define orange 0xfd20
#define greenyellow 0xafe5
#define pink 0xf81f
您可以将这些行添加到代码的顶部,只需使用函数中的颜色名称。
填充像素
tft.drawpixel(x,y,t); //drawpixel(int16_t x, int16_t y, uint16_t t)
tft.readpixel(x,y); //uint16_t readpixel(int16_t x, int16_t y)
drawpixel 功能用t颜色填充x和y位置的像素。
readpixel 函数读取x和y位置中像素的颜色。
绘制线
tft.drawfastvline(x,y,h,t);
//drawfastvline(int16_t x, int16_t y, int16_t h, uint16_t t)
tft.drawfasthline(x,y,w,t);
//drawfasthline(int16_t x, int16_t y, int16_t w, uint16_t t)
tft.drawline(xi,yi,xj,yj,t);
//drawline(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t t)
drawfastvline 函数绘制一条从x,y位置开始的垂直线,其长度为h像素,颜色为t。
drawfasthline 函数绘制一条从x和y位置开始的水平线和len gth是w像素,颜色是t。
drawline 函数绘制一条线从xi和yi位置开始的是xj和yj,颜色是t。
for (uint16_t a=0; a《5; a++)
{ tft.drawfastvline(x+a, y, h, t);}
for (uint16_t a=0; a《5; a++)
{ tft.drawfasthline(x, y+a, w, t);}
for (uint16_t a=0; a《5; a++)
{ tft.drawline(xi+a, yi, xj+a, yj, t);}
for (uint16_t a=0; a《5; a++)
{ tft.drawline(xi, yi+a, xj, yj+a, t);}
这三个代码块像前面的代码一样绘制线条,厚度为5像素。
tft.fillrect(x,y,w,h,t);
//fillrect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t t)
tft.drawrect(x,y,w,h,t);
//drawrect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t t)
tft.fillroundrect(x,y,w,h,r,t);
//fillroundrect (int16_t x, int16_t y, int16_t w, int16_t h, uint8_t r , uint16_t t)
tft.drawroundrect(x,y,w,h,r,t);
//drawroundrect(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t r , uint16_t t)
fillrect 函数在x和y位置绘制一个填充的矩形。 w是宽度,h是高度,t是rextangle的颜色
drawrect 函数在x和y位置绘制一个矩形,宽度为w,高度为h,颜色为t。
fillroundrect 函数绘制一个填充的矩形,其中x和y位置具有r半径圆角,w宽度和h高度和t颜色。
drawroundrect 函数绘制一个矩形,在x和y位置具有r半径圆角,w宽度和h高度和t颜色。
绘制圆圈
tft.drawcircle(x,y,r,t); //drawcircle(int16_t x, int16_t y, int16_t r, uint16_t t)
tft.fillcircle(x,y,r,t); //fillcircle(int16_t x, int16_t y, int16_t r, uint16_t t)
drawcircle 函数在x和y位置以及r radius和t color中绘制一个圆。
fillcircle 函数在x和y位置以及r radius和t color中绘制一个实心圆。
for (int p = 0; p 《 4000; p++)
{ j = 120 * (sin(pi * p / 2000));
i = 120 * (cos(pi * p / 2000));
j2 = 60 * (sin(pi * p / 2000));
i2 = 60 * (cos(pi * p / 2000));
tft.drawline(i2 + 160, j2 + 160, i + 160, j + 160, col[n]);
}
通过此代码,你可以绘制弧形。更改0到4000之间的“for”。
绘制三角形
tft.drawtriangle(x1,y1,x2,y2,x3,y3,t);
//drawtriangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3,// uint16_t t)
tft.filltriangle(x1,y1,x2,y2,x3,y3,t);
//filltriangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3,// uint16_t t)
drawtriangle 函数绘制一个三角形的三角形位置x,y和z,以及t颜色。
filltriangle 函数绘制一个三角形位置x,y和z以及t颜色的实心三角形。
显示文本
tft.setcursor(x,y); //setcursor(int16_t x, int16_t y)
此代码将光标位置设置为x和y
tft.settextcolor(t); //settextcolor(uint16_t t)
tft.settextcolor(t,b); //settextcolor(uint16_t t, uint16_t b)
第一行设置文字的颜色。下一行设置文本的颜色及其背景。
tft.settextsize(s); //settextsize(uint8_t s)
此代码用s设置文本的大小。 s是介于1和5之间的数字。
tft.write(c); //write(uint8_t c)
此代码显示一个字符。
tft.println(“www.electropeak.com”);
tft.print(“www.electropeak.com”);
第一个函数显示一个字符串并将光标移动到下一个第一行显示字符串。
showmsgxy(x,y,sz,&freesans9pt7b,“www.electropeak.com”);
//void showmsgxy(int x, int y, int sz, const gfxfont *f, const char *msg)
void showmsgxy(int x, int y, int sz, const gfxfont *f, const char *msg)
{ uint16_t x1, y1;
uint16_t wid, ht;
tft.setfont(f);
tft.setcursor(x, y);
tft.settextcolor(0x0000);
tft.settextsize(sz);
tft.print(msg);
}
这函数更改文本的字体。您应该添加此功能和字体库。
for (int j = 0; j 《 20; j++) {
tft.setcursor(145, 290);
int color = tft.color565(r -= 12, g -= 12, b -= 12);
tft.settextcolor(color);
tft.print(“www.electropeak.com”);
delay(30);
}
此功能可淡化文字。你应该把它添加到你的代码中。
旋转屏幕
tft.setrotation(r); //setrotation(uint8_t r)
此代码旋转屏幕。 0 = 0,1 = 90,2 = 180,3 = 270。
反转屏幕颜色
tft.invertdisplay(i); //invertdisplay(boolean i)
此代码反转屏幕的颜色。
tft.color565(r,g,b); //uint16_t color565(uint8_t r, uint8_t g, uint8_t b)
此代码提供rgb代码并获取utft颜色代码。
滚动屏幕
for (uint16_t i = 0; i 《 maxscroll; i++) {
tft.vertscroll(0, maxscroll, i);
delay(10);}
此代码滚动屏幕。 maxroll是滚动的最大高度。
重置
tft.reset();
此代码重置屏幕。
显示单色图像
static const uint8_t name[] progmem =
{ //add image code here.
}
tft.drawbitmap(x, y, name, sx, sy, 0x0000);
首先你应将您的图像转换为十六进制代码。从以下链接下载该软件。如果您不想更改软件的设置,则必须反转图像的颜色并使图像水平镜像并逆时针旋转90度。现在将其添加到软件并进行转换。打开导出的文件并将十六进制代码复制到arduino ide。 x和y是图像的位置。 sx和sy是图像的大小。您可以在最后一个输入中更改图像的颜色。
rgb彩色图像显示
const uint16_t name[] progmem = {
//add image code here.
}
tft.drawrgbbitmap(x, y, name, sx, sy);
首先,您应该将图像转换为代码。使用此链接转换图像:
http://www.rinkydinkelectronics.com/t_imageconverter565.php
上传图像并下载utft库可以处理的转换文件。现在将十六进制代码复制到arduino ide。 x和y是图像的位置。 sx和sy是图像的大小。
预先设计的模板
载入
在这个模板中,我们只使用了一个字符串和8个实心圆圈按顺序改变颜色。要在静态点周围绘制圆圈,可以使用sin();和cos();功能。您应该定义pi编号。要更改颜色,可以使用color565();功能并替换您的rgb代码。
#include “adafruit_gfx.h”
#include “mcufriend_kbv.h”
mcufriend_kbv tft;
#include “fonts/freesans9pt7b.h”
#include “fonts/freesans12pt7b.h”
#include “fonts/freeserif12pt7b.h”
#include “freedefaultfonts.h”
#define pi 3.1415926535897932384626433832795
int col[8];
void showmsgxy(int x, int y, int sz, const gfxfont *f, const char *msg)
{
int16_t x1, y1;
uint16_t wid, ht;
tft.setfont(f);
tft.setcursor(x, y);
tft.settextcolor(0x0000);
tft.settextsize(sz);
tft.print(msg);
}
void setup() {
tft.reset();
serial.begin(9600);
uint16_t id = tft.readid();
tft.begin(id);
tft.setrotation(1);
tft.invertdisplay(true);
tft.fillscreen(0xffff);
showmsgxy(170, 250, 2, &freesans9pt7b, “loading.。.”);
col[0] = tft.color565(155, 0, 50);
col[1] = tft.color565(170, 30, 80);
col[2] = tft.color565(195, 60, 110);
col[3] = tft.color565(215, 90, 140);
col[4] = tft.color565(230, 120, 170);
col[5] = tft.color565(250, 150, 200);
col[6] = tft.color565(255, 180, 220);
col[7] = tft.color565(255, 210, 240);
}
void loop() {
for (int i = 8; i 》 0; i--) {
tft.fillcircle(240 + 40 * (cos(-i * pi / 4)), 120 + 40 * (sin(-i * pi / 4)), 10, col[0]); delay(15);
tft.fillcircle(240 + 40 * (cos(-(i + 1)*pi / 4)), 120 + 40 * (sin(-(i + 1)*pi / 4)), 10, col[1]); delay(15);
tft.fillcircle(240 + 40 * (cos(-(i + 2)*pi / 4)), 120 + 40 * (sin(-(i + 2)*pi / 4)), 10, col[2]); delay(15);
tft.fillcircle(240 + 40 * (cos(-(i + 3)*pi / 4)), 120 + 40 * (sin(-(i + 3)*pi / 4)), 10, col[3]); delay(15);
tft.fillcircle(240 + 40 * (cos(-(i + 4)*pi / 4)), 120 + 40 * (sin(-(i + 4)*pi / 4)), 10, col[4]); delay(15);
tft.fillcircle(240 + 40 * (cos(-(i + 5)*pi / 4)), 120 + 40 * (sin(-(i + 5)*pi / 4)), 10, col[5]); delay(15);
tft.fillcircle(240 + 40 * (cos(-(i + 6)*pi / 4)), 120 + 40 * (sin(-(i + 6)*pi / 4)), 10, col[6]); delay(15);
tft.fillcircle(240 + 40 * (cos(-(i + 7)*pi / 4)), 120 + 40 * (sin(-(i + 7)*pi / 4)), 10, col[7]); delay(15);
}
}
徽标演示
在此模板中,我们将a.jpg图像转换为.c文件并添加到代码中,写了一个字符串并使用淡入淡出代码进行显示。然后我们使用滚动代码向左移动屏幕。下载.h文件并将其添加到arduino sketch的文件夹中。
#include “adafruit_gfx.h” // core graphics library
#include “mcufriend_kbv.h” // hardware-specific library
mcufriend_kbv tft;
#include “ard_logo.h”
#define black 0x0000
#define red 0xf800
#define green 0x07e0
#define white 0xffff
#define grey 0x8410
#include “fonts/freesans9pt7b.h”
#include “fonts/freesans12pt7b.h”
#include “fonts/freeserif12pt7b.h”
#include “freedefaultfonts.h”
void showmsgxy(int x, int y, int sz, const gfxfont *f, const char *msg)
{
int16_t x1, y1;
uint16_t wid, ht;
tft.setfont(f);
tft.setcursor(x, y);
tft.settextsize(sz);
tft.println(msg);
}
uint8_t r = 255, g = 255, b = 255;
uint16_t color;
void setup()
{
serial.begin(9600);
uint16_t id = tft.readid();
tft.begin(id);
tft.invertdisplay(true);
tft.setrotation(1);
}
void loop(void)
{
tft.invertdisplay(true);
tft.fillscreen(white);
tft.drawrgbbitmap(100, 50, logo, 350, 200);
delay(1000);
tft.settextsize(2);
for (int j = 0; j 《 20; j++) {
color = tft.color565(r -= 12, g -= 12, b -= 12);
tft.settextcolor(color);
showmsgxy(95, 280, 1, &freesans12pt7b, “electropeak presents”);
delay(20);
}
delay(1000);
for (int i = 0; i 《 480; i++) {
tft.vertscroll(0, 480, i);
tft.drawfastvline(i, 0, 320, 0xffff); // vertical line
delay(5);}
while (1);
}
圆点图
在这个模板中,我们使用了绘制线,实心圆和字符串显示功能。
#include “adafruit_gfx.h”
#include “mcufriend_kbv.h”
mcufriend_kbv tft;
uint16_t ox=0,oy=0;
int ave=0, avec=0, avet=0;
////////////////////////////////////////////////////////////////
void aveg(void)
{int z=0;
serial.println(ave);
serial.println(avec);
avet=ave/avec;
serial.println(avet);
avet=avet*32;
for (int i=0; i《24; i++){
for (uint16_t a=0; a《3; a++){
tft.drawline(avet+a, z, avet+a, z+10, 0xfb21);} // thick
for (uint16_t a=0; a《2; a++){ tft.drawline(avet-a, z, avet-a, z+10, 0xfb21);} delay(100); z=z+20; } } ////////////////////////////////////////////////////////////////// void dchart_10x10(uint16_t nx,uint16_t ny) { ave+=nx; avec++; nx=nx*32; ny=ny*48; tft.drawcircle(nx, ny, 10, 0x0517); tft.drawcircle(nx, ny, 9, 0x0517); tft.fillcircle(nx, ny, 7, 0x0517); delay (100); ox=nx; oy=ny; } /////////////////////////////////////////////////////////////////////// void dotchart_10x10(uint16_t nx,uint16_t ny) { ave+=nx; avec++; nx=nx*32; ny=ny*48; int plus=0; float fplus=0; int sign=0; int y=0,x=0; y=oy; x=ox; float xmines, ymines; xmines=nx-ox; ymines=ny-oy; if (ox》nx)
{xmines=ox-nx;
sign=1;}
else
sign=0;
for (int a=0; a《(ny-oy); a++)
{
fplus+=xmines/ymines;
plus=fplus;
if (sign==1)
tft.drawfasthline(0, y, x-plus, 0xbfdf);
else
tft.drawfasthline(0, y, x+plus, 0xbfdf);
y++;
delay(5);}
for (uint16_t a=0; a《2; a++){
tft.drawline(ox+a, oy, nx+a, ny, 0x01e8);} // thick
for (uint16_t a=0; a《2; a++){
tft.drawline(ox, oy+a, nx, ny+a, 0x01e8);}
ox=nx;
oy=ny;
}
////////////////////////////////////////////////////////////////////
void setup() {
tft.reset();
serial.begin(9600);
uint16_t id = tft.readid();
tft.begin(id);
}
void loop() {
tft.invertdisplay(true);
tft.fillscreen(0xffff);
dotchart_10x10(3, 0);
dotchart_10x10(2, 1);
dotchart_10x10(4, 2);
dotchart_10x10(4, 3);
dotchart_10x10(5, 4);
dotchart_10x10(3, 5);
dotchart_10x10(6, 6);
dotchart_10x10(7, 7);
dotchart_10x10(9, 8);
dotchart_10x10(8, 9);
dotchart_10x10(10, 10);
dchart_10x10(3, 0);
dchart_10x10(2, 1);
dchart_10x10(4, 2);
dchart_10x10(4, 3);
dchart_10x10(5, 4);
dchart_10x10(3, 5);
dchart_10x10(6, 6);
dchart_10x10(7, 7);
dchart_10x10(9, 8);
dchart_10x10(8, 9);
dchart_10x10(10, 10);
tft.setrotation(1);
tft.settextsize(2);
tft.settextcolor(0x01e8);
tft.setcursor(20, 20);
tft.print(“average”);
int dl=20;
for (int i=0;i《6;i++){
for (uint16_t a=0; a《3; a++){
tft.drawline(dl, 40+a, dl+10, 40+a, 0xfb21);}
dl+=16;}
tft.setrotation(0);
aveg();
while(1);
}
您可以在此处找到更多图表模板。
音乐
在这个模板中,我们添加了将转换后的图像转换为代码,然后使用两个黑色和白色弧来创建卷的指针。下载.h文件并将其添加到arduino草图的文件夹中。
#include “adafruit_gfx.h”
#include “mcufriend_kbv.h”
mcufriend_kbv tft;
#include “volume.h”
#define black 0x0000
int a = 0,b = 4000,c = 1000,d = 3000;
int s=2000;
int j, j2;
int i, i2;
int white;
void setup()
{
serial.begin(9600);
uint16_t id = tft.readid();
tft.begin(id);
tft.invertdisplay(true);
tft.setrotation(1);
}
void loop(void)
{
tft.invertdisplay(true);
tft.fillscreen(black);
tft.drawrgbbitmap(0, 0, test, 480, 320);
white = tft.color565(255, 255, 255);
while(1){
if (a 《 s) {
j = 14 * (sin(pi * a / 2000));
i = 14 * (cos(pi * a / 2000));
j2 = 1 * (sin(pi * a / 2000));
i2 = 1 * (cos(pi * a / 2000));
tft.drawline(i2 + 62, j2 + 240, i + 62, j + 240, white);
j = 14 * (sin(pi * (a-300) / 2000));
i = 14 * (cos(pi * (a-300) / 2000));
j2 = 1 * (sin(pi * (a-300) / 2000));
i2 = 1 * (cos(pi * (a-300) / 2000));
tft.drawline(i2 + 62, j2 + 240, i + 62, j + 240, 0x0000);
tft.fillrect(50, 285, 30, 30, 0x0000);
tft.settextsize(2);
tft.settextcolor(0xffff);
tft.setcursor(50, 285);
tft.print(a / 40); tft.print(“%”);
a++;
}
if (b 《 s) {
j = 14 * (sin(pi * b / 2000));
i = 14 * (cos(pi * b / 2000));
j2 = 1 * (sin(pi * b / 2000));
i2 = 1 * (cos(pi * b / 2000));
tft.drawline(i2 + 180, j2 + 240, i + 180, j + 240, white);
j = 14 * (sin(pi * (b-300) / 2000));
i = 14 * (cos(pi * (b-300) / 2000));
j2 = 1 * (sin(pi * (b-300) / 2000));
i2 = 1 * (cos(pi * (b-300) / 2000));
tft.drawline(i2 + 180, j2 + 240, i + 180, j + 240, 0x0000);
tft.fillrect(168, 285, 30, 30, 0x0000);
tft.settextsize(2);
tft.settextcolor(0xffff);
tft.setcursor(168, 285);
tft.print(b / 40); tft.print(“%”);
b++;}
if (c 《 s) {
j = 14 * (sin(pi * c / 2000));
i = 14 * (cos(pi * c / 2000));
j2 = 1 * (sin(pi * c / 2000));
i2 = 1 * (cos(pi * c / 2000));
tft.drawline(i2 + 297, j2 + 240, i + 297, j + 240, white);
j = 14 * (sin(pi * (c-300) / 2000));
i = 14 * (cos(pi * (c-300) / 2000));
j2 = 1 * (sin(pi * (c-300) / 2000));
i2 = 1 * (cos(pi * (c-300) / 2000));
tft.drawline(i2 + 297, j2 + 240, i + 297, j + 240, 0x0000);
tft.fillrect(286, 285, 30, 30, 0x0000);
tft.settextsize(2);
tft.settextcolor(0xffff);
tft.setcursor(286, 285);
tft.print(c / 40); tft.print(“%”);
c++;}
if (d 《 s) { j = 14 * (sin(pi * d / 2000)); i = 14 * (cos(pi * d / 2000)); j2 = 1 * (sin(pi * d / 2000)); i2 = 1 * (cos(pi * d / 2000)); tft.drawline(i2 + 414, j2 + 240, i + 414, j + 240, white); j = 14 * (sin(pi * (d-300) / 2000)); i = 14 * (cos(pi * (d-300) / 2000)); j2 = 1 * (sin(pi * (d-300) / 2000)); i2 = 1 * (cos(pi * (d-300) / 2000)); tft.drawline(i2 + 414, j2 + 240, i + 414, j + 240, 0x0000); tft.fillrect(402, 285, 30, 30, 0x0000); tft.settextsize(2); tft.settextcolor(0xffff); tft.setcursor(402, 285); tft.print(d / 40); tft.print(“%”); d++;} if (a 》 s) {
j = 14 * (sin(pi * a / 2000));
i = 14 * (cos(pi * a / 2000));
j2 = 1 * (sin(pi * a / 2000));
i2 = 1 * (cos(pi * a / 2000));
tft.drawline(i2 + 62, j2 + 240, i + 62, j + 240, white);
j = 14 * (sin(pi * (a+300) / 2000));
i = 14 * (cos(pi * (a+300) / 2000));
j2 = 1 * (sin(pi * (a+300) / 2000));
i2 = 1 * (cos(pi * (a+300) / 2000));
tft.drawline(i2 + 62, j2 + 240, i + 62, j + 240, 0x0000);
tft.fillrect(50, 285, 30, 30, 0x0000);
tft.settextsize(2);
tft.settextcolor(0xffff);
tft.setcursor(50, 285);
tft.print(a / 40); tft.print(“%”);
a--;}
if (b 》 s) {
j = 14 * (sin(pi * b / 2000));
i = 14 * (cos(pi * b / 2000));
j2 = 1 * (sin(pi * b / 2000));
i2 = 1 * (cos(pi * b / 2000));
tft.drawline(i2 + 180, j2 + 240, i + 180, j + 240, white);
j = 14 * (sin(pi * (b+300) / 2000));
i = 14 * (cos(pi * (b+300) / 2000));
j2 = 1 * (sin(pi * (b+300) / 2000));
i2 = 1 * (cos(pi * (b+300) / 2000));
tft.drawline(i2 + 180, j2 + 240, i + 180, j + 240, 0x0000);
tft.fillrect(168, 285, 30, 30, 0x0000);
tft.settextsize(2);
tft.settextcolor(0xffff);
tft.setcursor(168, 285);
tft.print(b / 40); tft.print(“%”);
b--;}
if (c 》 s) {
j = 14 * (sin(pi * c / 2000));
i = 14 * (cos(pi * c / 2000));
j2 = 1 * (sin(pi * c / 2000));
i2 = 1 * (cos(pi * c / 2000));
tft.drawline(i2 + 297, j2 + 240, i + 297, j + 240, white);
j = 14 * (sin(pi * (c+300) / 2000));
i = 14 * (cos(pi * (c+300) / 2000));
j2 = 1 * (sin(pi * (c+300) / 2000));
i2 = 1 * (cos(pi * (c+300) / 2000));
tft.drawline(i2 + 297, j2 + 240, i + 297, j + 240, 0x0000);
tft.fillrect(286, 285, 30, 30, 0x0000);
tft.settextsize(2);
tft.settextcolor(0xffff);
tft.setcursor(286, 285);
tft.print(c / 40); tft.print(“%”);
c--;}
if (d 》 s) {
j = 14 * (sin(pi * d / 2000));
i = 14 * (cos(pi * d / 2000));
j2 = 1 * (sin(pi * d / 2000));
i2 = 1 * (cos(pi * d / 2000));
tft.drawline(i2 + 414, j2 + 240, i + 414, j + 240, white);
j = 14 * (sin(pi * (d+300) / 2000));
i = 14 * (cos(pi * (d+300) / 2000));
j2 = 1 * (sin(pi * (d+300) / 2000));
i2 = 1 * (cos(pi * (d+300) / 2000));
tft.drawline(i2 + 414, j2 + 240, i + 414, j + 240, 0x0000);
tft.fillrect(402, 285, 30, 30, 0x0000);
tft.settextsize(2);
tft.settextcolor(0xffff);
tft.setcursor(402, 285);
tft.print(d / 40); tft.print(“%”);
d--;}
}
}
点击此链接可以找到更多动画模板(如下面的gif)
屏幕保护程序
在这个模板中,我们只是通过rgbbitmap和位图功能显示一些图像。只需为触摸屏制作代码并使用此模板即可。下载.h文件并将其添加到arduino草图的文件夹中。
#include “adafruit_gfx.h” // core graphics library
#include “mcufriend_kbv.h” // hardware-specific library
mcufriend_kbv tft;
#define black 0x0000
#define red 0xf800
#define green 0x07e0
#define white 0xffff
#define grey 0x8410
#include “images.h”
#include “fonts/freesans9pt7b.h”
#include “fonts/freesans12pt7b.h”
#include “fonts/freeserif12pt7b.h”
#include “freedefaultfonts.h”
int a = 3000;
int b = 4000;
int j, j2;
int i, i2;
void showmsgxy(int x, int y, int sz, const gfxfont *f, const char *msg)
{
int16_t x1, y1;
uint16_t wid, ht;
// tft.drawfasthline(0, y, tft.width(), 0xffff);
tft.setfont(f);
tft.setcursor(x, y);
tft.settextcolor(white);
tft.settextsize(sz);
tft.print(msg);
delay(1000);
}
void setup()
{
serial.begin(9600);
uint16_t id = tft.readid();
tft.begin(id);
tft.invertdisplay(true);
tft.setrotation(1);
}
void loop(void)
{
tft.invertdisplay(true);
tft.fillscreen(black);
tft.drawrgbbitmap(0, 0, test, 480, 320);
tft.drawbitmap(20, 20, line1, 45, 45, 0xffff);//battery
tft.drawbitmap(65, 20, line2, 45, 45, 0xffff);//wifi
tft.drawbitmap(125, 25, line3, 45, 45, 0xffff);//mail
tft.drawbitmap(185, 25, line4, 45, 45, 0xffff);//instagram
tft.drawbitmap(245, 25, line6, 45, 45, 0xffff);//power
tft.drawbitmap(20, 260, line5, 45, 45, 0xffff);//twitter
tft.drawbitmap(410, 140, line7, 45, 45, 0xffff);//rain
tft.settextsize(6);
tft.settextcolor(0xffff);
tft.setcursor(280, 210);
tft.print(“20:45”);
tft.settextsize(2);
tft.settextcolor(0xffff);
showmsgxy(330, 280, 1, &freesans12pt7b, “saturday”);
showmsgxy(300, 305, 1, &freesans12pt7b, “6 october 2018”);
while (1);
}

配备智能手套的太空服可能是解决宇航员操作困难问题
进博会:汇川技术×埃森哲,推进国际化合作
2018年人工智能发展趋势总结 总体呈现以下八大特点
MAX14529E, MAX14530E应用电路:具有USB
使用STM32单片机进行ADC间断转换模式的应用实例说明
如何使用Arduino的TFTLCD
8块2080ti主机炫到没朋友
思必驰与星海实验中学打造AI赋能教育新典范
采访刘军光:三星如何夺下小间距LED的风口机遇?
华为荣耀9你所不知道的一个隐藏属性!
魅族蓝牙音频接收器体验 有线耳机秒变蓝牙耳机
马斯克:2021年将在部分地区推出L5级全自动驾驶功能
ADPA7002CHIP功率放大器
ATAM 计划进入中国市场
ST推出电阻式多点触感单片控制器
业余开发者怎样进阶成专业开发者
基于区块链技术B2R企业对机器人的运营平台SKYFchain介绍
未来已来,人工智能将数字化运营提高,未来场景已经成为现实
感恩高原义诊,丁真点赞vivo影像加获奖作品
上海芯旺微正式宣布,公司引入包括硅港资本等在内的A轮融资