备注:文档写的不是特别明细,主要是记录学习的成果,
1,配置好platformio环境:选择开发型号为airm2m_core_esp32c3,Arduino开发框架。
2,安装TFT_eSPI,并修改TFT_eSPI目录下的User_Setup.h
主要修改如下:
#define TFT_WIDTH 128
#define TFT_HEIGHT 160
#define ST7735_GREENTAB2
#define TFT_INVERSION_OFF
#define TFT_BACKLIGHT_ON HIGH
#define TFT_MOSI 3---sdi(spi_mosi=sdi) // In some display driver board, it might be written as "SDA" and so on.
#define TFT_SCLK 2---sck(spi_ck=sck)
#define TFT_CS 7---cs // Chip select control pin
#define TFT_DC 6---dc // Data Command control pin
#define TFT_RST 10---reset // Reset pin (could connect to Arduino RESET pin)
#define SPI_FREQUENCY 27000000 //太高会出问题
//---后面对应的是显示屏上的引脚(数字是针脚再对应显示转接板上面的参数))
3,在测试案例中找一个测试demo,例如:
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
uint32_t targetTime = 0; // for next 1 second timeout
byte omm = 99;
bool initial = 1;
byte xcolon = 0;
unsigned int colour = 0;
static uint8_t conv2d(const char *p)
{
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
v = *p - '0';
return 10 * v + *++p - '0';
}
uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6); // Get H, M, S from compile time
void setup(void)
{
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour
targetTime = millis() + 1000;
}
void loop()
{
if (targetTime < millis())
{
targetTime = millis() + 1000;
ss++; // Advance second
if (ss == 60)
{
ss = 0;
omm = mm;
mm++; // Advance minute
if (mm > 59)
{
mm = 0;
hh++; // Advance hour
if (hh > 23)
{
hh = 0;
}
}
}
if (ss == 0 || initial)
{
initial = 0;
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.setCursor(8, 52);
tft.print(__DATE__); // This uses the standard ADAFruit small font
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.drawCentreString("It is windy", 120, 48, 2); // Next size up font 2
// tft.setTextColor(0xF81F, TFT_BLACK); // Pink
// tft.drawCentreString("12.34",80,100,6); // Large font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 . : a p m
}
// Update digital time
byte xpos = 6;
byte ypos = 0;
if (omm != mm)
{ // Only redraw every minute to minimise flicker
// Uncomment ONE of the next 2 lines, using the ghost image demonstrates text overlay as time is drawn over it
tft.setTextColor(0x39C4, TFT_BLACK); // Leave a 7 segment ghost image, comment out next line!
// tft.setTextColor(TFT_BLACK, TFT_BLACK); // Set font colour to black to wipe image
// Font 7 is to show a pseudo 7 segment display.
// Font 7 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 0 : .
tft.drawString("88:88", xpos, ypos, 7); // Overwrite the text to clear it
tft.setTextColor(0xFBE0); // Orange
omm = mm;
if (hh < 10)
xpos += tft.drawChar('0', xpos, ypos, 7);
xpos += tft.drawNumber(hh, xpos, ypos, 7);
xcolon = xpos;
xpos += tft.drawChar(':', xpos, ypos, 7);
if (mm < 10)
xpos += tft.drawChar('0', xpos, ypos, 7);
tft.drawNumber(mm, xpos, ypos, 7);
}
if (ss % 2)
{ // Flash the colon
tft.setTextColor(0x39C4, TFT_BLACK);
xpos += tft.drawChar(':', xcolon, ypos, 7);
tft.setTextColor(0xFBE0, TFT_BLACK);
}
else
{
tft.drawChar(':', xcolon, ypos, 7);
colour = random(0xFFFF);
// Erase the old text with a rectangle, the disadvantage of this method is increased display flicker
tft.fillRect(0, 64, 160, 20, TFT_BLACK);
tft.setTextColor(colour);
tft.drawRightString("Colour", 75, 64, 4); // Right justified string drawing to x position 75
String scolour = String(colour, HEX);
scolour.toUpperCase();
char buffer[20];
scolour.toCharArray(buffer, 20);
tft.drawString(buffer, 82, 64, 4);
}
}
}
4、可能出现的问题
4.1:How to fix PlatformIO Adafruit_BusIO_Register.h: No Such File or Directory
解决方法就是在platformio.ini 文件中添加配置,例如(主要是lib_deps下面):
debug_speed = 5000
lib_deps =
moononournation/GFX Library for Arduino@^1.3.7
bodmer/TFT_eSPI@^2.5.31
adafruit/Adafruit INA219 @ ^1.1.1
adafruit/Adafruit BusIO @ ^1.9.3
Wire
4.2 DMA is not supported on the ESP32 C3 (possible future update) [-Wcpp]
暂时解决方法是在TFT_eSPI.h中把
#include "Processors/TFT_eSPI_ESP32_C3.h"
改成 #include "Processors/TFT_eSPI_ESP32.h"
最后发现没报错,但是不正常显示,最终方法是“重启编辑器”解决