【雕爷学编程】Arduino动手做(153)---2.4寸TFT

2023-07-10  本文已影响0人  驴友花雕

37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板

18.jpg

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板

项目之六:逐行显示不同字号的英文字符串

实验开源代码:

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目之六:逐行显示不同字号的英文字符串

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/



#include <LCDWIKI_GUI.h> //导入核心图形库

#include <LCDWIKI_KBV.h> //导入特定硬件库

//如果 IC 模型已知或模块不可读,则可以使用此构造函数

LCDWIKI_KBV mylcd(ILI9341, A3, A2, A1, A0, A4); //模型、CS、CD、WR、RD、重置

//如果 IC 模型未知且模块可读,则可以使用此构造函数

//LCDWIKI_KBV my_lcd(240,320,A3,A2,A1,A0,A4);//屏幕宽度、高度、cs、cd、wr、rd、重置

//定义一些颜色值

#define BLACK  0x0000

#define BLUE  0x001F

#define RED   0xF800

#define GREEN  0x07E0

#define CYAN  0x07FF

#define MAGENTA 0xF81F

#define YELLOW 0xFFE0

#define WHITE  0xFFFF

void setup()

{

 Serial.begin(9600);

 mylcd.Init_LCD();

 Serial.println(mylcd.Read_ID(), HEX);

 mylcd.Fill_Screen(BLACK);

}

void loop()

{

 mylcd.Set_Text_Mode(0);

 //显示 1 次字符串

 mylcd.Fill_Screen(0x0000);

 mylcd.Set_Text_colour(RED);

 mylcd.Set_Text_Back_colour(BLACK);

 mylcd.Set_Text_Size(1);

 mylcd.Print_String("Hello World!", 0, 0);

 mylcd.Print_Number_Float(01234.56789, 2, 0, 8, '.', 0, ' ');

 mylcd.Print_Number_Int(0xDEADBEF, 0, 16, 0, ' ', 16);

 //mylcd.Print_String("DEADBEF", 0, 16);

 delay(166);

 //显示 2 次字符串

 mylcd.Set_Text_colour(GREEN);

 mylcd.Set_Text_Size(2);

 mylcd.Print_String("Hello World!", 0, 40);

 mylcd.Print_Number_Float(01234.56789, 2, 0, 56, '.', 0, ' ');

 mylcd.Print_Number_Int(0xDEADBEF, 0, 72, 0, ' ', 16);

 //mylcd.Print_String("DEADBEEF", 0, 72);

 delay(166);

 //显示 3 次字符串

 mylcd.Set_Text_colour(BLUE);

 mylcd.Set_Text_Size(3);

 mylcd.Print_String("Hello World!", 0, 104);

 mylcd.Print_Number_Float(01234.56789, 2, 0, 128, '.', 0, ' ');

 mylcd.Print_Number_Int(0xDEADBEF, 0, 152, 0, ' ', 16);

 // mylcd.Print_String("DEADBEEF", 0, 152);

 delay(166);

 //显示 4 次字符串

 mylcd.Set_Text_colour(WHITE);

 mylcd.Set_Text_Size(4);

 mylcd.Print_String("Hello!", 0, 192);

 delay(166);

 //显示 5 次字符串

 mylcd.Set_Text_colour(YELLOW);

 mylcd.Set_Text_Size(5);

 mylcd.Print_String("Hello!", 0, 224);

 delay(166);

 //显示 6 次字符串

 mylcd.Set_Text_colour(RED);

 mylcd.Set_Text_Size(6);

 mylcd.Print_String("Hello!", 0, 266);

 delay(666);

}

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

项目之六:逐行显示不同字号的英文字符串

WeChat_20210628114730.gif

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

项目之七:诊断接触针

实验开源代码:

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

  实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目之七:诊断接触针

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/



void showpins(int A, int D, int value, const char *msg) {

 Serial.print(msg);

 Serial.print(" (A" + String(A - A0) + ", D" + String(D) + ") = ");

 Serial.println(value);

}

void setup() {

 int i, j, value, Apins[2], Dpins[2], Values[2], found = 0;

 Serial.begin(9600);

 Serial.println("Making all control and bus pins INPUT_PULLUP");

 Serial.println("Typical 30k Analog pullup with corresponding pin");

 Serial.println("would read low when digital is written LOW");

 Serial.println("e.g. reads ~25 for 300R X direction");

 Serial.println("e.g. reads ~30 for 500R Y direction");

 Serial.println("");

 for (i = A0; i < A5; i++) pinMode(i, INPUT_PULLUP);

 for (i = 2; i < 10; i++) pinMode(i, INPUT_PULLUP);

 for (i = A0; i < A4; i++) {

  for (j = 5; j < 10; j++) {

   pinMode(j, OUTPUT);

   digitalWrite(j, LOW);

   value = analogRead(i);  // 忽略初读

   value = analogRead(i);

   if (value < 100) {

    showpins(i, j, value, "Testing :");

    if (found < 2) {

     Apins[found] = i;

     Dpins[found] = j;

     Values[found] = value;

     found++;

    }

   }

   pinMode(j, INPUT_PULLUP);

  }

 }

 if (found == 2) {

  Serial.println("Diagnosing as:-");

  for (i = 0; i < 2; i++) {

   showpins(Apins[i], Dpins[i], Values[i], (Values[i] < Values[!i]) ? "XM,XP: " : "YP,YM: ");

  }

 }

}

void loop() {

}
56.jpg

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

项目之八:纵向流动显示字符串

实验开源代码:

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目之八:纵向流动显示字符串

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/



#include <Adafruit_GFX.h> // 导入特定硬件的库

#include <MCUFRIEND_kbv.h>

MCUFRIEND_kbv tft;

#define BLACK  0x0000

#define BLUE  0x001F

#define RED   0xF800

#define GREEN  0x07E0

#define CYAN  0x07FF

#define MAGENTA 0xF81F

#define YELLOW 0xFFE0

#define WHITE  0xFFFF

// 在行号中工作。 以 16 为单位的字体高度

int16_t ht = 16, top = 3, line, lines = 15, scroll;

void setup() {

 tft.reset();

 uint16_t id = tft.readID();

 tft.begin(id);

 tft.setRotation(0);   //肖像

 tft.fillScreen(BLACK);

 tft.setTextColor(WHITE, BLACK);

 tft.setTextSize(2);   // 系统字体为 8 像素。 ht = 8*2=16

 tft.setCursor(100, 0);

 tft.print("ID = 0x");

 tft.println(id, HEX);

 if (id == 0x9320 || id == 0x9325 || id == 0xB509) {

  top = 0;           // 这些控制器滚动全屏

  lines = tft.height() / ht;  // 我们处于纵向模式

 }

 if (id == 0x7783) {

  tft.println("can NOT scroll");

  while (1);          // 休止

 }

 tft.setCursor(0, 0);

 for (line = 1; line < 21; line++) tft.println(String(line) + ": ");

}

void loop(){

 tft.setCursor(0, (scroll + top) * ht);

 if (++scroll >= lines) scroll = 0;

 tft.vertScroll(top * ht, lines * ht, (scroll) * ht);

 tft.println(String(line) + ": [" + String(scroll) + "] ");

 delay(100);

 line++;

}

Arduino实验场景图

https://imagemc.dfrobot.com.cn/data/attachment/forum/202106/28/125955hrd1ln1n7xrcmg7g.gif

WeChat_20210628125656.gif

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

项目之九:动态背景四面循环显示字符串

Arduino实验开源代码

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

  实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目之九:动态背景四面循环显示字符串

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/



#include <Adafruit_GFX.h> // 导入基本图形库

#include <MCUFRIEND_kbv.h>

MCUFRIEND_kbv tft;

void setup(){

  Serial.begin(9600);

  tft.reset();

  uint16_t identifier = tft.readID();

  Serial.print("ID = 0x");

  Serial.println(identifier, HEX);

  if (identifier == 0xEFEF) identifier = 0x9486;

  tft.begin(identifier);

  // tft fill Screen (BLACK);

}

char *msg[] = { "PORTRAIT", "LANDSCAPE", "PORTRAIT_REV", "LANDSCAPE_REV" };

uint8_t aspect;

void loop(){

  uint16_t x = 50, y = 100;

  tft.setRotation(aspect);

  tft.fillScreen(0x0000);

  tft.setCursor(0, 0);

  tft.setTextSize(2);

  tft.println(msg[aspect]);

  tft.setCursor(x, y);

  tft.println("[x=" + String(x) + ",y=" + String(y) + "]");

  delay(800);

  tft.println("INVERT ON");

  tft.invertDisplay(true);

  delay(250);

  tft.invertDisplay(false);

  tft.println("INVERT OFF");

  delay(250);

  if (++aspect >= 4) aspect = 0;

}

Arduino实验场景图

https://mc.dfrobot.com.cn/data/attachment/forum/202106/28/135424yzt2775hhjbth8mf.gif

WeChat_20210628133416.gif

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

项目之十:逐行显示不同字号的英文字符串

Arduino实验开源代码

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目之十:逐行显示不同字号的英文字符串

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/



#include <Adafruit_GFX.h>  // 导入核心图形库

#include <MCUFRIEND_kbv.h>  // 导入特定硬件库

MCUFRIEND_kbv tft;

#include <Fonts/FreeSans9pt7b.h> //导入字体库

#include <Fonts/FreeSans12pt7b.h>

#include <Fonts/FreeSerif12pt7b.h>

#include <FreeDefaultFonts.h>

#define BLACK  0x0000

#define RED   0xF800

#define GREEN  0x07E0

#define WHITE  0xFFFF

#define GREY  0x8410

void setup(void){

  Serial.begin(9600);

  uint16_t ID = tft.readID();

  Serial.println("Example: Font_simple");

  Serial.print("found ID = 0x");

  Serial.println(ID, HEX);

  Serial.println("The display font is ready OK!");

  if (ID == 0xD3D3) ID = 0x9481; //如果只写显示,则强制 ID

  tft.begin(ID);

  tft.setRotation(0);

}

void loop(void){

  tft.fillScreen(BLACK);

  showmsgXY(20, 10, 1, NULL, "System x1");

  showmsgXY(20, 24, 2, NULL, "System x2");

  showmsgXY(20, 60, 1, &FreeSans9pt7b, "FreeSans9pt7b");

  showmsgXY(20, 80, 1, &FreeSans12pt7b, "FreeSans12pt7b");

  showmsgXY(20, 100, 1, &FreeSerif12pt7b, "FreeSerif12pt7b");

  showmsgXY(20, 120, 1, &FreeSmallFont, "FreeSmallFont");

  showmsgXY(5, 180, 1, &FreeSevenSegNumFont, "01234");

  showmsgXY(5, 190, 1, NULL, "System Font is drawn from topline");

  tft.setTextColor(RED, GREY);

  tft.setTextSize(2);

  tft.setCursor(0, 220);

  tft.print("7x5 can overwrite");

  delay(50);

  tft.setCursor(0, 220);

  tft.print("if background set");

  delay(50);

  showmsgXY(5, 260, 1, &FreeSans9pt7b, "Free Fonts from baseline");

   delay(50);

  showmsgXY(5, 285, 1, &FreeSans9pt7b, "Free Fonts transparent");

  delay(50);

  showmsgXY(5, 310, 1, &FreeSans9pt7b, "erase backgnd with fillRect()");

  delay(300);

}

void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)

{

  int16_t x1, y1;

  uint16_t wid, ht;

  tft.drawFastHLine(0, y, tft.width(), WHITE);

  tft.setFont(f);

  tft.setCursor(x, y);

  tft.setTextColor(GREEN);

  tft.setTextSize(sz);

  tft.print(msg);

  delay(500);

}

实验串口返回情况

21.jpg

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

项目之十:逐行显示不同字号的英文字符串

Arduino实验场景图

WeChat_20210628150433.gif

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

项目之十一:四方向满屏动态字符串

实验开源代码:

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

 项目之十一:四方向满屏动态字符串

 模块直插,引脚用法如下:

 LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK

 Arduino Uno A3 A2 A1 A0 A4 10 11 12 13

 LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7

 Arduino Uno 8 9 2 3 4 5 6 7

*/

#define LCD_CS A3 // Chip Select goes to Analog 3

#define LCD_CD A2 // Command/Data goes to Analog 2

#define LCD_WR A1 // LCD Write goes to Analog 1

#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#include <Adafruit_GFX.h> // Hardware-specific library

#include <MCUFRIEND_kbv.h>

MCUFRIEND_kbv tft;

//#include <Adafruit_TFTLCD.h> // Hardware-specific library

//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

//Adafruit_TFTLCD tft;

//为一些常见的 16 位颜色值分配可读的名称

#define    BLACK  0x0000

#define    BLUE  0x001F

#define    RED   0xF800

#define    GREEN  0x07E0

#define CYAN  0x07FF

#define MAGENTA 0xF81F

#define YELLOW 0xFFE0

#define WHITE  0xFFFF

void setup(void);

void loop(void);

uint16_t g_identifier;

void setup(void) {

 tft.reset();

 g_identifier = tft.readID();

 Serial.begin(9600);

 Serial.print("readID = 0x");

 Serial.println(g_identifier, HEX);

 if (g_identifier == 0xFFFF) g_identifier = 0x9341;

 if (g_identifier == 0) {

  Serial.println("Unknown ID");

  while (1);

 }

 tft.begin(g_identifier);

 tft.setRotation(0);

}

uint16_t colors[] = {

 BLACK, BLUE

};

void colordump(uint16_t x, uint16_t y)

{

 uint16_t pixel, pixels[32];

 char i, j, buf[20], dirty;

 uint8_t wid = (tft.width() - 9 * 6) / (5 * 6), ht = (tft.height() / 8) - 1;

 tft.setTextColor(WHITE);

 tft.setTextSize(1);

 for (j = 0; j < ht; j++) {

  sprintf(buf, "%3d,%3d:", x, y + j);

  tft.print(buf);

  dirty = 1;

  for (i = 0; i < wid; i++) {

#if 1 && defined(MCUFRIEND_KBV_H_)

   if (dirty) tft.readGRAM(x, y + j, pixels, wid, 1);

   dirty = 0;

   pixel = pixels[i];

#else

   pixel = tft.readPixel(x + i, y + j);

#endif

   tft.print(" ");

   if (pixel == WHITE) tft.setTextColor(GREEN);

   sprintf(buf, "%04X", pixel);

   tft.print(buf);

   tft.setTextColor(WHITE);

  }

  tft.println();

 }

}

uint16_t bgr(uint16_t color) {

 return ((color & 0xF800) >> 11) | (color & 0x7E0) | (color & 0x1F) << 11;

}

void duffcolor(uint16_t color) {

 uint16_t pixel, x, y;

 char done, buf[20];

 uint16_t BGR = bgr(color);

 for (done = 0, y = 0; y < 320 && !done; y++) {

  for (x = 0; x < 240; x++) {

   //pixel = readxy(x, y);

   pixel = tft.readPixel(x, y);

   if (pixel != BGR) {

    done = 1;

    sprintf(buf, "0x%04X @ %d, %d", pixel, x, y);

    tft.println(buf);

    break;

   }

  }

 }

}

uint8_t aspect;

char *aspectname[] = {

 "PORTRAIT", "LANDSCAPE", "PORTRAIT_REV", "LANDSCAPE_REV"

};

void loop(void) {

 uint16_t iter, color;

 char buf[80];

 aspect = (aspect + 1) & 3;

 tft.setRotation(aspect);

 for (iter = 0; iter < sizeof(colors) / sizeof(uint16_t); iter++) {

  color = colors[iter];

  tft.fillScreen(color);

  tft.setTextColor(WHITE);

  tft.setTextSize(1);

  tft.setCursor(0, 0);

  sprintf(buf, "ID=0x%04X Background=%04X %s",

      tft.readID(), color, aspectname[aspect]);

  tft.println(buf);

  colordump(6 * 6, 0);

  //duffcolor(color);

  delay(400);

 }

}

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏

项目之十一:四方向满屏动态字符串

实验场景图

WeChat_20210628155902 (1).gif
上一篇下一篇

猜你喜欢

热点阅读