FPGA到PC的UART tx的程序设计
顶层设计
module top
(CLK,RSTn,TX_Pin_Out
);input CLK;
input RSTn;output TX_Pin_Out;wire[7:0] TX_Data;
wire TX_EN_Sig;control_module m1(.CLK(CLK),.RSTn(RSTn),.TX_Done_Sig(TX_Done_Sig),.TX_En_Sig(TX_En_Sig),.TX_Data(TX_Data));
UART_Tx m2(.CLK(CLK),.RSTn(RSTn),.TX_Data(TX_Data),.TX_En_Sig(TX_En_Sig),.TX_Pin_Out(TX_Pin_Out),.TX_Done_Sig(TX_Done_Sig));endmodule
数据传输:传输00~FF的数据
module control_module
(CLK,RSTn,TX_Done_Sig,TX_En_Sig,TX_Data
);
input CLK;
input RSTn;
input TX_Done_Sig;
output TX_En_Sig;
output[7:0] TX_Data;parameter N1=50000;
reg[24:0] count;
reg isEn;
reg[7:0] rData;
wire TX_En_Sig;
wire[7:0] TX_Data;always@(posedge CLK or negedge RSTn)
beginif( !RSTn)count <= 24'd0;else if(count == N1)count <= 24'd0;elsecount<=count+1'b1;
endalways@(posedge CLK or negedge RSTn)
beginif(!RSTn)beginisEn <= 1'b0;rData <= 8'h00;endelse if( TX_Done_Sig)beginrData <= rData + 1'b1;isEn <= 1'b0;endelse if(count == N1)isEn <= 1'b1;
end assign TX_Data = rData;
assign TX_En_Sig = isEn;endmodule
UART——tx封装
module UART_Tx
(CLK,RSTn,TX_Data,TX_En_Sig,TX_Pin_Out,TX_Done_Sig
);
input CLK;
input RSTn;
input[7:0] TX_Data;
input TX_En_Sig;output TX_Pin_Out;output TX_Done_Sig;tx_control_module U1(.clk2(CLK), .rst2(RSTn),.TX_En_Sig(TX_En_Sig), .TX_Data(TX_Data),.BPS_CLK(BPS_CLK), .TX_Done_Sig(TX_Done_Sig), .TX_Pin_Out(TX_Pin_Out));tx_bps_module U2(.clk1(CLK), .rst1(RSTn),.Count_Sig(TX_En_Sig), .BPS_CLK(BPS_CLK));endmodule
波特率:9600
module tx_bps_module//数据位中心定位模块
(clk1, rst1,Count_Sig, BPS_CLK
);input clk1;
input rst1; //复位信号 RSTn,当RSTn=0,系统复位
input Count_Sig; //串口数据帧开始确认信号:Count_Sig,当 Count_Sig=1,表示串口输入帧开始时刻,持续一个系统时钟周期
output BPS_CLK; //BPS_CLK,当计数至每一位的中间位置,BPS_CLK=1,提示信号采集时间到,否则不进行信号采集reg[12:0] Count_BPS;always @ ( posedge clk1 or negedge rst1 )if( !rst1 )Count_BPS <= 13'd0;else if( Count_BPS == 13'd5207 )Count_BPS <= 13'd0;else if( Count_Sig )Count_BPS <= Count_BPS + 1'b1;elseCount_BPS <= 13'd0;assign BPS_CLK = ( Count_BPS == 12'd2604 ) ? 1'b1 : 1'b0;endmodule
UART_tx数据传输模块
module tx_control_module
(clk2, rst2,TX_En_Sig, TX_Data, BPS_CLK, TX_Done_Sig, TX_Pin_Out);input clk2;
input rst2;
input TX_En_Sig;
input[7:0] TX_Data;
input BPS_CLK;output TX_Done_Sig;
output TX_Pin_Out;reg[3:0] i;
reg rTX;
reg isDone;always @ ( posedge clk2 or negedge rst2 )if( !rst2 )begini <= 4'd0;rTX <= 1'b1;isDone <= 1'b0;endelse if( TX_En_Sig )case ( i )4'd0 :if( BPS_CLK )begini <= i + 1'b1; rTX <= 1'b0;end4'd1, 4'd2, 4'd3, 4'd4, 4'd5, 4'd6, 4'd7, 4'd8 :if( BPS_CLK )begini <= i + 1'b1;rTX <= TX_Data[ i - 1 ];end4'd9 :if( BPS_CLK )begini <= i + 1'b1;rTX <= 1'b1;end 4'd10 :if( BPS_CLK )begini <= i + 1'b1;rTX <= 1'b1;end4'd11 :if( BPS_CLK )begini <= i + 1'b1;isDone <= 1'b1;end4'd12 :begini <= 4'd0;isDone <= 1'b0;endendcaseassign TX_Pin_Out = rTX;
assign TX_Done_Sig = isDone;endmodule