ESP8266/ESP32 驱动ST7789屏幕

本文以ESP12系列的esp8266相关多个开发板和一款ESP32开发板测试。只需要指定自己开发板具体的SPI引脚即可。

以ardunio 框架开发,本项目需要修改库文件,推荐基于platformio开发,一个项目一个第三方库目录这样不会与其他项目冲突。

安装库

  • ardunio IDE

这里以arduino ide 2.0举例,库管理搜索 搜索TFT_eSPI 找到作者为Bodmer的那个进行安装

  • 使用platformio + ardunio 框架开发的

    1.一种方式,使用pio home的界面搜索安装添加到 当前项目

    2.第二种方式:在当前项目路径打开终端执行以下命令,则会安装到本项目路径

    pio pkg install -l bodmer/TFT_eSPI

    3.第三种方式,修改ini.

    在platformio.ini里面添加以下内容,再执行构建或者 执行初始化pio init ,触发检测ini文件变化会自动下载到本项目路径

    lib_deps =bodmer/TFT_eSPI@^2.4.76
    

    使用clion+platformio 环境的同学使用第三种修改ini配置的,重新初始化项目更好一些,pio init --ide clion 帮助把下载的库文件路径加到 cmake 的include中。

定义模组引脚和屏幕

找到 TFT_eSPI 库本地文件路径中的 User_Setup_Select.h文件

ardunio ide需要自己去库的路径去查找文件。

使用platformio开发,当库安装完成后本项目的.pio文件夹下有一个libdeps文件夹

找到User_Setup_Select.h中的 #ifndef USER_SETUP_LOADED

在其上方粘贴以下代码

  • ESP8266

    // ############# 直接在这里统一定义 屏幕和开发板/模块SPI引脚############
    #define USER_SETUP_LOADED // 使其它定义不生效
    #define ST7789_DRIVER
    // 定义SPI的引脚 以下为8266通用的数字引脚
    // #define TFT_MISO 12 // 主读从写 暂不需要
    #define TFT_MOSI 13 // 主写从读
    #define TFT_SCLK 14 // 时钟
    #define TFT_CS   15  // 片选
    #define TFT_DC    2  // 数据/命令选择线
    #define TFT_RST  -1  // 这里连RST故设置为-1
    #define TOUCH_CS -1 // 触控的片选,这里暂不使用// 宽 高
    #define TFT_WIDTH  240
    #define TFT_HEIGHT 320// 以下设置为默认暂未修改
    #define LOAD_GLCD   // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
    #define LOAD_FONT2  // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
    #define LOAD_FONT4  // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
    #define LOAD_FONT6  // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
    #define LOAD_FONT7  // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-.
    #define LOAD_FONT8  // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
    #define LOAD_GFXFF  // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts
    #define SMOOTH_FONT
    #define SPI_FREQUENCY  27000000 // SPI频率
    #define SPI_TOUCH_FREQUENCY  2500000 // 字面意思 触控的SPI频率
    // ###########################################################################
    
  • ESP32 esp32并未测试多个不同的板子spi是否相等

    // ############# 直接在这里统一定义 屏幕和开发板/模块SPI引脚############
    #define USER_SETUP_LOADED // 使其它定义不生效
    #define ST7789_DRIVER
    // 定义SPI的引脚 以下为esp32 spi引脚 不确定是否通用
    // #define TFT_MISO -1 // 主读从写 暂不需要
    #define TFT_MOSI 23 // 主写从读
    #define TFT_SCLK 18 // 时钟
    #define TFT_CS   5  // 片选
    #define TFT_DC    2  // 数据/命令选择线
    #define TFT_RST  -1  // 这里连RST故设置为-1
    #define TOUCH_CS -1 // 触控的片选,这里暂不使用// 宽 高
    #define TFT_WIDTH  240
    #define TFT_HEIGHT 320// 以下设置为默认暂未修改
    #define LOAD_GLCD   // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
    #define LOAD_FONT2  // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
    #define LOAD_FONT4  // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
    #define LOAD_FONT6  // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
    #define LOAD_FONT7  // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-.
    #define LOAD_FONT8  // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
    #define LOAD_GFXFF  // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts#define SMOOTH_FONT
    #define SPI_FREQUENCY  27000000 // SPI频率
    #define SPI_TOUCH_FREQUENCY  2500000 // 字面意思 触控的SPI频率
    // ###########################################################################
    

    需要注意需要注意MOSI SCLK CS 这个三个用到引脚是否为默认SPI引脚。

引脚接线

BLK 是背光开关 可以不接MCU,该脚触碰芯片的金属屏蔽罩也能熄屏

ESP8266系列开发板

ST7789引脚 ESP8266数字引脚 Wemos D1 R1 Wemos d1 r2 & mini NodeMcu 12(E/F)
GND (地) - GND GND G
VCC (可以输入3.3或者5V) - 5V/3.3V 5V/3.3V 3V
SCl (SCK时钟) 14 D13/SCK/D5 D5 D5
SDA (数据线mcu到屏幕) 13 D11/MOSI/D7 D7 D7
RES (复位线) - RST RST RST
DC (数据/命令选择线) 2 D9 D4 D4
CS (片选) 15 D10/SS D8 D8

ESP32系列开发板

ST7789引脚 ESP32数字引脚 Wemos D1 R32
GND (地) - GND
VCC (可以输入3.3或者5V) - 5V/3.3V
SCl (SCK时钟) 18 IO18
SDA (数据线mcu到屏幕) 23 IO23
RES (复位线) - RST
DC (数据/命令选择线) 2 IO2
CS (片选) 5 IO5

代码

在库文件的examples目录很多。这里找了一个分辨率匹配的时钟例子。

#include <Arduino.h>
#include <SPI.h>
#include "TFT_eSPI.h" // Hardware-specific library#define TFT_GREY 0x5AEBTFT_eSPI tft = TFT_eSPI();       // Invoke custom libraryfloat sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;    // Saved H, M, S x & y multipliers
float sdeg = 0, mdeg = 0, hdeg = 0;
uint16_t osx = 120, osy = 120, omx = 120, omy = 120, ohx = 120, ohy = 120;  // Saved H, M, S x & y coords
uint16_t x0 = 0, x1 = 0, yy0 = 0, yy1 = 0;
uint32_t targetTime = 0;                    // for next 1 second timeoutstatic uint8_t conv2d(const char *p); // Forward declaration needed for IDE 1.6.x
uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6);  // Get H, M, S from compile timeboolean initial = 1;void setup(void) {tft.init();tft.setRotation(0);//tft.fillScreen(TFT_BLACK);//tft.fillScreen(TFT_RED);//tft.fillScreen(TFT_GREEN);//tft.fillScreen(TFT_BLUE);//tft.fillScreen(TFT_BLACK);tft.fillScreen(TFT_GREY);tft.setTextColor(TFT_WHITE, TFT_GREY);  // Adding a background colour erases previous text automatically// Draw clock facetft.fillCircle(120, 120, 118, TFT_GREEN);tft.fillCircle(120, 120, 110, TFT_BLACK);// Draw 12 linesfor (int i = 0; i < 360; i += 30) {sx = cos((i - 90) * 0.0174532925);sy = sin((i - 90) * 0.0174532925);x0 = sx * 114 + 120;yy0 = sy * 114 + 120;x1 = sx * 100 + 120;yy1 = sy * 100 + 120;tft.drawLine(x0, yy0, x1, yy1, TFT_GREEN);}// Draw 60 dotsfor (int i = 0; i < 360; i += 6) {sx = cos((i - 90) * 0.0174532925);sy = sin((i - 90) * 0.0174532925);x0 = sx * 102 + 120;yy0 = sy * 102 + 120;// Draw minute markerstft.drawPixel(x0, yy0, TFT_WHITE);// Draw main quadrant dotsif (i == 0 || i == 180) tft.fillCircle(x0, yy0, 2, TFT_WHITE);if (i == 90 || i == 270) tft.fillCircle(x0, yy0, 2, TFT_WHITE);}tft.fillCircle(120, 121, 3, TFT_WHITE);// Draw text at position 120,260 using fonts 4// Only font numbers 2,4,6,7 are valid. Font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : . - a p m// Font 7 is a 7 segment font and only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : .tft.drawCentreString("clock", 120, 260, 4);targetTime = millis() + 1000;
}void loop() {if (targetTime < millis()) {targetTime += 1000;ss++;              // Advance secondif (ss == 60) {ss = 0;mm++;            // Advance minuteif (mm > 59) {mm = 0;hh++;          // Advance hourif (hh > 23) {hh = 0;}}}// Pre-compute hand degrees, x & y coords for a fast screen updatesdeg = ss * 6;                  // 0-59 -> 0-354mdeg = mm * 6 + sdeg * 0.01666667;  // 0-59 -> 0-360 - includes secondshdeg = hh * 30 + mdeg * 0.0833333;  // 0-11 -> 0-360 - includes minutes and secondshx = cos((hdeg - 90) * 0.0174532925);hy = sin((hdeg - 90) * 0.0174532925);mx = cos((mdeg - 90) * 0.0174532925);my = sin((mdeg - 90) * 0.0174532925);sx = cos((sdeg - 90) * 0.0174532925);sy = sin((sdeg - 90) * 0.0174532925);if (ss == 0 || initial) {initial = 0;// Erase hour and minute hand positions every minutetft.drawLine(ohx, ohy, 120, 121, TFT_BLACK);ohx = hx * 62 + 121;ohy = hy * 62 + 121;tft.drawLine(omx, omy, 120, 121, TFT_BLACK);omx = mx * 84 + 120;omy = my * 84 + 121;}// Redraw new hand positions, hour and minute hands not erased here to avoid flickertft.drawLine(osx, osy, 120, 121, TFT_BLACK);osx = sx * 90 + 121;osy = sy * 90 + 121;tft.drawLine(osx, osy, 120, 121, TFT_RED);tft.drawLine(ohx, ohy, 120, 121, TFT_WHITE);tft.drawLine(omx, omy, 120, 121, TFT_WHITE);tft.drawLine(osx, osy, 120, 121, TFT_RED);tft.fillCircle(120, 121, 3, TFT_RED);}
}static uint8_t conv2d(const char *p) {uint8_t v = 0;if ('0' <= *p && *p <= '9')v = *p - '0';return 10 * v + *++p - '0';
}

可以看出主要就是利用TFT_eSPI相关函数进行绘制。

ESP8266/ESP32 驱动ST7789屏幕相关推荐

  1. 使用ESP32驱动ST7789,效果很好的IPS显示屏

    前言 在平台上面购买了这一种模块,然后这种模块是IPS液晶屏,显示效果会比早期那种好一些.显示屏的控制芯片是ST7789 我准备使用ESP32进行驱动,测试上使用简单的Arduino IDE,那么具体 ...

  2. ESP32 驱动ST7789 240*240 1.3寸 TFT 显示屏

    百问网LVGL中文开发手册 STM32之littlevGL系列教程: 图形用户界面(GUI) 微雪课堂 正点原子手把手教你学littleVGL[轻量级开源GUI]-基于STM32_哔哩哔哩_bilib ...

  3. ESP8266/ESP32/nodeMcu/wemos D1 MINI开发板用TFT_eSPI库驱动ST7789(240*240)TFT显示屏

    ESP8266/ESP32/nodeMcu/wemos D1 MINI开发板用TFT_eSPI库驱动ST7789(240*240)TFT显示屏 材料 硬件连接 引脚连接表 引脚连接图 安装TFT_eS ...

  4. ESP8266使用TFT-eSPI驱动ST7789 240*240 1.3寸 TFT

    目录 一.安装库 二.屏幕连接到ESP8266 三.修改TFT库文件为我所用 四.如何控制屏幕亮度? 屏幕介绍参照:https://blog.csdn.net/yulusilian1/article/ ...

  5. ⑥ESP8266 开发学习笔记_By_GYC 【ESP8266 驱动 OLED屏幕】

    ⑥ESP8266 开发学习笔记_By_GYC [ESP8266 驱动 OLED屏幕] 目录 ⑥ESP8266 开发学习笔记_By_GYC [ESP8266 驱动 OLED屏幕] 适用范围 user_c ...

  6. ESP8266+MicroPython开发:ESP8266使用硬件SPI驱动TFT屏幕(ST7735)

    由于上次模拟SPI驱动TFT屏幕太慢,所以,本次采用硬件SPI来进行驱动,但是,当我直接把对应SPI接口更换时,发现TFT屏幕驱动并没有因此得到好转,依然是非常的满,然后只能从对应花点铺色,显示字符串 ...

  7. ESP8266(NodeMCU)+Lua语言+IIC驱动OLED屏幕

    前言 本来打算用Arduino IDE驱动OLED屏幕,但是因为在家无法安装Arduino的第三方库,改用ESPlorer和Lua语言驱动. 硬件准备 1. NodeMCU开发板 2. OLED屏幕( ...

  8. esp8266使用128x128 ST7735屏幕像素偏移问题处理

    esp8266使用128x128 ST7735屏幕像素偏移处理 修改TFT_eSPI库 一.安装TFT_eSPI库 二.修改User_Setup.h 接线对照表 最后打开例程烧录测试 修改TFT_eS ...

  9. ESP32-IDF开发笔记 | 03 - 使用SPI外设驱动ST7789 SPILCD

    一.硬件说明 ST7789屏幕引脚 ESP32C3F引脚 3V3 3V3 GND GND MOSI IO_07 CLK IO_06 DC IO_08 RST IO_04 BL IO_05 二.ESP3 ...

  10. WT32-SC01是ESP32驱动3.5彩屏开发板方案适合用arduino方式开发吗?因为需要彩屏和电容触摸的驱动的

    ESP32驱动3.5寸彩屏开发板方案因为带有彩屏和电容触摸的驱动,能否用arduino方式开发,这是很多熟悉arduino开发的技术人员关心的问题. 目前启明云端推出的http://esp32.8ms ...

最新文章

  1. [JAVA EE] JPA 查询用法:自定义查询,分页查询
  2. 【全网最精简写法】ES6获取浏览器url跟参
  3. 可交互的对抗网络如何增强人类创造力?
  4. [jvm]对象访问机制
  5. E. 存储过程(procedure)
  6. matlab机械臂工作空间代码_【ROS-Moveit!】机械臂控制探索(3)——基于python的API示例代码分析...
  7. HDU2012 素数判定
  8. POI写Word换行
  9. 程序员被公司开除,隔阵子领导命令回前公司讲解代码,网友直呼:关我嘛事?
  10. Linux Desktop Entry 文件深入解析
  11. nc加载不了java,用友NC系统使用过程中常见问题和解决方法
  12. camtasia studio2022新版本高清录屏编辑无水印
  13. 大数据Flink面试考题___Flink高频考点,万字超全整理(建议)
  14. 数据库的三才阵——人
  15. CodeLite 16无法自动代码补全
  16. 10天学会PHP之PHP快速入门
  17. 程序员如何通过造轮子走向人生巅峰?
  18. python关键字中文意思_python解析URL中文关键字
  19. OpenCV实战(7)——OpenCV色彩空间转换
  20. 不想多挣钱的程序员都有罪!!!

热门文章

  1. cookie的工作原理
  2. 小程序父子组件间传值(微信/支付宝/钉钉)
  3. win7计算机怎么重置,win7系统快速重置的设置办法
  4. 新能源车提车、上牌流程
  5. Android跨进程通信——AIDL原理解析
  6. rust执行cmd命令隐藏窗口
  7. java.util.TaskQueue的最小堆排序算法的应用
  8. 工作中linux安装打印机和扫描仪
  9. Linux 系统启动与服务管理
  10. 光缆弹性模量计算_光缆的基本常识