Arduino 使用 TFT 彩屏

这篇博客介绍了如何在Arduino环境下,利用Arduino UNO和1.8寸128x160 RGB TFT彩屏(ST7735)进行基本显示操作。内容包括硬件连接、软件设置,如使用Arduino IDE和Adafruit库。博客详细讲解了纯色填充屏幕、字符串显示、绘制直线和多种几何图形(矩形、圆形、三角形、圆角矩形及组合图案)的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(如有更新,见原文:https://blog.iyatt.com/?p=12284)

1环境

1.1 硬件

Arduino UNO
1.8 寸 128x160 RGB TFT(ST7735)
file

1.2 软件

Arduino IDE 2.2.1
Adafruit ST7735 and ST7789 Library 1.10.3

接线:

\begin{array}{|l|l|}
\hline
TFT & Arduino \\
\hline
SCL & 13 (硬件 SPI - SCLK)\\
SDA & 11 (硬件 SPI - MOSI)\\
RST & 9 \\
DC & 8 \\
CS & 10 \\
GND & GND \\
VCC & 5V(3.3V 不能正常工作)\\
\hline
\end{array}

2 基本显示

关于颜色转换参考:https://blog.iyatt.com/?p=12867
2.1 是完全代码,后面开始非 setup 和 loop 部分都和 2.1 一样,直接省略。

2.1 纯色填充屏幕

#include <Adafruit_ST7735.h>
#include <SPI.h>

#define CS      10
#define RST     9
#define DC      8

Adafruit_ST7735 tft = Adafruit_ST7735(CS, DC, RST);

uint16_t rgb888_to_565 (uint32_t rgb888, bool reverse = false)
{
	uint8_t r = (rgb888 & 0xFF0000) >> 16;
	uint8_t g = (rgb888 & 0x00FF00) >> 8;
	uint8_t b = (rgb888 & 0x0000FF);

    if (reverse)
    {
        return ((b >> 3) << 11) |
                ((g >> 2) << 5) |
                (r >> 3);
    }
	
	return ((r >> 3) << 11) |
            ((g >> 2) << 5) |
            (b >> 3);
}

uint16_t rgb888_to_565(uint8_t r, uint8_t g, uint8_t b, bool reverse = false)
{
    if (reverse)
    {
        return ((b >> 3) << 11) |
                ((g >> 2) << 5) |
                (r >> 3);
    }

    return ((r >> 3) << 11) |
            ((g >> 2) << 5) |
            (b >> 3);
}

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
}

void loop()
{
    // 自定义颜色
    tft.fillScreen(rgb888_to_565(255, 0, 255, true)); // 紫色
    delay(500);
    tft.fillScreen(rgb888_to_565(0xFFC0CB, true)); // 粉色
    delay(500);
    tft.fillScreen(rgb888_to_565(255, 0, 0, true)); // 红色
    delay(500);
}

可以看到各种颜色轮番闪烁
file
file

2.2 字符串显示

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
    tft.fillScreen(ST77XX_BLACK); // 填充黑屏
    delay(1000);

    tft.setCursor(0, 0); // 设置起始光标位置
    tft.setTextColor(rgb888_to_565(0xFF0000, true)); // 设置颜色
    tft.setTextWrap(true); // 设置自动换行
    tft.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ");
    // delay(2000);
    tft.setTextWrap(false);
}

void loop()
{
    tft.fillScreen(ST77XX_BLACK);
    

    tft.setCursor(0, 30);
    tft.setTextSize(1);
    tft.setTextColor(rgb888_to_565(0xE3CF57, true));
    tft.println("hello world!");
    delay(500);

    tft.setTextColor(rgb888_to_565(0xFF00FF, true));
    tft.setTextSize(2);
    tft.println("hello world!");
    delay(500);

    tft.setTextColor(rgb888_to_565(0xFF8000, true));
    tft.setTextSize(3);
    tft.println("hello world!");
    delay(500);
}

file
file

2.3 绘制直线

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
    delay(1000);
}

void loop()
{
    uint16_t color = rgb888_to_565(255, 0, 0, true); // 红色
    tft.fillScreen(ST77XX_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6)
    {
        tft.drawLine(0, 0, x, tft.height()-1, color);
        delay(10);
    }
    for (int16_t y=0; y < tft.height(); y+=6)
    {
        tft.drawLine(0, 0, tft.width()-1, y, color);
        delay(10);
    }

    color = rgb888_to_565(0, 255, 0, true); // 绿色
    tft.fillScreen(ST77XX_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6)
    {
        tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
        delay(10);
    }
    for (int16_t y=0; y < tft.height(); y+=6)
    {
        tft.drawLine(tft.width()-1, 0, 0, y, color);
        delay(10);
    }

    color = rgb888_to_565(0, 0, 255, true); // 蓝色
    tft.fillScreen(ST77XX_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6)
    {
        tft.drawLine(0, tft.height()-1, x, 0, color);
        delay(10);
    }
    for (int16_t y=0; y < tft.height(); y+=6)
    {
        tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
        delay(10);
    }

    color = rgb888_to_565(255, 0, 255, true); // 紫色
    tft.fillScreen(ST77XX_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6)
    {
        tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
        delay(10);
    }
    for (int16_t y=0; y < tft.height(); y+=6)
    {
        tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
        delay(10);
    }
}

file
file
file

水平线和垂直线

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
    delay(1000);
}

void loop()
{
    static uint16_t color1 = rgb888_to_565(255, 0, 0, true);
    static uint16_t color2 = rgb888_to_565(0, 0, 255);
    tft.fillScreen(ST77XX_BLACK);
    for (int16_t y=0; y < tft.height(); y+=5)
    {
        tft.drawFastHLine(0, y, tft.width(), color1);
        delay(30);
    }
    for (int16_t x=0; x < tft.width(); x+=5)
    {
        tft.drawFastVLine(x, 0, tft.height(), color2);
        delay(30);
    }
}

file
file

2.4 绘制几何图形

2.4.1 绘制矩形

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
    delay(1000);
}

void loop()
{
    static uint16_t color1 = rgb888_to_565(255, 0, 0, true);
    static uint16_t color2 = rgb888_to_565(0, 0, 255, true);
    tft.fillScreen(ST77XX_BLACK);
    for (int16_t x=tft.width()-1; x > 6; x-=6)
    {
        tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1); // 实心
        tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2); // 边框
        delay(50);
    }
}

file
file

2.4.2 绘制圆形

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
    delay(1000);
}

void loop()
{
    static uint16_t color1 = rgb888_to_565(255, 0, 0, true);
    static uint16_t color2 = rgb888_to_565(0, 0, 255, true);
    static uint8_t radius = 30;
    tft.fillScreen(ST77XX_BLACK);
    for (int16_t x=0; x < tft.width()+radius; x+=radius*2)
    {
        for (int16_t y=0; y < tft.height()+radius; y+=radius*2)
        {
            tft.fillCircle(x, y, radius, color1);
            tft.drawCircle(x, y, radius, color2);
            delay(50);
        }
    }
}

file
file

2.4.3 绘制三角形

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
    delay(1000);
}

void loop()
{
    uint16_t color = rgb888_to_565(0, 0, 0, true);
    tft.fillScreen(ST77XX_BLACK);
    int w = tft.width() / 2;
    int x = tft.height() - 1;
    int y = 0;
    int z = tft.width();
    for(int t = 0 ; t <= 15; t++)
    {
        tft.drawTriangle(w, y, y, x, z, x, color);
        x-=4;
        y+=4;
        z-=4;
        color+=100;
        delay(100);
    }
}

file
file

2.4.4 绘制圆角矩形

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
    delay(1000);
}

void loop()
{
    uint16_t color = rgb888_to_565(0, 0, 0, true);
    tft.fillScreen(ST77XX_BLACK);
    for(int t = 0 ; t <= 4; t+=1)
    {
        int x = 0;
        int y = 0;
        int w = tft.width()-2;
        int h = tft.height()-2;
        for(int i = 0 ; i <= 16; i+=1)
        {
            tft.drawRoundRect(x, y, w, h, 5, color);
            x+=2;
            y+=3;
            w-=4;
            h-=6;
            color+=1100;
            delay(50);
        }
        color += 100;
    }
}

file
file

2.4.5 绘制组合图案

void setup()
{
    tft.initR(INITR_GREENTAB); // 初始化
    delay(1000);
}

void loop()
{
    tft.fillScreen(ST77XX_BLACK);
    tft.fillRoundRect(25, 10, 78, 60, 8, ST77XX_WHITE);
    tft.fillTriangle(42, 20, 42, 60, 90, 40, rgb888_to_565(0xFF0000, true));
    delay(500);

    tft.fillRoundRect(25, 90, 78, 60, 8, ST77XX_WHITE);
    tft.fillRoundRect(39, 98, 20, 45, 5, rgb888_to_565(0x00FF00, true));
    tft.fillRoundRect(69, 98, 20, 45, 5, rgb888_to_565(0x00FF00, true));
    delay(500);

    tft.fillTriangle(42, 20, 42, 60, 90, 40, rgb888_to_565(0x0000FF, true));
    delay(50);

    tft.fillRoundRect(39, 98, 20, 45, 5, rgb888_to_565(0xFF0000, true));
    tft.fillRoundRect(69, 98, 20, 45, 5, rgb888_to_565(0xFF0000, true));

    tft.fillTriangle(42, 20, 42, 60, 90, 40, rgb888_to_565(0x00FF00, true));
}

file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IYATT yx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值