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

相关文章

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使