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

相关文章

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

c/c++的opencv实现图片膨胀

《c/c++的opencv实现图片膨胀》图像膨胀是形态学操作,通过结构元素扩张亮区填充孔洞、连接断开部分、加粗物体,OpenCV的cv::dilate函数实现该操作,本文就来介绍一下opencv图片... 目录什么是图像膨胀?结构元素 (KerChina编程nel)OpenCV 中的 cv::dilate() 函

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

idea中project的显示问题及解决

《idea中project的显示问题及解决》:本文主要介绍idea中project的显示问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录idea中project的显示问题清除配置重China编程新生成配置总结idea中project的显示问题新建空的pr

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

Java如何根据文件名前缀自动分组图片文件

《Java如何根据文件名前缀自动分组图片文件》一大堆文件(比如图片)堆在一个目录下,它们的命名规则遵循一定的格式,混在一起很难管理,所以本文小编就和大家介绍一下如何使用Java根据文件名前缀自动分组图... 目录需求背景分析思路实现代码输出结果知识扩展需求一大堆文件(比如图片)堆在一个目录下,它们的命名规

将图片导入Python的turtle库的详细过程

《将图片导入Python的turtle库的详细过程》在Python编程的世界里,turtle库以其简单易用、图形化交互的特点,深受初学者喜爱,随着项目的复杂度增加,仅仅依靠线条和颜色来绘制图形可能已经... 目录开篇引言正文剖析1. 理解基础:Turtle库的工作原理2. 图片格式与支持3. 实现步骤详解第

在React聊天应用中实现图片上传功能

《在React聊天应用中实现图片上传功能》在现代聊天应用中,除了文字和表情,图片分享也是一个重要的功能,本文将详细介绍如何在基于React的聊天应用中实现图片上传和预览功能,感兴趣的小伙伴跟着小编一起... 目录技术栈实现步骤1. 消息组件改造2. 图片预览组件3. 聊天输入组件改造功能特点使用说明注意事项

电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案

《电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案》最近有不少兄弟反映,电脑突然弹出“mfc100u.dll已加载,但找不到入口点”的错误提示,导致一些程序无法正... 在计算机使用过程中,我们经常会遇到一些错误提示,其中最常见的就是“找不到指定的模块”或“缺少某个DL

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE