风筝
发表于: 2017-2-15 16:42:08 | 显示全部楼层

本文主要介绍如何在Arduino开发板上连接一个1.8寸的SPI TFT液晶显示屏。如果以前没有接触过,这或许对您有些帮助。

1.jpg


为什么要选择SPI的TFT

嗯,这是因为,众所周知,传输数据有两种模式:串行和并行。当使用多个数据传输的数据总线时,这种方式是并行数据总线的TFT,或者是通常称为普通TFT。这种类型的显示屏需要相对较多的引脚数目用于连接信号,同时数据总线需要8或16个引脚,他们通常会占用UNO开发板的所有引脚,甚至可能会需要一个扩展板。


当需要单根数据总线来传送所有数据时,所有数据被一系列地压缩,然后推向接收端。 这种类型的TFT被称为SPI TFT,其中SPI代表串行外设接口(SERIAL PERIPHERAL INTERFACE)。 现在我们知道SPI是一种缩写形式。


还要记住,该1.8英寸SPI TFT在UNO开发板的引脚连接方式与MEGA开发板不同。 在MEGA开发板上,SPI引脚位于50、51、52和53。

2.jpg


连接方式和示例代码

首先,所需软硬件的清单如下:

■    Arduino IDE1.0.5开发环境

■    Arduino UNO开发板

■    1k欧的电阻

■    导线若干

请注意:在Arduino版本1.0.5以及更高版本中已经预装了TFT库。


按如下所述进行连接:

5.jpg


现在我并没有将前5个引脚直接连接到Arduino开发板,而是在TFT引脚和Arduino引脚之间串联一个1k阻值的电阻。 这是因为IO端口支持3.3v,而所需的Vcc是5v。

3.jpg


现在,打开Arduino IDE并运行除“BitmapLogo”之外的任何程序。 BitmapLogo会使用SD卡功能,并要求您连接到带SD卡的TFT。


我成功演示了下面的示例:

  1. #include <TFT.h>  // Arduino LCD library
  2. #include <SPI.h>

  3. // pin definition for the Uno
  4. #define cs   10
  5. #define dc   9
  6. #define rst  8

  7. // pin definition for the Leonardo
  8. // #define cs   7
  9. // #define dc   0
  10. // #define rst  1

  11. // create an instance of the library
  12. TFT TFTscreen = TFT(cs, dc, rst);

  13. // char array to print to the screen
  14. char sensorPrintout[4];

  15. void setup() {

  16.   // Put this line at the beginning of every sketch that uses the GLCD:
  17.   TFTscreen.begin();

  18.   // clear the screen with a black background
  19.   TFTscreen.background(0, 0, 0);

  20.   // write the static text to the screen
  21.   // set the font color to white
  22.   TFTscreen.stroke(255, 255, 255);
  23.   // set the font size
  24.   TFTscreen.setTextSize(2);
  25.   // write the text to the top left corner of the screen
  26.   TFTscreen.stroke(255, 0, 0);
  27.   TFTscreen.text("IoT Freaks!\n ", 20, 0);
  28.   TFTscreen.stroke(0, 255, 255);
  29.   TFTscreen.text("Sensor value\n ", 12, 25);
  30.   // ste the font size very large for the loop
  31.   
  32. }

  33. void loop() {

  34.   // Read the value of the sensor on A0
  35.   String sensorVal = String(analogRead(A0));

  36.   // convert the reading to a char array
  37.   sensorVal.toCharArray(sensorPrintout, 4);

  38.   TFTscreen.stroke(255, 255, 0);
  39.   TFTscreen.setTextSize(5);
  40.   TFTscreen.text(sensorPrintout, 34, 50);
  41.   delay(550);
  42.   TFTscreen.stroke(0, 0, 0);
  43.   TFTscreen.text(sensorPrintout, 34, 50);
  44. }
复制代码

该段代码需要在引脚A0处连接一个传感器,并且读取其值,然后在TFT上显示出来本。 因此,可以使用任何模拟的传感器,如电位计、LDR或红外接收器。

4.jpg

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

本版积分规则

主题 700 | 回复: 1479



手机版|

GMT+8, 2024-3-29 02:53 , Processed in 0.131636 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

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

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