arduino/mixly TFT显示SD卡的图片

2023-10-28

一、器材

SD卡模块

 1.8寸TFT屏,ST7735

arduino uno开发板

SD卡

 

 

二、接线

     TFT屏 arduino uno

GND

GND
VCC 5V
SCL D13
SDA D11
RES D8
DC D10
CS D9
BL D7

 

SD卡模块 arduino uno
GND GND
VCC 5V
MISO D12
MOSI D11
CLK D13
CS D4

三、正式开始

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

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

然后重新调整像素大小到以下图所示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 20

void bmpDraw(char *filename, uint8_t x, uint8_t y) {

  File     bmpFile;
  int      bmpWidth, bmpHeight;   // W+H in pixels
  uint8_t  bmpDepth;              // 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_t  sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  boolean  goodBmp = false;       // Set to true on valid header parse
  boolean  flip    = true;        // BMP is stored bottom-to-top
  int      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 card
  if ((bmpFile = SD.open(filename)) == NULL) {
    Serial.print("File not found");
    return;
  }

  // Parse BMP header
  if(read16(bmpFile) == 0x4D42) { // BMP signature
    Serial.print("File size: "); Serial.println(read32(bmpFile));
    (void)read32(bmpFile); // Read & ignore creator bytes
    bmpImageoffset = read32(bmpFile); // Start of image data
    Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
    // Read DIB header
    Serial.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 pixel
      Serial.print("Bit Depth: "); Serial.println(bmpDepth);
      if((bmpDepth == 24) && (read32(bmpFile) == 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;

        // 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 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;

        // Set TFT address window to clipped image bounds
        tft.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-bottom
            pos = 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)) { // Indeed
              bmpFile.read(sdbuffer, sizeof(sdbuffer));
              buffidx = 0; // Set index to beginning
              tft.startWrite();
            }

            // Convert pixel from BMP to TFT format, push to display
            r = sdbuffer[buffidx++];
            g = sdbuffer[buffidx++];
            b = sdbuffer[buffidx++];
            tft.pushColor(tft.color565(r,g,b));
          } // end pixel
        } // end scanline
        tft.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(); // MSB
  return 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(); // MSB
  return result;
}

void setup(void) {
  pinMode(12,INPUT); // Set SD's MISO IO State, VERY IMPORTANT!
  Serial.begin(9600);

  // Initialize 1.8" TFT
  tft.initR(INITR_GREENTAB);   // initialize a ST7735S chip, green tab
  tft.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);
}

效果

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

arduino/mixly TFT显示SD卡的图片 的相关文章

随机推荐

  • 视频监控系统时间显示常见故障分析 及时间同步解决方案

    分别任职湖北三峡职业技术学院电子信息学院教研室主任 宜昌市教育技术装备站网络中心主任 宜昌市公安局科研所所长 视频监控系统是指综合应用视音频监控 通信 计算机网络等技术监视设防区域 并实时显示 记录现场图像的电子系统或网络 系统可以在非常事
  • Ubuntu 22.04部署Kubernetes 1.25

    Ubuntu 22 04部署Kubernetes 1 25 基本环境 系统和软件版本 master节点ip 安装过程 1 准备工作 1 1 修改主机名 1 2 关闭swap分区 1 3 关闭防火墙 1 4 重启电脑 确认swap和防火墙均已
  • angular组件封装

    1 这个公共组件的封装 2 c dropdown component ts import Component OnInit Input EventEmitter Output ViewChild from angular core Comp
  • 08LinuxC线程学习之pthread_join函数以及根据参2获取返回值的案例

    1 pthread join函数 int pthread join pthread t thread void retval 功能 阻塞等待线程退出 获取线程退出状态 其作用 对应进程中 waitpid 函数 成功 0 失败 错误号 参1
  • 1033. 旧键盘打字

    1033 旧键盘打字 20 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN Yue 旧键盘上坏了几个键 于是在敲一段文字的时候 对应的字符就不会出现 现在给出应该输
  • 【笔记】Nginx+Ngrok实现80端口服务器+80端口内网穿透

    安装ngrok 笔记 ngrok安装方法 安装完毕后ngrok默认将服务器的80端口占用 这时 需要修改启动脚本 vim etc init d ngrokd 找到如下部分 nohup sudo bin ngrokd tlsKey serve
  • 【每日一题】-金牌榜排序

    文章目录 题目描述 输入 输出 样例 解析 代码 题目描述 2012伦敦奥运会即将到来 大家都非常关注奖牌榜的情况 现在我们假设奖牌榜的排名规则如下 1 首先gold medal 数量多的排在前面 2 其次silver medal 数量多的
  • SpringBoot中 Lua函数操作redis

    Lua Lua 是一个简洁 轻量 可扩展的脚本语言 它的特性有 轻量 源码包只有核心库 编译后体积很小 高效 由 ANSI C 写的 启动快 运行快 内嵌 可内嵌到各种编程语言或系统中运行 提升静态语言的灵活性 如 OpenResty 就是
  • xman的思维导图快捷键_这个良心好用的思维导图软件,居然不用氪金充钱

    今天给大家介绍一款免费的在线思维导图工具 GitMind 提供了丰富的功能和模板 可免费导出 JPG PNG 图片 PDF 文档以及 TXT 文本等多种格式 此外 GitMind 还集成了制作流程图的能力 网站展示的流程图示例有泳道图 拓扑
  • Springboot项目使用达梦数据库

    下载达梦数据库驱动 Dm7JdbcDriver16 jar 执行maven命令把驱动包打入本地maven仓库 mvn install install file DgroupId com dm DartifactId DmJdbcDriver
  • 学校计算机如何脱控,学校机房脱控方法(已控状态)/极域电子教室脱离老师控制图文教程...

    老师没控制的时候 刀友应该都会断掉控制吧 我就不说了 就说说老师老师已经控制了该如何脱离控制 拔网线比较麻烦就不说了 以下操作之前先检查极域电子教室 右键右下角极域电子教室端 打开设置 把禁止结束学生端进程前面的勾去掉 把断网锁屏前面的勾去
  • 部署ELFK

    目录 ELFK ES logstash filebeat kibana 环境准备 所有节点 Elasticsearch 集群部署 在Node1 Node2节点上操作 修改elasticsearch主配置文件 es 性能调优参数 启动elas
  • Marriage is Stable

    http acm hdu edu cn showproblem php pid 1522 Problem Description Albert Brad Chuck are happy bachelors who are in love w
  • JVM--三大子系统详解

    首先需要了解java的命令 javac 将java文件编译为 class文件 里面是一些二进制文件 javap c 将 class文件变为反汇编 例如 javap c hello class gt demo txt 可以将class文件转化
  • GPIO介绍

    目录 一 GPIO是什么 二 STM32引脚分类 三 GPIO内部结构 四 GPIO的工作模式 4 1 输入模式 模拟 上拉 下拉 浮空 4 2 输出模式 推挽 开漏 4 3 复用功能 推挽 开漏 4 4 模拟输入输出 上下拉无影响 一 G
  • c语言将csv文件存储到数组,读取CSV文件并将值存储到数组中

    青春有我 我最喜欢的CSV解析器是一个内置在 NET库中的解析器 这是Microsoft VisualBasic命名空间中隐藏的宝藏 下面是一个示例代码 using Microsoft VisualBasic FileIO var path
  • ConcurrentHashMap 的实现原理

    目录 常见问题 1 concurrentHashMap特点 2 concurrentHashMap如何保证效率高 又安全的 1 构造函数 2 put方法 2 1 initTable 2 2 addCount方法 3 get方法 常见问题 1
  • 【SpinalHDL】Windows10系统搭建SpinalHDL 开发环境

    本文主要记载如何从零开始在win平台搭建SpinalHDL开发环境并跑通第一个spinal project demo 1 环境准备 1 1 软件下载 首先列出需要安装的软件 并逐一对这些软件的功能和其必要性进行说明 需要安装的软件 IDEA
  • 继电器的过流过压保护(自恢复保险丝)

    简述 继电器广泛应用于消费电子产业和工业设备中 它具有控制系统 又称输入回路 和被控制系统 又称输出回路 它实际上是用较小的电流去控制较大电流的一种 自动开关 故在电路中起着自动调节 安全保护 转换电路等作用 继电器可能因为过流或者过压而损
  • arduino/mixly TFT显示SD卡的图片

    一 器材 SD卡模块 1 8寸TFT屏 ST7735 arduino uno开发板 SD卡 二 接线 TFT屏 arduino uno GND GND VCC 5V SCL D13 SDA D11 RES D8 DC D10 CS D9 B