求助,st7735显示图片扭曲
前段时间买了个st7735s驱动的0.96tft屏,这两天研究了一下st7735的驱动,在网上找了些示例,修改来轮流显示esp8266内的几张图片,开始的时候一切正常,今天想节约一个管脚,想着反正就一个屏,CS就直接接地也应该可以,结果显示图片就成扭曲状(见附图),这是什么原因啊?请大神帮忙解答一下,不胜感激#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <FS.h>
#include <Wire.h>
//SCL/CLK:D5,MISO/SDA/SDIN:D6
#define TFT_CS D2//这个就是片选信号,低电平有效,直接接地就会出现图形扭曲现象
#define TFT_RST -1// Reset line for TFT (or see below...)
#define TFT_DC D1// Data/command line for TFTDC/RS
//调用TFT显示设置函数
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
File fsUploadFile;//创建文件列表
#define BUFFPIXEL 20
// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.
double delay_time;//延时时间
int num = 0;//文件计数器
uint16_t read16(File f) {
uint16_t result;
((uint8_t *)&result) = f.read(); // LSB
((uint8_t *)&result) = f.read(); // MSB
return result;
}
uint32_t read32(File f) {
uint32_t result;
((uint8_t *)&result) = f.read(); // LSB
((uint8_t *)&result) = f.read();
((uint8_t *)&result) = f.read();
((uint8_t *)&result) = f.read(); // MSB
return result;
}
void bmpDraw(uint8_t x, uint8_t y,int num) {//显示函数仅刷新SPIFFS内的logo
File bmpbuffer;
String file_name;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_tbmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_tsdbuffer; // pixel buffer (R+G+B per pixel)
uint8_tbuffidx = sizeof(sdbuffer); // Current position in sdbuffer
booleangoodBmp = false; // Set to true on valid header parse
booleanflip = true; // BMP is stored bottom-to-top
int w, h, row, col;
uint8_tr, g, b;
uint32_t pos = 0, startTime = millis();
if((x >= tft.width()) || (y >= tft.height())){ //如果高和宽超出限制 返回
Serial.println("RETURN");
return;}
file_name = "/"+String(num)+".bmp";//创建文件名
/*修改处*/
bmpbuffer = SPIFFS.open(file_name,"r");//常打开这个文件
// Open requested file on SD card
if (!bmpbuffer) {
Serial.print("File not found");
return;
}//如果未找到文件打印输出并且返回
// Parse BMP header
if(read16(bmpbuffer) == 0x4D42) //如果找到BMP文件类型
{
// BMP signature
Serial.print("File size: ");
Serial.println(read32(bmpbuffer));
//read32(bmpbuffer);
(void)read32(bmpbuffer); // Read & ignore creator bytes
bmpImageoffset = read32(bmpbuffer); // Start of image data
Serial.print("Image Offset: ");
Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print("Header size: ");
Serial.println(read32(bmpbuffer));
bmpWidth= read32(bmpbuffer);//读出图片的宽度
bmpHeight = read32(bmpbuffer);//读出图片的高度
if(read16(bmpbuffer) == 1) { // # planes -- must be '1'
bmpDepth = read16(bmpbuffer); // bits per pixel 确定图片的位数
Serial.print("Bit Depth: ");
Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpbuffer) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
Serial.print("Image size: ");
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;//读出BMP图片的ROWSIZE
// If bmpHeight is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
//如果bPheight为负,则图像按自上而下的顺序排列。
//这不是正典,而是在野外观察到的。
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= tft.width())w = tft.width()- x;
if((y+h-1) >= tft.height()) h = tft.height() - y;
// digitalWrite(D6,LOW);
// Set TFT address window to clipped image bounds
tft.setAddrWindow(x, y, x+w-1, y+h-1);//确定TFT位置
//digitalWrite(D6,HIGH);
//将缓存的数据发送到tft
for (row=0; row<h; row++) { // For each scanline...
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;//定位
if(bmpbuffer.position() != pos) { // Need seek?
bmpbuffer.seek(pos,SeekSet);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<80; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpbuffer.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
//将每个色值写入 SPIFFS bmpbuffer.write();
r =255- sdbuffer;//因模块问题,颜色取反
g = 255-sdbuffer;
b = 255-sdbuffer;
tft.pushColor(tft.color565(r,g,b));
} // end pixel 一个像素颜色的发送函数
} // end scanline整幅图片的发送结束
Serial.print("Loaded in ");//输出读出时间
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpbuffer.close();//删除缓存文件
if(!goodBmp) Serial.println("BMP format not recognized.");
}
//文件大小读取
String formatBytes(size_t bytes){
if (bytes < 1024){
return String(bytes)+"B";
} else if(bytes < (1024 * 1024)){
return String(bytes/1024.0)+"KB";
} else if(bytes < (1024 * 1024 * 1024)){
return String(bytes/1024.0/1024.0)+"MB";
} else {
return String(bytes/1024.0/1024.0/1024.0)+"GB";
}
}
//初始化函数
void setup(void){
pinMode(D3, INPUT);
String filename;//要搜索的文件名
filename = "/"+String(num)+".bmp";//创建文件名
Serial.begin(9600);
Serial.println("begin");
Serial.setDebugOutput(true);
tft.initR(INITR_MINI160x80);//TFT显示屏初始化INITR_144GREENTAB、INITR_MINI160x80
// for (uint8_t i=0; i<4; i++) {
// tft.fillScreen(ST77XX_WHITE);
//
// Serial.println(tft.getRotation(), DEC);
//
// tft.drawPixel(10,20, ST77XX_WHITE);
//
// tft.setRotation(tft.getRotation()+1);
//
// }
tft.fillScreen(ST7735_WHITE);//背景色WHITE/BLACK
SPIFFS.begin();//开启SPIFFS
while(SPIFFS.exists(filename))//搜索文件数量
{
num++;
filename = "/"+String(num)+".bmp";//创建文件名
}
}
void loop(void){
//以下为轮流显示0-9
for(int i = 0;i < num;i++)
{
bmpDraw(0,0,i);
delay(1000);
}
/////////////////////
if(digitalRead(D3)==0){
Serial.println("SPIFFS format start");
SPIFFS.format(); // 格式化SPIFFS
Serial.println("SPIFFS format finish");
}
}
坐标点错位了,128*160 luokuipeng 发表于 2022-1-18 00:28
坐标点错位了,128*160
请问大神是什么原因呢,怎样才能修正?
页:
[1]