通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

1.仿真预览

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

2.理论分析

标准硬件系统图像显示流程如下:

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

但是在仿真阶段,为了验证USB的功能,我们需要对系统的工作模式做下调整,使得符合仿真使用。具体如下所示:

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

注意,由于摄像机,在仿真阶段,没法获得实际的图片,我们这里通过摄像机驱动,驱动我们的存储器中的图片输出图像。然后将图像数据通过USB进行输出,并在modelsim仿真过程中,将仿真数据保存,然后用matlab对仿真数据进行解析,得到图像,从而验证USB接口的正确性。

然后配置上,还包括 CY7C68013 芯片的配置两个部分。

注意,由于我们将测试图片,做到 FPGA 的缓存中用于仿真了,实际下载硬件这个环节是没有的,所以这个做法会导致资源不够,所以再编译的时候按点如下安健:

(所以,我们这里采用图片大小是 600*800 的,原始相机 3000 多扫描, FPGA 仿真会非常非常非常非常非常慢,所以这里用较小的图片,然后后面提供的 matlab 成像软件,还是按照扫描模式来成像的。)

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

也就是第二个安全,这个安全是一个综合过程,只用于模拟,

第一个安全是下载硬件的安全。既然设置了缓存,肯定会出现资源不足的情况,所以没必要点这个安全。

下面介绍USB接口的定义:

1.USB时钟,

2.复位信号,

3.图像数据输入到USB接口,这个接口要加入FIFO

4.USB中断信号输入

5.USB读写使能信号

6.USB输出到电脑的接口,外部接口,这个接口要加入FIFO

7.输入信号触发信号

————————————————– ————————————————–

运行QII12.1版本软件,

首先可以看到系统的RTL结构图

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

进行modelsim仿真,得到如下的仿真结果:

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

模拟完成后会生成通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示,这个文件在通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

打开这个文件,将前面多个xx删除,然后复制到matlab文件夹

然后用matlab软件进行数据还原

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

双击读取数据,然后点击显示图片,得到如下结果:

通过FPGA实现USB接口传输图片,通过MATLAB对图片进行显示

说明USB数据接口成功。

3.部分核心代码

`timescale 1ns / 10ps

module tops(
            i_clk,
				i_rst,
				clk1,
				clk2,
				o_R_ccd,
				o_G_ccd,
				o_B_ccd,
				o_RGB,
				o_hang_plus,
				//usb fifo in
				o_data_fifo_in,
				o_almost_full_in,
				o_full_in,
				o_empty_in,
				o_usedw_in,
				//USB output
				o_wr_en,
				o_usb_start,
				o_USB_dout,
				o_rec_dat,
				o_rec_done,
				
				
				//usb fifo out
				o_data_fifo_o,
				o_almost_full_o,
				o_full_o,
				o_empty_o,
				o_usedw_o,
				SampleEnd,
				ifclk,
				slwr,
				slcs,
				PKTEND,
				addr0,
				addr1,
				flagb_led,
				flagc_led,
				flaga_led,
				slrd,
				sloe,
				stop,
				fifo_data
           );

input      i_clk;
input      i_rst;
output     clk1;
output     clk2;
output[7:0]o_R_ccd;
output[7:0]o_G_ccd;
output[7:0]o_B_ccd;
output[7:0]o_RGB;
output     o_hang_plus;


output[7:0]o_data_fifo_in;
output     o_almost_full_in;
output     o_full_in;
output     o_empty_in;
output[9:0]o_usedw_in;
 
output     o_wr_en;
output     o_usb_start;
output[7:0]o_USB_dout;
output[7:0]o_rec_dat;
output     o_rec_done;


output[7:0]o_data_fifo_o;
output     o_almost_full_o;
output     o_full_o;
output     o_empty_o;
output[9:0]o_usedw_o;


output SampleEnd;
output ifclk;
output slwr;
output slcs;
output PKTEND;
output addr0;
output addr1;
output flagb_led;
output flagc_led;
output flaga_led;
output slrd;
output sloe;
output stop;
output[7:0] fifo_data;

//clk 3pider
//clk 3pider
//clk 3pider
//clk 3pider
//clk 3pider
//clk 3pider
//clk 3pider
//clk 3pider
reg[3:0]cnt;
reg     clk1;
reg     clk2;
always @(posedge i_clk or posedge i_rst)
begin
     if(i_rst)
	  begin
	  cnt  <= 4'd0;
	  clk1 <= 1'b0;
	  clk2 <= 1'b0;
	  end
else begin
          if(cnt==4'd6)
			 cnt <= 4'd1;
	  else
	       cnt <= cnt + 4'd1; 

			 if(cnt == 4'd1) 
			 clk1 <= 1'b1;
			 else
			 clk1 <= 1'b0;
			 
			 if(cnt == 4'd1 | cnt == 4'd3  | cnt == 4'd5 ) 
			 clk2 <= 1'b1;
			 else
			 clk2 <= 1'b0;			 
			 
     end
end

 
//Images buffer to define the CCD
//Images buffer to define the CCD
//Images buffer to define the CCD
//Images buffer to define the CCD
//Images buffer to define the CCD
//Images buffer to define the CCD
//Images buffer to define the CCD
//Images buffer to define the CCD
//Images buffer to define the CCD
images_buffer uut(
                  .i_clk1  (clk1),
						.i_clk2  (clk2),
						.i_rst   (i_rst),
					   .i_ren   (1'b1), 
						.o_R     (o_R_ccd),
						.o_G     (o_G_ccd),
						.o_B     (o_B_ccd),
						.o_image (o_RGB),
						.o_hang_plus(o_hang_plus)//逐行输出扫描,hangplus为行信号,标注每一行的信息
                  );
						
//USB interface******************************************************************************************************
//USB interface******************************************************************************************************
//USB interface******************************************************************************************************
//USB interface******************************************************************************************************
//USB interface******************************************************************************************************
//USB interface******************************************************************************************************
//USB interface******************************************************************************************************
//USB interface******************************************************************************************************

//USB Controller
wire w_i;
wire r_i;
wire w_o;							  
wire r_o;
USB_send_controller ut4(
                        .clk2 (clk2),
								.i_rst(i_rst),
								.w_i  (w_i),
								.r_i  (r_i),
								.w_o  (w_o),
								.r_o  (r_o)
                       );

//fifo input
fifo_io fifo_io_u1(
	.aclr       (i_rst),
	.clock      (clk2),
	.data       (o_RGB),
	.rdreq      (r_i),
	.wrreq      (w_i),
	.almost_full(o_almost_full_in),
	.empty      (o_empty_in),
	.full       (o_full_in),
	.q          (o_data_fifo_in),
	.usedw      (o_usedw_in)
	);

 
//usb interface
usb_top usb_top_u(
           .i_clk            (clk2),      
			  .i_rst            (i_rst),
			  //USB中断控制输入信号
			  .i_usb_interrupt  (1'b0),     
			  //USB发送数据控制指令
		     .i_trans_start    (1'b1),   
			  //usb输入
			  .i_USB_din        (o_data_fifo_in),    
			  //输入发送数据长度 
			  .i_USB_len        (8'd8),
			  //write输出
			  .o_wr_en          (o_wr_en), 
	        //read输出		  
			  .o_rd_en          (o_usb_start),         
			  //usb输出
			  .o_USB_dout       (o_USB_dout),      
			  //输出数据
           .o_rec_dat        (o_rec_dat),     
			  //接收完成标志
			  .o_rec_done       (o_rec_done)      
			  );

//fifo output
fifo_io fifo_io_u2(
	.aclr       (i_rst),
	.clock      (clk2),
	.data       (o_USB_dout),
	.rdreq      (r_o),
	.wrreq      (w_o),
	.almost_full(o_almost_full_o),
	.empty      (o_empty_o),
	.full       (o_full_o),
	.q          (o_data_fifo_o),
	.usedw      (o_usedw_o)
	);

//CY7C68013驱动芯片的配置
wire SampleEnd;
wire ifclk;
wire slwr;
wire slcs;
wire PKTEND;
wire addr0;
wire addr1;
wire flagb_led;
wire flagc_led;
wire flaga_led;
wire slrd;
wire sloe;
wire stop;
wire[7:0] fifo_data;
CY7C68013_setup CY7C68013_setup_u
(
	.START(o_usb_start) ,	// input  START_sig
	.flaga(1'b1) ,	// input  flaga_sig
	.flagb(1'b1) ,	// input  flagb_sig
	.flagc(1'b1) ,	// input  flagc_sig
	.clkin(clk2) ,	// input  clkin_sig
	.SampleEnd(SampleEnd) ,	// output  SampleEnd_sig
	.ifclk    (ifclk) ,	// output  ifclk_sig
	.slwr     (slwr) ,	// output  slwr_sig
	.slcs     (slcs) ,	// output  slcs_sig
	.PKTEND   (PKTEND) ,	// output  PKTEND_sig
	.addr0    (addr0) ,	// output  addr0_sig
	.addr1    (addr1) ,	// output  addr1_sig
	.flagb_led(flagb_led) ,	// output  flagb_led_sig
	.flagc_led(flagc_led) ,	// output  flagc_led_sig
	.flaga_led(flaga_led) ,	// output  flaga_led_sig
	.slrd     (slrd) ,	// output  slrd_sig
	.sloe     (sloe) ,	// output  sloe_sig
	.stop     (stop) ,	// output  stop_sig
	.fifo_data(fifo_data) 	// output [7:0] fifo_data_sig
);

 
	

endmodule 
function varargout = tops(varargin)


gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @tops_OpeningFcn, ...
                   'gui_OutputFcn',  @tops_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before tops is made visible.
function tops_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to tops (see VARARGIN)

% Choose default command line output for tops
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes tops wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = tops_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global R1;
global G1;
global B1;
global I;
load data.txt
Len= 158;
R1 = [zeros(Len,1);data(2:3:2+3*480000-1-3*Len)];
G1 = [zeros(Len,1);data(3:3:3+3*480000-1-3*Len)];
B1 = [zeros(Len,1);data(4:3:4+3*480000-1-3*Len)];

I(:,:,1) = [reshape(R1',[800,600])]';%一共产生600个行信息,扫描成像,我将扫描的效果给你放慢做出来了
I(:,:,2) = [reshape(G1',[800,600])]';
I(:,:,3) = [reshape(B1',[800,600])]';
msgbox('读取完毕');


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global R1;
global G1;
global B1;
global I;
axes(handles.axes1);
I2 = zeros(size(I));
KK=15;
for i = 1:600/KK
I2(KK*(i-1)+1:KK*i,:,1) = I(KK*(i-1)+1:KK*i,:,1);
I2(KK*(i-1)+1:KK*i,:,2) = I(KK*(i-1)+1:KK*i,:,2);
I2(KK*(i-1)+1:KK*i,:,3) = I(KK*(i-1)+1:KK*i,:,3);
imshow(uint8(I2));
hold off
drawnow;
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc;
clear;
close all;

A38-08

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2022年3月18日
下一篇 2022年3月18日

相关推荐