风筝
发表于: 2020-10-19 20:50:18 | 显示全部楼层

大家好,欢迎来到今天的Arduino教程。在本篇文章中,我们将向您展示如何在Arduino开发板上使用3.2寸彩色TFT显示屏。


每个项目的显示需求都是独特的,一个项目可能只需要一个简单的单字符OLED显示屏,而另一个项目可能需要一个尺寸大的显示屏,所有这些都是根据要执行的功能来决定。因此,作为制造商或电子爱好者,任何人都需要知道如何使用尽可能多种的显示屏,这就是今天的原因,我们将看看如何在Arduino开发板上使用超便宜的3.2寸彩色TFT显示屏。

Capture.jpg


在本篇文章种,我们将使用3.2寸TFT显示屏。该显示屏基于HX8357B LCD控制器,支持16线DataBus接口,480 x 320分辨率,提供262K颜色。该模块包括一个SD卡插槽、一个SPI FLASH电路以及一个5V-3.3V电源和逻辑电平转换电路,可轻松与任何使用5v或3.3v逻辑电平的微控制器一起使用。该模块可以直接插入Arduino Mega或Due开发板上。为演示显示屏的工作原理,我们将使用UTFT LCD库在显示屏上显示一些图像和文本,包括动画。所有这些都将说明如何将显示屏用于诸如示波器之类的设备。


所需的组件

●    3.2寸TFT显示屏

●    Arduino Due开发板

●    Arduino Mega开发板


原理图

显示屏采用扩展板的方式,这意味着可以将其直接插入要使用的Arduino,因此不需要原理图。如下图所示,将显示器屏插入Arduino Mega或Due。

sch.jpg


代码

为了实现本文的目标,我们将使用UTFT库中自带的简单示例代码。 UTFT库是为了方便微控制器和LCD显示屏之间的交互而制作的库。不幸的是,最新版本的UTFT库不支持用于3.2英寸TFT显示屏的HX8357B LCD控制器。为了解决这个问题,我们将在Arduino IDE上安装该库的以前版本。Henning Karlsen编写的库非常棒,可以从下面的链接下载。这些库是为每个Arduino开发板预先构建的,因此请从下面的链接中选择与您正在使用的开发板相匹配的正确库。

●    Arduino Mega开发板:https://github.com/Bodmer/TFT_HX8357

●    Arduino Due开发板:https://github.com/Bodmer/TFT_HX8357_Due


在下载并启动Arduino IDE实例后,使用您喜欢的库安装方法来安装库。打开IDE,单击文件,选择示例,选择UTFT,然后选择Display Demo或UTFT_Demo_480x320示例。


我们将尝试对代码进行简要说明。该代码首先将其运行速度(变量WAIT)设置为2000。此速度可以降低为零,因此演示可以缓慢播放。之后,我们包含utft库并为Arduino Due调用自定义库。

  1. #define WAIT 2000 // Delay between tests, set to 0 to demo speed, 2000 to see what it does!

  2. #define CENTRE 240

  3. #include <TFT_HX8357_Due.h> // Hardware-specific library

  4. TFT_HX8357_Due tft = TFT_HX8357_Due();       // Invoke custom library
复制代码

接下来,我们为要使用的字体指定初始颜色。应当注意,要使用自定义字体,必须通过编辑库中的User_Setup.h文件将它们预先加载到库中。

  1. #define TFT_GREY 0x7BEF
复制代码

完成后,我们开始void setup()函数。在setup()函数中,我们使用init命令初始化LCD,并使用set rotation(1)函数确保LCD显示屏处于横向视角。

  1. void setup()
  2. {
  3.   randomSeed(analogRead(5));
  4.   Serial.begin(38400);
  5. // Setup the LCD
  6.   tft.init();
  7.   tft.setRotation(1);
  8. }
复制代码

接下来是void loop()函数,可以将其用作函数以及如何使用它们的参考。 void loop函数使用了许多函数,这些函数在演示中用于实现不同的效果。这些函数易于使用,并且从它们的名称中就可以轻松分辨出它们的功能。

  1. void loop()
  2. {
  3.   int buf[478];
  4.   int x, x2;
  5.   int y, y2;
  6.   int r;

  7.   runTime = millis();
  8. // Clear the screen and draw the frame
  9.   tft.fillScreen(TFT_BLACK);

  10.   tft.fillRect(0, 0, 480, 13, TFT_RED);

  11.   tft.fillRect(0, 305, 480, 320, TFT_GREY);
  12.   tft.setTextColor(TFT_BLACK,TFT_RED);

  13.   tft.drawCentreString("* TFT_HX8357_Due *", CENTRE, 3, 1);
  14.   tft.setTextColor(TFT_YELLOW,TFT_GREY);
  15.   tft.drawCentreString("Adapted by Bodmer", CENTRE, 309,1);

  16.   tft.drawRect(0, 14, 479, 305-14, TFT_BLUE);

  17. // Draw crosshairs
  18.   tft.drawLine(239, 15, 239, 304, TFT_BLUE);
  19.   tft.drawLine(1, 159, 478, 159, TFT_BLUE);
  20.   for (int i=9; i<470; i+=10)
  21.     tft.drawLine(i, 157, i, 161, TFT_BLUE);
  22.   for (int i=19; i<220; i+=10)
  23.     tft.drawLine(237, i, 241, i, TFT_BLUE);

  24. // Draw sin-, cos- and tan-lines  
  25.   tft.setTextColor(TFT_CYAN);
  26.   tft.drawString("Sin", 5, 15,2);
  27.   for (int i=1; i<478; i++)
  28.   {
  29.     tft.drawPixel(i,159+(sin(((i*1.13)*3.14)/180)*95),TFT_CYAN);
  30.   }
  31.   
  32.   tft.setTextColor(TFT_RED);
  33.   tft.drawString("Cos", 5, 30,2);
  34.   for (int i=1; i<478; i++)
  35.   {
  36.     tft.drawPixel(i,159+(cos(((i*1.13)*3.14)/180)*95),TFT_RED);
  37.   }

  38.   tft.setTextColor(TFT_YELLOW);
  39.   tft.drawString("Tan", 5, 45,2);
  40.   for (int i=1; i<478; i++)
  41.   {
  42.     tft.drawPixel(i,159+(tan(((i*1.13)*3.14)/180)),TFT_YELLOW);
  43.   }

  44.   delay(WAIT);

  45.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);
  46.   tft.drawLine(239, 15, 239, 304,TFT_BLUE);
  47.   tft.drawLine(1, 159, 478, 159,TFT_BLUE);

  48. // Draw a moving sinewave
  49. int col = 0;
  50.   x=1;
  51.   for (int i=1; i<(477*15); i++)
  52.   {
  53.     x++;
  54.     if (x==478)
  55.       x=1;
  56.     if (i>478)
  57.     {
  58.       if ((x==239)||(buf[x-1]==159))
  59.         col = TFT_BLUE;
  60.       else
  61.         tft.drawPixel(x,buf[x-1],TFT_BLACK);
  62.     }
  63.     y=159+(sin(((i*0.7)*3.14)/180)*(90-(i / 100)));
  64.     tft.drawPixel(x,y, TFT_BLUE);
  65.     buf[x-1]=y;
  66.   }

  67.   delay(WAIT);
  68.   
  69.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  70. // Draw some filled rectangles
  71.   for (int i=1; i<6; i++)
  72.   {
  73.     switch (i)
  74.     {
  75.       case 1:
  76.         col = TFT_MAGENTA;
  77.         break;
  78.       case 2:
  79.         col = TFT_RED;
  80.         break;
  81.       case 3:
  82.         col = TFT_GREEN;
  83.         break;
  84.       case 4:
  85.         col = TFT_BLUE;
  86.         break;
  87.       case 5:
  88.         col = TFT_YELLOW;
  89.         break;
  90.     }
  91.     tft.fillRect(150+(i*20), 70+(i*20), 60, 60,col);
  92.   }

  93.   delay(WAIT);
  94.   
  95.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  96. // Draw some filled, rounded rectangles
  97.   for (int i=1; i<6; i++)
  98.   {
  99.     switch (i)
  100.     {
  101.       case 1:
  102.         col = TFT_MAGENTA;
  103.         break;
  104.       case 2:
  105.         col = TFT_RED;
  106.         break;
  107.       case 3:
  108.         col = TFT_GREEN;
  109.         break;
  110.       case 4:
  111.         col = TFT_BLUE;
  112.         break;
  113.       case 5:
  114.         col = TFT_YELLOW;
  115.         break;
  116.     }
  117.     tft.fillRoundRect(270-(i*20), 70+(i*20), 60, 60, 3, col);
  118.   }
  119.   
  120.   delay(WAIT);
  121.   
  122.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  123. // Draw some filled circles
  124.   for (int i=1; i<6; i++)
  125.   {
  126.     switch (i)
  127.     {
  128.       case 1:
  129.         col = TFT_MAGENTA;
  130.         break;
  131.       case 2:
  132.         col = TFT_RED;
  133.         break;
  134.       case 3:
  135.         col = TFT_GREEN;
  136.         break;
  137.       case 4:
  138.         col = TFT_BLUE;
  139.         break;
  140.       case 5:
  141.         col = TFT_YELLOW;
  142.         break;
  143.     }
  144.     tft.fillCircle(180+(i*20),100+(i*20), 30,col);
  145.   }
  146.   
  147.   delay(WAIT);
  148.   
  149.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  150. // Draw some lines in a pattern

  151.   for (int i=15; i<304; i+=5)
  152.   {
  153.     tft.drawLine(1, i, (i*1.6)-10, 303, TFT_RED);
  154.   }

  155.   for (int i=304; i>15; i-=5)
  156.   {
  157.     tft.drawLine(477, i, (i*1.6)-11, 15, TFT_RED);
  158.   }

  159.   for (int i=304; i>15; i-=5)
  160.   {
  161.     tft.drawLine(1, i, 491-(i*1.6), 15, TFT_CYAN);
  162.   }

  163.   for (int i=15; i<304; i+=5)
  164.   {
  165.     tft.drawLine(477, i, 490-(i*1.6), 303, TFT_CYAN);
  166.   }
  167.   
  168.   delay(WAIT);
  169.   
  170.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  171. // Draw some random circles
  172.   for (int i=0; i<100; i++)
  173.   {
  174.     x=32+random(416);
  175.     y=45+random(226);
  176.     r=random(30);
  177.     tft.drawCircle(x, y, r,random(0xFFFF));
  178.   }

  179.   delay(WAIT);
  180.   
  181.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  182. // Draw some random rectangles
  183.   for (int i=0; i<100; i++)
  184.   {
  185.     x=2+random(476);
  186.     y=16+random(289);
  187.     x2=2+random(476);
  188.     y2=16+random(289);
  189.     if (x2<x) {
  190.       r=x;x=x2;x2=r;
  191.     }
  192.     if (y2<y) {
  193.       r=y;y=y2;y2=r;
  194.     }
  195.     tft.drawRect(x, y, x2-x, y2-y,random(0xFFFF));
  196.   }

  197.   delay(WAIT);
  198.   
  199.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  200. // Draw some random rounded rectangles
  201.   for (int i=0; i<100; i++)
  202.   {
  203.     x=2+random(476);
  204.     y=16+random(289);
  205.     x2=2+random(476);
  206.     y2=16+random(289);
  207.     if (x2<x) {
  208.       r=x;x=x2;x2=r;
  209.     }
  210.     if (y2<y) {
  211.       r=y;y=y2;y2=r;
  212.     }
  213.     tft.drawRoundRect(x, y, x2-x, y2-y, 3,random(0xFFFF));
  214.   }

  215.   delay(WAIT);
  216.   
  217.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  218.   for (int i=0; i<100; i++)
  219.   {
  220.     x=2+random(476);
  221.     y=16+random(289);
  222.     x2=2+random(476);
  223.     y2=16+random(289);
  224.     col=random(0xFFFF);
  225.     tft.drawLine(x, y, x2, y2,col);
  226.   }

  227.   delay(WAIT);
  228.   
  229.   tft.fillRect(1,15,478-1,304-15,TFT_BLACK);

  230.   for (int i=0; i<10000; i++)
  231.   {
  232.     tft.drawPixel(2+random(476), 16+random(289),random(0xFFFF));
  233.   }

  234.   delay(WAIT);

  235.   tft.fillRect(0, 0, 480, 320, TFT_BLUE);

  236.   tft.fillRoundRect(160, 70, 319-160, 169-70, 3,TFT_RED);
  237.   
  238.   tft.setTextColor(TFT_WHITE,TFT_RED);
  239.   tft.drawCentreString("That's it!", CENTRE, 93,2);
  240.   tft.drawCentreString("Restarting in a", CENTRE, 119, 2);
  241.   tft.drawCentreString("few seconds...", CENTRE, 132, 2);

  242.   tft.setTextColor(TFT_GREEN,TFT_BLUE);
  243.   tft.drawCentreString("Runtime: (msecs)", CENTRE, 280, 2);
  244.   tft.setTextDatum(TC_DATUM);
  245.   runTime = millis()-runTime;
  246.   tft.drawNumber(runTime, CENTRE, 300,2);
  247.   tft.setTextDatum(TL_DATUM);
  248.   delay (10000);
  249. }
复制代码

花一些时间浏览代码以更好地了解每个函数的功能。


演示效果

将代码上传到Arduino开发板上,几分钟后您应该会看到显示出来的内容,显示文本和其他图形。 下图显示了运行中的显示视图。

demo-1.jpg


您可以使用本文上面提到的两个Arduino开发板中的任何一个。 Arduino Due比Arduino mega运行速度更快,因此它将比mega更快地运行代码。 例如,在Arduino Due上,代码花了23秒才能结束,而在Arduino Mega上,花了44秒才能确认结束。以上就是本文的全部内容,是不是太酷了? 您将使用此显示屏制作哪些项目呢?欢迎在下面回复中分享您的项目。

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1483



手机版|

GMT+8, 2024-5-21 00:57 , Processed in 0.054625 second(s), 9 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表