|
本帖最后由 wei27311 于 2019-9-23 21:23 编辑
接线:
VCC -> 3.3V
GND -> GND
CS -> D8
RESET -> D2
DC/RS -> D1
SDI/MOSI -> D7
SCK -> D5
LED -> 3.3V或PWM控制亮度
我用的是SPI接口的ILI9341彩色LCD
这个屏幕需要在上电的时候复位一次才能显示否则直接白屏
很多的Arduino驱动库是不包含这一步的例如现在用的Adafruit_GFX库
所以一定要注意这一点
具体的做法就是在reset引脚先给低电平
然后再给高电平
屏幕运行期间reset引脚需要一直给高电平
否则就白屏了
Adafruit_GFX库可以理解为是一个接口它提供了通用的方法
需要搭配具体的屏幕驱动库一起使用
在这里需要搭配Adafruit_ILI9341
关于Adafruit_GFX这个库的详细介绍可以登陆下面连接查看
https://electropeak.com/learn/absolute-beginners-guide-to-tft-lcd-displays-by-arduino/
显示图片的原理其实就是把一张图片通过软件
把每一个像素点变成十六位的RGB数值
然后存入一个数组里面传输到LCD显示
我们常用的图片是RGB888 24位的颜色 一个像素点要3个字节
但是一般这种LCD用的是RGB565 是16位的颜色 一个像素点只需要2个字节
所以要用转换软件
这里推荐用picture2hex
第一步:
用AI设计好仪表盘的背景图
第二部:
导出成RGB565的BMP文件
第三部:
用picture2hex软件转换成数组
注意要选好分辨率生成好的数组在picture2hex软件根目录的work文件夹里面
第四部:
在arduino 项目文件夹里新建一个 h文件
把picture2hex生成的数组放入
const uint16_t bg[] PROGMEM = { //code };
第五步:
编译并上传到esp8266.
由于esp8266图形处理能力不高
在这个温湿度计中只有指针和温度值是实时画出来的
其他都是一张图片
底图只加载一次
每次刷新画出指针以及打印出温度值
延时一定时间后用背景色清除掉
另外这个屏幕只有65k色彩
色域很窄过渡不够自然
底图不适合用渐变效果
色差与电脑屏幕相差非常大
- #include "bg.h"
- #include <dht11.h>
- #define DHT11PIN D6
- dht11 DHT11;
- #include <Adafruit_ILI9341.h>
- #define lcdreset D2
- #define TFT_DC D1
- #define TFT_CS D8
- Adafruit_ILI9341 ucg = Adafruit_ILI9341(TFT_CS, TFT_DC);
- #define bgcolor ucg.color565(102,102,102)
- #define WHITE ucg.color565(255,255,255)
- int chk;
- int tem;
- int hum;
- int tem2 = 0;
- int hum2 = 0;
- void Clear1(int value)
- {
- int cx = 160; //圆心x
- int cy = 120; //圆心y
- int cr = 118; //外圆半径r
- //清除湿度指针
- for(int i=0;i<=15;i++)
- {
- int v = 300 + value * 9;
- int v2 = (300 + value * 9) - i;
- int v3 = (300 + value * 9) + i;
- int line1_x0 = floor(cx + (cr - 135) * cos(2 * PI / 1200 * v2));
- int line1_y0 = floor(cy + (cr - 135) * sin(2 * PI / 1200 * v2));
- int line1_x1 = floor(cx + (cr - 40) * cos(2 * PI / 1200 * v));
- int line1_y1 = floor(cy + (cr - 40) * sin(2 * PI / 1200 * v));
- int line2_x0 = floor(cx + (cr - 135) * cos(2 * PI / 1200 * v3));
- int line2_y0 = floor(cy + (cr - 135) * sin(2 * PI / 1200 * v3));
- int line2_x1 = floor(cx + (cr - 40) * cos(2 * PI / 1200 * v));
- int line2_y1 = floor(cy + (cr - 40) * sin(2 * PI / 1200 * v));
- ucg.drawLine(line1_x0,line1_y0,line1_x1,line1_y1,bgcolor);
- ucg.drawLine(line2_x0,line2_y0,line2_x1,line2_y1,bgcolor);
- }
- }
- void draw1(int value)
- {
- int cx = 160; //圆心x
- int cy = 120; //圆心y
- int cr = 118; //外圆半径r
- //湿度中心圆
- ucg.drawCircle(cx,cy,10,CYAN);
-
- //湿度指针
- for(int i=0;i<=15;i++)
- {
- int v = 300 + value * 9;
- int v2 = (300 + value * 9) - i;
- int v3 = (300 + value * 9) + i;
- int line1_x0 = floor(cx + (cr - 135) * cos(2 * PI / 1200 * v2));
- int line1_y0 = floor(cy + (cr - 135) * sin(2 * PI / 1200 * v2));
- int line1_x1 = floor(cx + (cr - 40) * cos(2 * PI / 1200 * v));
- int line1_y1 = floor(cy + (cr - 40) * sin(2 * PI / 1200 * v));
- int line2_x0 = floor(cx + (cr - 135) * cos(2 * PI / 1200 * v3));
- int line2_y0 = floor(cy + (cr - 135) * sin(2 * PI / 1200 * v3));
- int line2_x1 = floor(cx + (cr - 40) * cos(2 * PI / 1200 * v));
- int line2_y1 = floor(cy + (cr - 40) * sin(2 * PI / 1200 * v));
- ucg.drawLine(line1_x0,line1_y0,line1_x1,line1_y1,WHITE);
- ucg.drawLine(line2_x0,line2_y0,line2_x1,line2_y1,WHITE);
- }
- }
- void Clear2()
- {
- int cx = 160; //圆心x
- int cy = 120; //圆心y
- //清除内圆数值
- ucg.fillRect((cx + 18),(cy + 45),35,30,bgcolor);
- }
- void draw2(int value2)
- {
- int cx = 160; //圆心x
- int cy = 120; //圆心y
-
- //内圆显示数值
- ucg.setTextSize(3);
- ucg.setTextColor(WHITE);
- ucg.setCursor((cx + 18),(cy + 37));
- ucg.print(value2);
- }
- void setup()
- {
- pinMode(DHT11PIN,OUTPUT);
- pinMode(lcdreset,OUTPUT);
- digitalWrite(lcdreset,LOW);
- delay(10);
- digitalWrite(lcdreset,HIGH);
- ucg.begin();
- ucg.setRotation(1); //屏幕旋转90度
- ucg.drawRGBBitmap(0,0,bg,320,240);
- delay(100);
- draw1(hum2);
- draw2(tem2);
- }
- void loop()
- {
- chk = DHT11.read(DHT11PIN); //将读取到的值赋给chk
- tem=(float)DHT11.temperature; //将温度值赋值给tem
- hum=(float)DHT11.humidity; //将湿度值赋值给hum
- if(hum2 != hum)
- {
- Clear1(hum2);
- hum2 = hum;
- draw1(hum2);
- }
- else
- {
-
- }
- if(tem2 != tem)
- {
- Clear2(tem2);
- tem2 = tem;
- draw2(tem2);
- }
- else
- {
-
- }
- }
复制代码
|
esp8266, ILI9341, esp8266, ILI9341, esp8266, ILI9341, esp8266, ILI9341, esp8266, ILI9341, DHT11, esp8266, ILI9341, 图片教程, esp8266, ILI9341, esp8266, ILI9341
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
打赏
-
查看全部打赏
|