【小梅哥FPGA进阶教程】第十四章 TFT屏显示图片

本文介绍如何在FPGA中通过TFT显示屏驱动设计显示图片,使用 Img2Lcd 转换BMP格式,BMP2Mif生成mif文件,结合ROM IP核和控制逻辑实现图片显示,并提供了仿真验证和板级验证的过程。
摘要由CSDN通过智能技术生成
十四、TFT屏显示图片

本文由杭电网友曾凯峰贡献,特此感谢

学习了小梅哥的TFT显示屏驱动设计后,想着在此基础上通过TFT屏显示一张图片,有了这个想法就开始动工了。首先想到是利用FPGA内部ROM存储图片数据,然后通过控制读取数据地址将图片数据传给TFT驱动模块,从而将每个图片数据显示在对应的像素点上。整个设计的框图如下:

图片1

主要是在小梅哥TFT驱动设计基础上增加了图片数据发送控制模块Imgdata_send,该模块包括存储图片数据的rom,和一些简单的逻辑控制。具体的rom IP核的建立我这里就不说了,不懂的可以参看小梅哥的相关内容的视频,我这里主要讲如何将图片转换成mif文件。这里有两种方法,可以作为参考,主要用到如下软件:

图片2

步骤1:利用Img2Lcd将图片转化为BMP格式的(当然图片本身为BMP格式就不需要转了,直接进入步骤2);

步骤2:利用BMP2Mif可将图片转化为mif文件。

具体实现如下:

步骤1:先打开Img2Lcd打开一张图片,选择输出格式为BMP格式,输出灰度选择24位真彩色(由于BMP2Mif软件只能载入24位或8位的,所以这里就没有直接选16位的真彩色),最大宽度和高度根据图片实际的大小进行选择的,由于内部rom能存储的数据量有限,我选择了一张像素为160*120的图片如果想显示大的图片,如480*272图片,用这种方法就不能实现(看到这里有人想,如果想显示大点的图片那应该怎么解决,后面我会有其他方法来实现TFT屏保显示)。

图片3

步骤2:打开BMP2Mif软件,加载刚转换输出的24位BMP格式图片,选择输出图像格式和文件类型,点击一键转换便将图片转换为了mif文件了,可以将mif文件名更改下以区别不同图片mif文件。

图片4

可以用Notepad++将转化的mif文件打开看看,截取部分图如下:

图片5

rom IP核设置完成后就是数据发送控制模块Imgdata_send中控制逻辑的编写,主要是让图片显示在屏幕指定的地方,这就需要根据TFT_CTRL模块的TFT行和场扫描计数器输出信号来控制rom的数据地址,从而控制TFT_CTRL的待显示数据data_in。具体代码如下:

1   module Imgdata_send(
2       clk50M,
3       rst_n,
4       tft_de,
5       hcount,
6       vcount,
7       data_in
8   );
9 
10      input clk50M;           //系统时钟,默认50M
11      input rst_n;            //复位信号,低电平有效
12      input tft_de;           //TFT数据使能
13      input [9:0]hcount;     //TFT行扫描计数器
14      input [9:0]vcount;     //TFT场扫描计数器
15      output [15:0]data_in; //待显示图片数据
16      
17      wire img_ack;              //图片数据使能
18      
19      localparam IMG_H = 160,  //图片行像素点个数
20                   IMG_V = 120;  //图片场像素点个数
21                    
22      localparam TFT_H = 480,  //TFT屏行像素点个数
23                   TFT_V = 272;  //TFT屏场像素点个数
01 试看小梅哥fpga设计思想与验证方法视频:http://bbs.elecfans.com/jishu_484523_1_1.html 02 芯航线FPGA开发平台设计初衷:http://bbs.elecfans.com/jishu_514523_1_1.html 03 芯航线FPGA学习平台介绍:http://bbs.elecfans.com/jishu_514526_1_1.html 04 芯航线FPGA学习平台焊接记录:http://bbs.elecfans.com/jishu_514914_1_1.html 05 芯航线FPGA学习平台系统调试:http://bbs.elecfans.com/jishu_515169_1_1.html 06【连载视频教程(一)】科学的开发流程:http://bbs.elecfans.com/jishu_515340_1_1.html 07【连载视频教程(二)】3-8译码器设计验证:http://bbs.elecfans.com/jishu_515468_1_1.html 08【连载视频教程(三)】二进制计数器设计验证:http://bbs.elecfans.com/jishu_515476_1_1.html 09【连载视频教程(四)】高性能计数器IP核使用:http://bbs.elecfans.com/jishu_515714_1_1.html 10【连载视频教程(五)】BCD码计数器设计验证:http://bbs.elecfans.com/jishu_515893_1_1.html 11【连载视频教程(六)】例解阻塞赋值与非阻塞赋值:http://bbs.elecfans.com/jishu_516094_1_1.html 12【连载视频教程(七)】例说状态机:http://bbs.elecfans.com/jishu_516249_1_1.html 13【连载视频教程(八)】基于状态机的独立按键消抖:http://bbs.elecfans.com/jishu_516791_1_1.html 14【连载视频教程(九)】独立按键控制LED与亚稳态问题引入:http://bbs.elecfans.com/jishu_516795_1_1.html 15【连载视频教程(十)】数码管动态扫描设计与实现:http://bbs.elecfans.com/jishu_516979_1_1.html 16【连载视频教程(十一)】UART串口发送模块设计与验证:http://bbs.elecfans.com/jishu_516984_1_1.html 17【连载视频教程(十二)】UART串口接收模块设计与验证:http://bbs.elecfans.com/jishu_518308_1_1.html 18【芯航线FPGA学习平台众筹进度帖】芯航线FPGA开发板入手测试说明:http://bbs.elecfans.com/jishu_518301_1_1.html 19【芯航线FPGA学习平台众筹进度帖】芯航线FPGA学习套件配置文件固化教程 http://bbs.elecfans.com/jishu_518816_1_1.html 20【芯航线FPGA学习平台众筹进度帖】芯航线FPGA学习套件众筹回报已发放完毕,特公布运单号 http://bbs.elecfans.com/jishu_518819_1_1.html 21【芯航线FPGA学习平台众筹进度帖】芯航线FPGA学习套件众筹总结暨抽奖结果 http://bbs.elecfans.com/jishu_520454_1_1.html 22【芯航线FPGA学习平台众筹进度帖】【有奖活动】芯航线FPGA学习平台有奖活动第一季——看教程,秀笔记,赢好礼 http://bbs.elecfans.com/jishu_520463_1_1.html 23【连载视频教程(十三)】嵌入式块RAM应用之双口RAM:http://bbs.elecfans.com/jishu_520914_1_1.html 24【连载视频教程(十四)】搭建串口收发与存取双口RAM简易应用系统: http://bbs.elecfans.com/jishu_520915_1_1.html 25【连载视频教程(十五)】ROM的搭建与Signaltap II 工具使用:http://bbs.elecfans.com/jishu_522774_1_1.html 26【连载视频教程(十六)】FIFO介绍与时序验证:http://bbs.elecfans.com/jishu_522778_1_1.html 27【连载视频教程(十七)】使用PLL进行设计+Verilog参数化设计介绍: http://bbs.elecfans.com/jishu_524442_1_1.html 28【芯航线FPGA学习平台众筹进度帖】芯航线FPGA学习套件下载器驱动安装说明 http://bbs.elecfans.com/jishu_524793_1_1.html 29【芯航线FPGA学习平台众筹进度帖】Verilog语法基础讲解之参数化设计: http://bbs.elecfans.com/jishu_528770_1_1.html 30【连载视频教程(十八)】基于线性序列机设计思想的串行DAC(TLC5620)驱动: http://bbs.elecfans.com/jishu_533944_1_1.html 31【连载视频教程(十九)】基于线性序列机设计思想的串行ADC驱动: http://bbs.elecfans.com/jishu_537056_1_1.html
Bmp To Mif 转换器 // (karimov 2005) // This program was originnaly written by one of the ECE241 students to convert an image // supplied in a BMP file into an MIF file format for use with Quartus II. // // This program has recently been modified to work with the new VGA controller used with the DE2 // board. // // What to do: // 1. Create an image in Microsoft paint (or other program). The image must be 160 pixels wide, 120 pixels high and // use 24-bits to represent colour information. // 2. Once you create the image you need, flip it up-side down. Then save the BMP file. (example: foo.bmp) // 3. Run this converter in command prompt using the name of the BMP file you created as a command-line parameter. // For example: // bmp2mif foo.bmp // 4. The program generates two files: // image.colour.mif - an MIF file with 3 bits colour information obtained from the BMP file you supplied // image.mono.mif - an MIF file containing 1 bit of colour for every pixel on the screen. The dot will either be // black or white. // You can change the file names once they are created, but they should still have the .mif extension. // // 5. Copy the proper MIF file to the directory where your design is located and include it in your project. // 6. Change the BACKGROUND_IMAGE parameter of the VgaAdapter to indicate your MIF file. // 7. The COLOR_CHANNEL_DEPTH parameter must be set to 1 to work with the image.colour.mif file. #include #include #define FLIP_INT(c) ((c >> 24) & 0x000000FF) | ((c & 0x00FF0000) >> 8) | ((c & 0x0000FF00) << 8) | ((c & 0x000000FF) <> 8) | ((c & 0x00FF) << 8) typedef struct s_header { unsigned short bfType; unsigned int bfSize; unsigned short reserved1; unsigned short reserved2; unsigned int offset; } t_bmp_header; typedef struct s_bmp_info { unsigned int biSize; unsigned int biWidth; unsigned int biHeight; unsigned short biPlanes; unsigned short biBitCount; unsigned int biCompression; unsigned int biSizeImage; unsigned int biXPelsPerMeter; unsigned int biYPelsPerMeter; unsigned int biClrUsed; unsigned int biClrImportant; } t_bmp_info; int faprint(FILE *fcol, FILE *fm, const char *pattern) { fprintf(fcol, pattern); return fprintf(fm, pattern); } int main(int argc, char* argv[]) { FILE *f, *fcol, *fm; int y; unsigned int x, c, r, g, b; unsigned int width, height; if (argc != 2) { printf("Usage: bmp2mif \n"); return 0; } else { printf("Input file is: %s\n", argv[1]); } printf("This program converts n x m 24-bit .BMP image to MIF file\n"); printf("There are 2 files produced:\n"); printf("\timage.colour.mif - 8-colour channel, n x m x 3\n"); printf("\timage.mono.mif - black and white image, n x m x 1\n\n"); f = fopen(argv[1], "rb"); fcol = fopen("image.colour.mif", "wb"); fm = fopen("image.mono.mif", "wb"); if (f) { t_bmp_header header; t_bmp_info info; fread(&header, 14, 1, f); /* sizeof(t_bmp_header) returns 16 instead of 14. Should be 14. */ fread(&info, sizeof(t_bmp_info), 1, f); #if !defined (WIN32) header.bfSize = FLIP_INT(header.bfSize); header.bfType = FLIP_SHORT(header.bfType); header.offset = FLIP_INT(header.offset); header.reserved1 = FLIP_SHORT(header.reserved1); header.reserved2 = FLIP_SHORT(header.reserved2); info.biSize = FLIP_INT(info.biSize); info.biWidth = FLIP_INT(info.biWidth); info.biHeight = FLIP_INT(info.biHeight); info.biPlanes = FLIP_SHORT(info.biPlanes); info.biBitCount = FLIP_SHORT(info.biBitCount); info.biCompression = FLIP_INT(info.biCompression); info.biSizeImage = FLIP_INT(info.biSizeImage); info.biXPelsPerMeter = FLIP_INT(info.biXPelsPerMeter); info.biYPelsPerMeter = FLIP_INT(info.biYPelsPerMeter); info.biClrUsed = FLIP_INT(info.biClrUsed); info.biClrImportant = FLIP_INT(info.biClrImportant); #endif printf("Input file is %ix%i %i-bit depth\n", info.biWidth, info.biHeight, info.biBitCount); if (info.biBitCount == 24) { char temp[100]; width = info.biWidth; height = info.biHeight; printf("Converting...\n"); sprintf(temp, "Depth = %i;\r\n",width*height); faprint(fcol, fm, temp); fprintf(fcol, "Width = 3;\r\n"); fprintf(fm, "Width = 1;\r\n"); faprint(fcol, fm, "Address_radix=dec;\r\n"); faprint(fcol, fm, "Data_radix=bin;\r\n"); faprint(fcol, fm, "Content\r\n"); faprint(fcol, fm, "BEGIN\r\n"); sprintf(temp, "\t[0..%i] : 000;\r\n", width*height - 1); fprintf(fcol, temp); sprintf(temp, "\t[0..%i] : 0;\r\n", width*height - 1); fprintf(fm, temp); fseek(f, 54, SEEK_SET); for(y=height-1; y >=0; y--) { x = 0; fprintf(fcol, "\t%i :", y*width+x); fprintf(fm, "\t%i :", y*width+x); for(x=0; x 0) && ((x % 40) == 0)) { fprintf(fcol, ";\r\n\t%i :", y*width + x); fprintf(fm, ";\r\n\t%i :", y*width + x); } #if defined (WIN32) c = ((c >> 24) & 0x000000FF) | ((c & 0x00FF0000) >> 8) | ((c & 0x0000FF00) << 8) | ((c & 0x000000FF) <>= 8; b = (c & 0xFF0000) >> 16; g = (c & 0x00FF00) >> 8; r = (c & 0x0000FF); c = r + g + b; c /= 3; r = (r >= 128 ? 1 : 0); g = (g >= 128 ? 1 : 0); b = (b >= 128 ? 1 : 0); c = (c >= 128 ? 1 : 0); fprintf(fcol, " %i%i%i", r, g, b); fprintf(fm, " %i", c); } faprint(fcol, fm, ";\r\n"); if ((x*3) % 4 != 0) { fread(&c, 4-((x*3) % 4), 1, f); } } faprint(fcol, fm, "End;\r\n"); } else printf("Input file image.bmp is not in a 24-bit colour format!\n"); fclose(fm); fclose(fcol); fclose(f); printf("All done.\n"); } else printf("Cannot open input file. Check for input.bmp\n"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值