零、前言
最近在搞tft屏幕,遇到了非常多的困难,这个笔记主要帮助的是和我一样比较小白的单片机初学者,在这里我用到的板子是ESP32,用的屏幕是单工SPI通信协议的一块屏幕。
一、准备工作
在这里我们准备写入驱动程序,这块屏幕用到的驱动时ST7735,所以把不用的全部注释掉,具体的配置这里不再过多解释,关键是设置引脚的连接。
查阅乐鑫官方的文档我们就可以知道,那些引脚可以作为SDA(数据传输),CS(片选),SCK(时钟线),DC(指令选择引脚),注意,ESP32的6到11号引脚虽然可以传输数据,但是一般不用,因为这些引脚是要用于flash的数据传输的
// For ESP32 Dev board (only tested with GC9A01 display)
// The hardware SPI can be mapped to any pins
// #define TFT_MISO 19
// #define TFT_MOSI 21
// #define TFT_SCLK 22
// #define TFT_CS 5
// #define TFT_DC 25
// #define TFT_RST 26#define TFT_MOSI 15 // In some display driver board, it might be written as "SDA" and so on.
#define TFT_SCLK 14
#define TFT_CS 5 // Chip select control pin
#define TFT_DC 25 // Data Command control pin
#define TFT_RST 26 // Reset pin (could connect to Arduino RESET pin)
#define TFT_BL 22 // LED back-light//#define TOUCH_CS 21 // Chip select pin (T_CS) of touch screen//#define TFT_WR 22 // Write strobe for modified Raspberry Pi TFT only// For the M5Stack module use these #define lines
//#define TFT_MISO 19
//#define TFT_MOSI 23
//#define TFT_SCLK 18
//#define TFT_CS 14 // Chip select control pin
//#define TFT_DC 27 // Data Command control pin
//#define TFT_RST 33 // Reset pin (could connect to Arduino RESET pin)
//#define TFT_BL 32 // LED back-light (required for M5Stack)
这里要注意,你使用的屏幕上面有哪几个引脚就选用哪种方法,把其他的不用的全部注释掉。然后ESP32可以连接屏幕的引脚组合非常多,接到那个引脚把后面的引脚改一下,就可以正常驱动了,再然后就是利用arduino或者plantformio进行编程,这里写一下常用的函数,做一个总结:
tft.init(); //初始化tft.fillScreen(TFT_WHITE); //屏幕颜色tft.setCursor(1, 10, 2); //设置起始坐标(10, 10),2 号字体tft.setTextColor(TFT_BLUE); //设置文本颜色为白色tft.setTextSize(1); //设置文字的大小 (1~7)tft.println("Two roads diverged in a wood,and "); //显示文字tft.drawLine(10,50,118,50,TFT_WHITE); //画线tft.drawPixel(70,70,TFT_RED);//画点tft.setTextColor(TFT_WHITE,TFT_BLUE);//设置文字颜色和背景颜色
参考文档:
esp32-wroom-32_datasheet_cn.pdf (espressif.com)(ESP32技术规格书)