ESP32+platformio+TFT_ESPI+ST7796S踩坑记录

本人在使用官方DEMO的时候发现屏幕只会发白光,如下图:


之后尝试了许多方法都不可行,最后找了客服,客服给我发了一份文件,我发现文件内有个ST7796_Init,该文件的内容与TFT_Espi库内的并不相同,所以我就把库内的内容改了(注释的为官方文件,之后就是我改的)

ST7796_Init:

/*
#define TFT_INIT_DELAY 0
{
	delay(120);

	writecommand(0x01); //Software reset
	delay(120);

	writecommand(0x11); //Sleep exit                                            
	delay(120);

	writecommand(0xF0); //Command Set control                                 
	writedata(0xC3);    //Enable extension command 2 partI
	
	writecommand(0xF0); //Command Set control                                 
	writedata(0x96);    //Enable extension command 2 partII
	
	writecommand(0x36); //Memory Data Access Control MX, MY, RGB mode                                    
	writedata(0x48);    //X-Mirror, Top-Left to right-Buttom, RGB  
	
	writecommand(0x3A); //Interface Pixel Format                                    
	writedata(0x55);    //Control interface color format set to 16
	
	
	writecommand(0xB4); //Column inversion 
	writedata(0x01);    //1-dot inversion

	writecommand(0xB6); //Display Function Control
	writedata(0x80);    //Bypass
	writedata(0x02);    //Source Output Scan from S1 to S960, Gate Output scan from G1 to G480, scan cycle=2
	writedata(0x3B);    //LCD Drive Line=8*(59+1)


	writecommand(0xE8); //Display Output Ctrl Adjust
	writedata(0x40);
	writedata(0x8A);	
	writedata(0x00);
	writedata(0x00);
	writedata(0x29);    //Source eqaulizing period time= 22.5 us
	writedata(0x19);    //Timing for "Gate start"=25 (Tclk)
	writedata(0xA5);    //Timing for "Gate End"=37 (Tclk), Gate driver EQ function ON
	writedata(0x33);
	
	writecommand(0xC1); //Power control2                          
	writedata(0x06);    //VAP(GVDD)=3.85+( vcom+vcom offset), VAN(GVCL)=-3.85+( vcom+vcom offset)
	 
	writecommand(0xC2); //Power control 3                                      
	writedata(0xA7);    //Source driving current level=low, Gamma driving current level=High
	 
	writecommand(0xC5); //VCOM Control
	writedata(0x18);    //VCOM=0.9

	delay(120);
	
	//ST7796 Gamma Sequence
	writecommand(0xE0); //Gamma"+"                                             
	writedata(0xF0);
	writedata(0x09); 
	writedata(0x0b);
	writedata(0x06); 
	writedata(0x04);
	writedata(0x15); 
	writedata(0x2F);
	writedata(0x54); 
	writedata(0x42);
	writedata(0x3C); 
	writedata(0x17);
	writedata(0x14);
	writedata(0x18); 
	writedata(0x1B); 
	 
	writecommand(0xE1); //Gamma"-"                                             
	writedata(0xE0);
	writedata(0x09); 
	writedata(0x0B);
	writedata(0x06); 
	writedata(0x04);
	writedata(0x03); 
	writedata(0x2B);
	writedata(0x43); 
	writedata(0x42);
	writedata(0x3B); 
	writedata(0x16);
	writedata(0x14);
	writedata(0x17); 
	writedata(0x1B);

  delay(120);
	
	writecommand(0xF0); //Command Set control                                 
	writedata(0x3C);    //Disable extension command 2 partI

	writecommand(0xF0); //Command Set control                                 
	writedata(0x69);    //Disable extension command 2 partII

  end_tft_write();
  delay(120);
  begin_tft_write();

	writecommand(0x29); //Display on                                          	
}
*/

#define TFT_INIT_DELAY 0
{
	delay(120);
	writecommand(0xF0);
	writedata(0xC3);
	writecommand(0xF0);
	writedata(0x96);
	writecommand(0x36);
	writedata(0x48);  
	writecommand(0x3A);
	writedata(0x05);  
	writecommand(0xB0);
	writedata(0x80);  
	writecommand(0xB6);
	writedata(0x00);
	writedata(0x02);  
	writecommand(0xB5);
	writedata(0x02);
	writedata(0x03);
	writedata(0x00);
	writedata(0x04);
	writecommand(0xB1);
	writedata(0x80);  
	writedata(0x10);  
	writecommand(0xB4);
	writedata(0x00);
	writecommand(0xB7);
	writedata(0xC6);
	writecommand(0xC5);
	writedata(0x1C);
	writecommand(0xE4);
	writedata(0x31);
	writecommand(0xE8);
	writedata(0x40);
	writedata(0x8A);
	writedata(0x00);
	writedata(0x00);
	writedata(0x29);
	writedata(0x19);
	writedata(0xA5);
	writedata(0x33);
	writecommand(0xC2);
	writecommand(0xA7);
	
	writecommand(0xE0);
	writedata(0xF0);
	writedata(0x09);
	writedata(0x13);
	writedata(0x12);
	writedata(0x12);
	writedata(0x2B);
	writedata(0x3C);
	writedata(0x44);
	writedata(0x4B);
	writedata(0x1B);
	writedata(0x18);
	writedata(0x17);
	writedata(0x1D);
	writedata(0x21);

	writecommand(0XE1);
	writedata(0xF0);
	writedata(0x09);
	writedata(0x13);
	writedata(0x0C);
	writedata(0x0D);
	writedata(0x27);
	writedata(0x3B);
	writedata(0x44);
	writedata(0x4D);
	writedata(0x0B);
	writedata(0x17);
	writedata(0x17);
	writedata(0x1D);
	writedata(0x21);

	writecommand(0xF0);
	writedata(0x3C);
	writecommand(0xF0);
	writedata(0x69);
	writecommand(0X13);
	writecommand(0X11);
  	end_tft_write();
  	delay(120);
  	begin_tft_write();
	writecommand(0X29);                                        	
}

但是改后还是亮不了,所以之后我找了开发板的原理图,我怀疑是接线的错误,所以依据引脚图更改了接线


 #define TFT_BL   21            // LED back-light control pin
 #define TFT_BACKLIGHT_ON HIGH  // Level to turn ON back-light (HIGH or LOW)


#define TFT_MISO 12
#define TFT_MOSI 13
#define TFT_SCLK 14
#define TFT_CS   15  // Chip select control pin
#define TFT_DC    2  // Data Command control pin
#define TFT_RST  4  // Reset pin (could connect to RST pin)

怕有朋友看不懂,我补上Stepup的所有内容

//                            USER DEFINED
很高兴回答你的问题,以下是使用Arduino、lvgl、esp32tft_espi的示例代码实现网络天气时钟的方法: ``` #include <Arduino.h> #include <TFT_eSPI.h> #include <lvgl.h> #include <WiFi.h> #include <ArduinoJson.h> #include <time.h> #define WIFI_SSID "your_wifi_ssid" #define WIFI_PASS "your_wifi_password" #define OPENWEATHERMAP_APIKEY "your_openweathermap_api_key" TFT_eSPI tft = TFT_eSPI(135, 240); // 设置 TFT 屏幕的大小 lv_obj_t * label_temperature, * label_temperature_symbol, * label_weather_description, * label_city_name, * label_clock; void get_weather_data(String & temperature, String & weather_description, String & city_name) { WiFiClient client; if (!client.connect("api.openweathermap.org", 80)) { Serial.println("connection failed!"); return; } String url = "/data/2.5/weather?q=Shenzhen,CN&appid=" OPENWEATHERMAP_APIKEY "&units=metric"; client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: api.openweathermap.org\r\n" + "Connection: close\r\n\r\n"); String response = ""; while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { break; } } while (client.available()) { response += (char)client.read(); } const int capacity = JSON_OBJECT_SIZE(4) + 100; DynamicJsonDocument doc(capacity); deserializeJson(doc, response); JsonObject main = doc["main"]; temperature = main["temp"].as<String>(); JsonArray weather = doc["weather"]; weather_description = weather[0]["description"].as<String>(); city_name = doc["name"].as<String>(); } void get_time_string(String & time_string) { time_t now; struct tm timeinfo; time(&now); localtime_r(&now, &timeinfo); char strftime_buf[64]; strftime(strftime_buf, sizeof(strftime_buf), "%H:%M", &timeinfo); time_string = strftime_buf; } void setup() { Serial.begin(115200); lv_init(); tft.init(); tft.setRotation(3); tft.fillScreen(TFT_BLACK); lvgl_driver_init(&tft); lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); disp_drv.buffer = &lvgl_buffer; disp_drv.flush_cb = lvgl_flush; lv_disp_drv_register(&disp_drv); lv_theme_t * theme = lv_theme_material_init(210, NULL); lv_theme_set_current(theme); label_temperature = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_temperature, "temperature"); lv_obj_align(label_temperature, NULL, LV_ALIGN_CENTER, 0, -40); label_temperature_symbol = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_temperature_symbol, "°C"); lv_obj_set_pos(label_temperature_symbol, lv_obj_get_x(label_temperature) + lv_obj_get_width(label_temperature), lv_obj_get_y(label_temperature)); label_weather_description = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_weather_description, "weather_description"); lv_obj_align(label_weather_description, NULL, LV_ALIGN_CENTER, 0, 0); label_city_name = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_city_name, "city_name"); lv_obj_align(label_city_name, NULL, LV_ALIGN_CENTER, 0, 40); label_clock = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_clock, "clock"); lv_obj_align(label_clock, NULL, LV_ALIGN_CENTER, 0, 80); lv_task_create([](lv_task_t * task) { String temperature; String weather_description; String city_name; get_weather_data(temperature, weather_description, city_name); lv_label_set_text(label_temperature, temperature.c_str()); lv_label_set_text(label_weather_description, weather_description.c_str()); lv_label_set_text(label_city_name, city_name.c_str()); }, 5000, LV_TASK_PRIO_LOW, NULL); lv_task_create([](lv_task_t * task) { String time_string; get_time_string(time_string); lv_label_set_text(label_clock, time_string.c_str()); }, 1000, LV_TASK_PRIO_MID, NULL); WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { delay(1000); } Serial.println("setup complete!"); } void loop() { lv_task_handler(); delay(1); } ``` 该代码使用了 OpenWeatherMap API 获取深圳的当前天气数据,在屏幕上输出温度、天气描述和城市名称,通过 `lv_task_create` 函数设置定时任务每 5 秒钟更新一次天气信息和每秒钟更新一次当前时间,是一个简单而实用的网络天气时钟的实现方式。 注:这里使用的API Key只是做演示,请勿用于商业用途,如有需要请自行注册。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值