arduino/mixly TFT显示SD卡的图片

2023-12-06 07:10
文章标签 显示 图片 sd arduino tft mixly

本文主要是介绍arduino/mixly TFT显示SD卡的图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、器材

SD卡模块

 1.8寸TFT屏,ST7735

arduino uno开发板

SD卡

 

 

二、接线

     TFT屏arduino uno

GND

GND
VCC5V
SCLD13
SDAD11
RESD8
DCD10
CSD9
BLD7

 

SD卡模块arduino uno
GNDGND
VCC5V
MISOD12
MOSID11
CLKD13
CSD4

三、正式开始

首先我们从网上找到一张想要显示的图片,比如下面这一张

 然后我们打开电脑自带的画图工具打开这张图片

然后重新调整像素大小到以下图所示160*128

 然后保存为。bmp格式的图片,这里我保存为x.bmp然后移动到SD卡中,再把SD卡插到SD卡模块中即可。

再复制以下程序,下载到arduino中

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>#if defined(__SAM3X8E__)#undef __FlashStringHelper::F(string_literal)#define F(string_literal) string_literal
#endif// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.  For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define SD_CS    4  // Chip select line for SD card
#define TFT_CS  9  // Chip select line for TFT display
#define TFT_DC   10  // Data/command line for TFT
#define TFT_RST  8  // Reset line for TFT (or connect to +5V)Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);#define BUFFPIXEL 20void bmpDraw(char *filename, uint8_t x, uint8_t y) {File     bmpFile;int      bmpWidth, bmpHeight;   // W+H in pixelsuint8_t  bmpDepth;              // Bit depth (currently must be 24)uint32_t bmpImageoffset;        // Start of image data in fileuint32_t rowSize;               // Not always = bmpWidth; may have paddinguint8_t  sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbufferboolean  goodBmp = false;       // Set to true on valid header parseboolean  flip    = true;        // BMP is stored bottom-to-topint      w, h, row, col;uint8_t  r, g, b;uint32_t pos = 0, startTime = millis();if((x >= tft.width()) || (y >= tft.height())) return;Serial.println();Serial.print("Loading image '");Serial.print(filename);Serial.println('\'');// Open requested file on SD cardif ((bmpFile = SD.open(filename)) == NULL) {Serial.print("File not found");return;}// Parse BMP headerif(read16(bmpFile) == 0x4D42) { // BMP signatureSerial.print("File size: "); Serial.println(read32(bmpFile));(void)read32(bmpFile); // Read & ignore creator bytesbmpImageoffset = read32(bmpFile); // Start of image dataSerial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);// Read DIB headerSerial.print("Header size: "); Serial.println(read32(bmpFile));bmpWidth  = read32(bmpFile);bmpHeight = read32(bmpFile);if(read16(bmpFile) == 1) { // # planes -- must be '1'bmpDepth = read16(bmpFile); // bits per pixelSerial.print("Bit Depth: "); Serial.println(bmpDepth);if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressedgoodBmp = 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 boundaryrowSize = (bmpWidth * 3 + 3) & ~3;// If bmpHeight is negative, image is in top-down order.// This is not canon but has been observed in the wild.if(bmpHeight < 0) {bmpHeight = -bmpHeight;flip      = false;}// Crop area to be loadedw = bmpWidth;h = bmpHeight;if((x+w-1) >= tft.width())  w = tft.width()  - x;if((y+h-1) >= tft.height()) h = tft.height() - y;// Set TFT address window to clipped image boundstft.startWrite();tft.setAddrWindow(x, y, w, h);for (row=0; row<h; row++) { // For each scanline...// Seek to start of scan line.  It might seem labor-// intensive to be doing this on every line, but this// method covers a lot of gritty details like cropping// and scanline padding.  Also, the seek only takes// place if the file position actually needs to change// (avoids a lot of cluster math in SD library).if(flip) // Bitmap is stored bottom-to-top order (normal BMP)pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;else     // Bitmap is stored top-to-bottompos = bmpImageoffset + row * rowSize;if(bmpFile.position() != pos) { // Need seek?tft.endWrite();bmpFile.seek(pos);buffidx = sizeof(sdbuffer); // Force buffer reload}for (col=0; col<w; col++) { // For each pixel...// Time to read more pixel data?if (buffidx >= sizeof(sdbuffer)) { // IndeedbmpFile.read(sdbuffer, sizeof(sdbuffer));buffidx = 0; // Set index to beginningtft.startWrite();}// Convert pixel from BMP to TFT format, push to displayr = sdbuffer[buffidx++];g = sdbuffer[buffidx++];b = sdbuffer[buffidx++];tft.pushColor(tft.color565(r,g,b));} // end pixel} // end scanlinetft.endWrite();Serial.print("Loaded in ");Serial.print(millis() - startTime);Serial.println(" ms");} // end goodBmp}}bmpFile.close();if(!goodBmp) Serial.println("BMP format not recognized.");
}// 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.uint16_t read16(File f) {uint16_t result;((uint8_t *)&result)[0] = f.read(); // LSB((uint8_t *)&result)[1] = f.read(); // MSBreturn result;
}uint32_t read32(File f) {uint32_t result;((uint8_t *)&result)[0] = f.read(); // LSB((uint8_t *)&result)[1] = f.read();((uint8_t *)&result)[2] = f.read();((uint8_t *)&result)[3] = f.read(); // MSBreturn result;
}void setup(void) {pinMode(12,INPUT); // Set SD's MISO IO State, VERY IMPORTANT!Serial.begin(9600);// Initialize 1.8" TFTtft.initR(INITR_GREENTAB);   // initialize a ST7735S chip, green tabtft.setRotation(3);Serial.println("OK!");tft.fillScreen(ST7735_BLACK);
}void loop() {Serial.print("Initializing SD card...");if (!SD.begin(SD_CS)) {Serial.println("failed!");tft.setTextSize(2);tft.fillScreen(ST7735_BLACK);tft.setCursor(0, 0);tft.setTextColor(ST7735_BLUE);tft.print("SD Card init error!");return;}bmpDraw("x.bmp", 0, 0);
}

效果

 

这篇关于arduino/mixly TFT显示SD卡的图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/460895

相关文章

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

《Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)》本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看... 目录1.获取图片2.目标识别3.背景坐标识别3.1 ddddocr3.2 打码平台4.坐标点击5.图

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更