创意 · 2023/07/03 0

基于合宙ESP32S3和C3的Arduino GFX点亮0.96英寸LCD屏幕教程

基于ArduinoGFX合宙ESP32S3和C3点亮0.96英寸LCD屏幕

在Arduino IDE中先添加开发板链接,然后下载ESP32支持包。这一步网上教程有很多。

然后就是添加两个库。

请注意以下部分代码,根据C3或S3来注释不同的代码,注意开发板的选择与下载模式的选择。

//合宙C3,板子选择EP32C3 DEV MODULE,使用DIO模式
//Arduino_DataBus *bus = new Arduino_ESP32SPI(6 /* DC */, 7 /* CS */, 2 /* SCK */, 3 /* MOSI */, GFX_NOT_DEFINED /* MISO */);
//合宙S3,板子选择EP32S3 DEV MODULE 
//Arduino_DataBus *bus = new Arduino_ESP32SPI(15 /* DC */, 14 /* CS */, 18 /* SCK */, 17 /* MOSI */, GFX_NOT_DEFINED /* MISO */);

以下是完整代码:

#define SPI_FREQUENCY  40000000
#include <Arduino_GFX_Library.h>
 
//合宙C3,板子选择EP32C3 DEV MODULE,使用DIO模式
//Arduino_DataBus *bus = new Arduino_ESP32SPI(6 /* DC */, 7 /* CS */, 2 /* SCK */, 3 /* MOSI */, GFX_NOT_DEFINED /* MISO */);
//合宙S3,板子选择ESP32-S3-BOX 
//Arduino_DataBus *bus = new Arduino_ESP32SPI(15 /* DC */, 14 /* CS */, 18 /* SCK */, 17 /* MOSI */, GFX_NOT_DEFINED /* MISO */);
Arduino_GFX *gfx = new Arduino_ST7735(bus, 10 /*RST*/, 3 /* rotation */, false /* IPS */, 80 /* width */, 160 /* height */, 26 /* col offset 1 */, 1 /* row offset 1 */, 26 /* col offset 2 */, 1 /* row offset 2 */);
int32_t w, h, n, n1, cx, cy, cx1, cy1, cn, cn1;
uint8_t tsa, tsb, tsc, ds;
 
void setup()
{
  Serial.begin(115200);
  // Serial.setDebugOutput(true);
  // while(!Serial);
  Serial.println("Arduino_GFX library Test!");
 
#ifdef GFX_PWD
  pinMode(GFX_PWD, OUTPUT);
  digitalWrite(GFX_PWD, HIGH);
#endif
 
  gfx->begin();
  // gfx->begin(80000000); /* specify data bus speed */
 
  w = gfx->width();
  h = gfx->height();
  n = min(w, h);
  n1 = n - 1;
  cx = w / 2;
  cy = h / 2;
  cx1 = cx - 1;
  cy1 = cy - 1;
  cn = min(cx1, cy1);
  cn1 = cn - 1;
  tsa = ((w <= 176) || (h <= 160)) ? 1 : (((w <= 240) || (h <= 240)) ? 2 : 3); // text size A
  tsb = ((w <= 272) || (h <= 220)) ? 1 : 2;                                    // text size B
  tsc = ((w <= 220) || (h <= 220)) ? 1 : 2;                                    // text size C
  ds = (w <= 160) ? 9 : 12;                                                    // digit size
 
#ifdef GFX_BL
  pinMode(GFX_BL, OUTPUT);
  digitalWrite(GFX_BL, HIGH);
#endif
}

static inline uint32_t micros_start() __attribute__((always_inline));
static inline uint32_t micros_start()
{
  uint8_t oms = millis();
  while ((uint8_t)millis() == oms)
    ;
  return micros();
}
 
void loop(void)
{
  gfx->fillScreen(WHITE);
  gfx->setCursor(0, 0);
 
  gfx->setTextSize(1);
  gfx->setTextColor(BLACK, WHITE);
  gfx->println(F("Hello World!"));
 
  gfx->setTextSize(2);
  gfx->setTextColor(gfx->color565(0xff, 0xff, 0x00));
  gfx->print(F("LFF "));
  gfx->setTextColor(gfx->color565(0x00, 0xff, 0xff));
  gfx->print(F("DON'T    "));
  gfx->setTextColor(gfx->color565(0xff, 0x00, 0xff));
  gfx->println(F("WANNA REVIEW"));
 
  gfx->setTextSize(tsa);
  gfx->setTextColor(YELLOW);
  gfx->println(123456);
 
  gfx->setTextColor(WHITE);
  gfx->println((w > 128) ? 0xDEADBEEF : 0xDEADBEE, HEX);
 
  gfx->setTextColor(CYAN, WHITE);
  gfx->println(F("Groop,"));
 
  gfx->setTextSize(tsc);
  gfx->setTextColor(MAGENTA, WHITE);
  gfx->println(F("I implore thee,"));
 
  gfx->setTextSize(1);
  gfx->setTextColor(NAVY, WHITE);
  gfx->println(F("my foonting turlingdromes."));
  
  while(1);
}