SD卡中读取bin图片显示LCD上

该博客详细介绍了如何在STM32微控制器上从SD卡读取BIN格式的图片文件,并将其显示在LCD屏幕上。作者通过使用FatFS文件系统库来操作SD卡,逐行读取图片数据,并转换为LCD显示所需的格式。代码中包含了关键函数如`LCD_show_picture_2`和`sd_show_picture_bin_1`,用于处理图片数据并显示。整个过程没有进行LCD坐标转换,保持了原始图片的x和y方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

分类: 嵌入式

2011-07-04 17:07:44

 

工程代码:  6_从SD中读取BIN图片显示LCD上.rar    

四张图片及生成的bin文件图片格式  bin文件.rar  

描述: 

     将图片保存为 bin格式,然后在stm32中读取 bin文件,
最后显示bin文件在 LCD上。128*160 图片 生成的 bin文件挺大的,但是STM32RB的内存大小只有20K,所以不能一次定义 数组空间,将一个bin文件都读取到 数组中,我们可以这么实现:定义一行 像素点数组 u8 data[320] 为什么是320大小呢, 一个像素点是16位真彩色, 所以生成的16进制 有4位, 所以说,一个像素点包含了数组中的 2 位。

 
  1. /*在lcd显示上, 本来是 -------------->y方向
  2.                      |
  3.                      |
  4.                      |
  5.                      |
  6.                      |
  7.                      |
  8.                      |
  9.                      x方向
  10. 那么y方向应该是 128像素点, x方向是160像素点
  11. 但是在 LCD_PutString LCD_show_number 这些函数中, 已经事项
  12. 将 x y 方向转换了,------------------>x 转换后的
  13.                  |
  14.                  |
  15.                  |
  16.                  |     
  17.                  转换后的y方向
  18. 在 显示bin图片过程中,是没有经过 转换的, x方向是 向下的, y方向是水平的
  19. = 160 y = 128
  20.  
  21. */
  22. void LCD_show_picture_2(u8 *pic)//显示lcd 2行像素点
  23. {
  24.     //int i,j,k;
  25.     int j = 0, k = 0;
  26.     unsigned char picH,picL;
  27.  
  28. //    k=0;
  29. //    for(= x; i < x 2; i )/
  30.     //{
  31.         for(j=0;j<160;) //显示一行 x 像素点
  32.         {
  33.             picH=pic[k];//读取像素点的 高位 颜色
  34.             k ;
  35.             picL=pic[k];//读取像素点的 低位 颜色
  36.             k ;
  37.             LCD_DataWrite(picH,picL);
  38.         }
  39. //    }
  40.         
  41. }
  42.  
  43. void sd_show_picture_bin_1(void)//LCD显示SD中 bin 图片
  44. {
  45.     UINT br;
  46.     u16 i;
  47.     u8 data[320]; //存储一行像素点160 的 16进制颜色信息 160*2=320
  48.  
  49.     res = f_mount(0, &fs);//打开文件,不存在则创建,可读 可写
  50.     res = f_open(&file, "1.bin", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
  51.     
  52.     for(= 0; i < 128; i )//y方向是 128行
  53.     {
  54.         res = f_read(&file,data,320,&br);//x方向160个像素点,每个像素点 2 个 
  55.         LCD_show_picture_2(data);//显示 1 行像素点
  56.     //    f_lseek(&file2,0); // /* Move Pointer to the top of the file */ 
  57.       //    f_lseek(&file2,512);//Index为要显示的文字的点阵数组 处于SD卡字库中的起始位置。 
  58.     }          
  59.     f_close(&file);    
  60. }



 
  1. /* 
  2. *    Author :     余威先
  3. *    Date:         2011.6.19    
  4. * 开发板上:PD2 ~ LED2
  5. * PA8 ~ LED0
  6. * PA15 ~ KEY1
  7. * PA13 ~ KEY2
  8. * 修改Date: 2011.6.30 19:20
  9. * rcc时钟配置: SYSCLK = 16MHZ
  10. * AHB = 16MHZ
  11. *              APB1 = 8 MHZ // TIM2 TIM3 TIM4
  12. *                 APB2 = 16 MHZ //GPIO A B C D
  13. * 修改Date:2011.7.3 21:00
  14.     简单描述:
  15.      2011.7.3 21:00 移植 fatfs 成功 ff8b
  16.             2011.7.4 8:48 读取SD中 yu.txt中数据,LCD显示读到的数据
  17.             2011.7.4 13:14 读取SD中 图片bin数据 显示在LCD上 循环显示 4张图片
  18.      
  19. */
  20. #include "stm32f10x.h"
  21. #include "rcc.h"
  22. #include "systick.h"
  23. #include "led.h"
  24. #include "delay.h"
  25. //#include "key.h"
  26. #include "tim3.h"
  27. #include "usart1.h"
  28. #include "lcd.h"
  29. #include "rtc.h"
  30. #include "flash.h"
  31. #include "sd_spi.h"
  32. #include "..\FATS\ff.h"
  33. #include "..\FATS\integer.h"
  34. #include "..\FATS\ffconf.h"
  35. #include "..\FATS\diskio.h"
  36. //#include "picture.h"
  37.  
  38. volatile u8 sec = 0; // 全局变量 秒 时 小时
  39. volatile u8 min = 0;
  40. volatile u8 hour = 0;
  41.  
  42. FATFS fs;
  43. FRESULT res;
  44. FIL file;
  45.  
  46. u8 send_buffer[512] = {97,6};
  47. u8 receiv_buffer[512] = {0,0};
  48. u32 capacity = 0;
  49. void write_file(void);
  50. void sd_show_picture_bin_1(void); //显示SD中 1.bin 图片
  51. void sd_show_picture_bin_2(void); //显示SD中 2.bin 图片
  52. void sd_show_picture_bin_3(void);
  53. void sd_show_picture_bin_4(void);
  54. void LCD_show_picture_2(u8 *pic); //显示LCD一行像素点 160个点
  55.  
  56. int main(void)
  57. {    
  58. //    u16 i = 0;//
  59.  
  60.     RCC_Configuration(); //系统时钟配置
  61.     delay_init();     // 延时 初始化
  62.  
  63. //    RTC_Configuration(); //RTC系统 配置
  64. //    RTC_NVIC_Configuration(); //RTC中断配置
  65. //    RTC_Init();// RTC 时钟初始化
  66.  
  67.     SPI1_Configuration(); //SPI1 初始化
  68. //    SD_Init();             //SD卡 初始化
  69.  
  70.     LCD_Init();         //LCD 彩屏初始化
  71.  
  72.     write_cmd(0x2C); //LCD 写数据命令
  73.     DrawFull_single_colour(0xff, 0xff);    //显示 纯白色 
  74. //    LCD_show_picture(Image_pic);//显示 flash中图片
  75.     
  76.     while(1) //循环显示 4 张 bin图片
  77.     {
  78.         sd_show_picture_bin_1();
  79.         delay_s(2);
  80.         sd_show_picture_bin_2();
  81.         delay_s(2);
  82.         sd_show_picture_bin_3();
  83.         delay_s(2);
  84.         sd_show_picture_bin_4();
  85.         delay_s(2);
  86.     }
  87.  
  88. //    capacity = SD_GetCapacity();    //获取 容量
  89. //    LCD_show_number(48,128,capacity); //打印低16位
  90. //    LCD_show_number(0,128,capacity>>16); //打印高16位
  91.  
  92. //    LCD_PutString(0,0, "start to write file..");
  93. //    write_file();
  94. #if 0
  95.     for(= 0; i < 256; i ) //发送数据填充
  96.         send_buffer[i] = i;    
  97.     
  98.     for(= 0; i < 256; i ) //发送数据填充
  99.         send_buffer[i 256] = i;    
  100.  
  101.     SD_WriteSingleBlock(0, send_buffer); //写数据到 块 中
  102.     SD_ReadSingleBlock(0, receiv_buffer); //从 块 中 读数据
  103.  
  104.     for(= 0; i < 512; i ) // 显示从块中读取到的数据, 一个字节最大255
  105.     {
  106.         LCD_show_number(8,32,receiv_buffer[i]);
  107.         delay_s(1);
  108.     }
  109. #endif
  110.  
  111.     while(1) //无限循环, 中断中 显示 秒时钟
  112.     {
  113.         #if 0
  114.         LCD_show_number_2(40,16,hour);
  115.         LCD_show_number_2(64,16,min);
  116.         LCD_show_number_2(88,16,sec);    
  117.         #endif
  118.     }
  119. //    return 0;
  120. }
  121.  
  122. /*在lcd显示上, 本来是 -------------->y方向
  123.                      |
  124.                      |
  125.                      |
  126.                      |
  127.                      |
  128.                      |
  129.                      |
  130.                      x方向
  131. 那么y方向应该是 128像素点, x方向是160像素点
  132. 但是在 LCD_PutString LCD_show_number 这些函数中, 已经事项
  133. 将 x y 方向转换了,------------------>x 转换后的
  134.                  |
  135.                  |
  136.                  |
  137.                  |     
  138.                  转换后的y方向
  139. 在 显示bin图片过程中,是没有经过 转换的, x方向是 向下的, y方向是水平的
  140. = 160 y = 128
  141.  
  142. */
  143. void LCD_show_picture_2(u8 *pic)//显示lcd 2行像素点
  144. {
  145.     //int i,j,k;
  146.     int j = 0, k = 0;
  147.     unsigned char picH,picL;
  148.  
  149. //    k=0;
  150. //    for(= x; i < x 2; i )/
  151.     //{
  152.         for(j=0;j<160;) //显示一行 x 像素点
  153.         {
  154.             picH=pic[k];//读取像素点的 高位 颜色
  155.             k ;
  156.             picL=pic[k];//读取像素点的 低位 颜色
  157.             k ;
  158.             LCD_DataWrite(picH,picL);
  159.         }
  160. //    }
  161.         
  162. }
  163.  
  164. void sd_show_picture_bin_1(void)//LCD显示SD中 bin 图片
  165. {
  166.     UINT br;
  167.     u16 i;
  168.     u8 data[320]; //存储一行像素点160 的 16进制颜色信息 160*2=320
  169.  
  170.     res = f_mount(0, &fs);//打开文件,不存在则创建,可读 可写
  171.     res = f_open(&file, "1.bin", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
  172.     
  173.     for(= 0; i < 128; i )//y方向是 128行
  174.     {
  175.         res = f_read(&file,data,320,&br);//x方向160个像素点,每个像素点 2 个 
  176.         LCD_show_picture_2(data);//显示 1 行像素点
  177.     //    f_lseek(&file2,0); // /* Move Pointer to the top of the file */ 
  178.       //    f_lseek(&file2,512);//Index为要显示的文字的点阵数组 处于SD卡字库中的起始位置。 
  179.     }          
  180.     f_close(&file);    
  181. }
  产生 bin 图片文件  
  为了实现从SD中读取图片数据,并且显示在LCD上。 这里首先实现了
直接读取图片数据,显示LCD上。
   图片16进制文件大小都 203KB了,生成的 hex文件 124KB
,flash大小总共才128KB

    
主要代码实现:
 
  1. void LCD_show_picture(const u8 *pic)
  2. {
  3.     int i,j,k;
  4.     unsigned char picH,picL;
  5.  
  6.     k=0;
  7.     for(i=0;i<128;i++)
  8.     {
  9.         for(j=0;j<160;j++)
  10.         {
  11.             picH=pic[k];
  12.             k++;
  13.             picL=pic[k];
  14.             k++;
  15.             LCD_DataWrite(picH,picL);
  16.         }
  17.     }
  18.         
  19. }
 
  1. #ifndef __PICTURE_H
  2. #define __PICTURE_H
  3.  
  4. #include "stm32f10x.h"
  5.  
  6. //SPI 彩屏图片数组
  7. extern const u8 Image_pic[];
  8.  
  9. #endif
 
  1. #include "picture.h"
  2.  
  3. const u8 Image_pic[]=
  4. { 
  5. 0x42,0x04,0x52,0xA7,0x4A,0x86,0x3A,0x44,0x32,0x82,0x53,0x64,0x4B,0x43,0x3A,0xA3,0x2A,0x22,0x4B,0x04,0x42,0xE2,0x5B,0xC5,0x5B,0xA4,0x3A,0xA2,0x32,0x41,0x32,0x82,
  6. 0x4B,0x65,0x2A,0x41,0x42,0xC3,0x4B,0x23,0x53,0x64,0x53,0x65,0x42,0xA3,0x3A,0x62,0x4B,0x05,0x6C,0x29,0x64,0x08,0x4B,0x24,0x32,0xA2,0x64,0x28,0x6C,0x28,0x4A,0xE4,
  7. 0x53,0x06,0x3A,0x25,0x31,0xC2,0x4A,0xC4,0x73,0xE7,0x7C,0x28,0x73,0xC7,0x63,0x46,0x6B,0x88,0x5B,0x25,0x5B,0x66,0x52,0xE5,0x52,0xA5,0x5A,0xE6,0x42,0x02,0x6B,0x68,
  8. 0xA5,0x0E,0x94,0x6C,0x63,0x06,0x7B,0x6A,0x20,0x60,0x7B,0x49,0x72,0xE7,0x39,0x62,0x6B,0x47,0x4A,0x85,0x6B,0x66,0x42,0x02,0x63,0x86,0x5B,0x84,0x53,0x43,0x6C,0x26,
  9. 0x6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值