|
本文主要介绍如何在Arduino开发板上连接一个1.8寸的SPI TFT液晶显示屏。如果以前没有接触过,这或许对您有些帮助。
为什么要选择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。
连接方式和示例代码
首先,所需软硬件的清单如下: ■ Arduino IDE1.0.5开发环境
■ Arduino UNO开发板 ■ 1k欧的电阻 ■ 导线若干 请注意:在Arduino版本1.0.5以及更高版本中已经预装了TFT库。
按如下所述进行连接:
现在我并没有将前5个引脚直接连接到Arduino开发板,而是在TFT引脚和Arduino引脚之间串联一个1k阻值的电阻。 这是因为IO端口支持3.3v,而所需的Vcc是5v。
现在,打开Arduino IDE并运行除“BitmapLogo”之外的任何程序。 BitmapLogo会使用SD卡功能,并要求您连接到带SD卡的TFT。
我成功演示了下面的示例: - #include <TFT.h> // Arduino LCD library
- #include <SPI.h>
- // pin definition for the Uno
- #define cs 10
- #define dc 9
- #define rst 8
- // pin definition for the Leonardo
- // #define cs 7
- // #define dc 0
- // #define rst 1
- // create an instance of the library
- TFT TFTscreen = TFT(cs, dc, rst);
- // char array to print to the screen
- char sensorPrintout[4];
- void setup() {
- // Put this line at the beginning of every sketch that uses the GLCD:
- TFTscreen.begin();
- // clear the screen with a black background
- TFTscreen.background(0, 0, 0);
- // write the static text to the screen
- // set the font color to white
- TFTscreen.stroke(255, 255, 255);
- // set the font size
- TFTscreen.setTextSize(2);
- // write the text to the top left corner of the screen
- TFTscreen.stroke(255, 0, 0);
- TFTscreen.text("IoT Freaks!\n ", 20, 0);
- TFTscreen.stroke(0, 255, 255);
- TFTscreen.text("Sensor value\n ", 12, 25);
- // ste the font size very large for the loop
-
- }
- void loop() {
- // Read the value of the sensor on A0
- String sensorVal = String(analogRead(A0));
- // convert the reading to a char array
- sensorVal.toCharArray(sensorPrintout, 4);
- TFTscreen.stroke(255, 255, 0);
- TFTscreen.setTextSize(5);
- TFTscreen.text(sensorPrintout, 34, 50);
- delay(550);
- TFTscreen.stroke(0, 0, 0);
- TFTscreen.text(sensorPrintout, 34, 50);
- }
复制代码
该段代码需要在引脚A0处连接一个传感器,并且读取其值,然后在TFT上显示出来本。 因此,可以使用任何模拟的传感器,如电位计、LDR或红外接收器。
|