通过FPGA将图片信息通过RS232串口发送到PC端,使用MATLAB进行图片显示
Posted fpga&matlab
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过FPGA将图片信息通过RS232串口发送到PC端,使用MATLAB进行图片显示相关的知识,希望对你有一定的参考价值。
1.仿真预览
2.部分核心代码
FPGA部分代码
module uarttx(clk,reset,datain,wrsig,tx);
input clk;
input reset;
input [7:0] datain;
input wrsig;
output tx;
reg idle;
reg tx;
reg send;
reg wrsigbuf;
reg wrsigrise;
reg presult;
reg [7:0] cnt;
parameter paritymode=1'b0;
always @(posedge clk)
begin
wrsigbuf<=wrsig;
wrsigrise<=(~wrsigbuf) & wrsig;
end
always @(posedge clk or negedge reset)
begin
if(reset==1'b0)
begin
send<=1'b0;
end
else
begin
send<=1'b1;
end
end
always @(posedge clk or negedge reset)
begin
if(reset==1'b0)
begin
cnt<=8'd0;
tx<=1'b1;
end
else begin
case(cnt)
8'd0:
begin
tx<=1'b0;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd16:
begin
tx<=datain[0];
presult<=datain[0]^paritymode;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd32:
begin
tx<=datain[1];
presult<=datain[1]^presult;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd48:
begin
tx<=datain[2];
presult<=datain[2]^presult;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd64:
begin
tx<=datain[3];
presult<=datain[3]^presult;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd80:
begin
tx<=datain[4];
presult<=datain[4]^presult;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd96:
begin
tx<=datain[5];
presult<=datain[5]^presult;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd112:
begin
tx<=datain[6];
presult<=datain[6]^presult;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd128:
begin
tx<=datain[7];
presult<=datain[7]^presult;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd144:
begin
tx<=presult;
presult<=datain[0]^paritymode;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd160:
begin
tx<=1'b1;
idle<=1'b1;
cnt<=cnt+8'd1;
end
8'd176:
begin
tx<=1'b1;
idle<=1'b0;
cnt<=cnt+8'd1;
end
default:
begin
cnt<=cnt+8'd1;
end
endcase
end
end
endmodule
MATLAB部分代码
global FileName;
global RR;
global CC;
RR=250;
CC=250;
C1=str2num(get(handles.edit1,'String'));
if C1==1
Serial_obj=serial('COM1');
end
if C1==2
Serial_obj=serial('COM2');
end
if C1==3
Serial_obj=serial('COM3');
end
if C1==4
Serial_obj=serial('COM4');
end
Serial_obj.BaudRate=str2num(get(handles.edit2,'String'));
Serial_obj.DataBits=str2num(get(handles.edit3,'String'));
Serial_obj.Parity='none';
Serial_obj.StopBits=str2num(get(handles.edit4,'String'));
% Serial_obj.Terminator=0;
Serial_obj.InputBufferSize=RR*CC; %保存扫描的5000*1行数据,保存1000个
% Serial_obj.timeout=20;
%% 创建txt文档。
Name = clock;
FileName=[num2str(Name(5)),num2str(round(Name(6))),'.txt'];
FileID1=fopen(FileName,'a+');
%% 打开串口,读写串口内容。
fopen(Serial_obj);
Temp_2 = [fscanf(Serial_obj,'%d')]';
fprintf(FileID1,'%d\\n',Temp_2); % Write data to text file
fprintf(FileID1,'\\n');
%% 关闭串口,清理缓存区
fclose(FileID1);
fclose(Serial_obj);
delete(Serial_obj);
clear Serial_obj
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 FileName;
global RR;
global CC;
I = load(FileName);
%读取标志信号
start = [];
for i = 5001:length(I)
tmps = I(i-5000:i-1);
cnt = 0;
for j = 1:length(tmps)
if tmps(j) == 153
cnt = cnt + 1;
end
end
if cnt==5000
start = [start,i+1];
end
end
pix_bigen = start(end);
if isempty(pix_bigen)==0
II = I(pix_bigen:end);
else
II = I;
end
if length(II) < RR*CC
III = [II;zeros(RR*CC-length(II),1);];
else
III = II;
end
I2= [reshape(III,[RR,CC])]';
I3 = zeros(size(I2));
axes(handles.axes1);
KK=10;
for i = 1:RR/KK
I3(KK*(i-1)+1:KK*i,:) = I2(KK*(i-1)+1:KK*i,:);
imshow(uint8(I3));
hold off
drawnow;
end
A38-09
以上是关于通过FPGA将图片信息通过RS232串口发送到PC端,使用MATLAB进行图片显示的主要内容,如果未能解决你的问题,请参考以下文章