百度网站地图制作,大气微电影类网站织梦模板完整版,互联网技术类岗位有哪些,辛集手机网站建设先了解一下关于uart和rs232的基础知识 文章目录 一、RS232的回环测试1.1模块整体架构1.2 rx模块设计1.2.1 波形设计1.2.2代码实现与tb1.2.4 仿真 1.3 tx模块设计1.3.1 波形设计1.3.2 代码实现与tb1.3.4 顶层设计1.3.3 仿真 本篇内容#xff1a;
一、RS232的回环测试
上位机…先了解一下关于uart和rs232的基础知识 文章目录 一、RS232的回环测试1.1模块整体架构1.2 rx模块设计1.2.1 波形设计1.2.2代码实现与tb1.2.4 仿真 1.3 tx模块设计1.3.1 波形设计1.3.2 代码实现与tb1.3.4 顶层设计1.3.3 仿真 本篇内容
一、RS232的回环测试
上位机由串口助手通过 rx 线往 FPGA 发 8 比特数据当 FPGA接收到 8 比特数据后再通过 tx 线把接收到的 8 比特数据给上位机发回去要求上位机接收到的数据和上位机发送的数据一样并且保证连续发送也没问题。 1.1模块整体架构 1.2 rx模块设计
1.2.1 波形设计 1.2.2代码实现与tb
代码
module uart_rx(input wire clk,input wire rst,input wire rx,output reg [7:0]po_data,output reg po_flag);parameter CNT_END100; //9600bps cnt5207 sim时cnt100;
parameter CNT_END_HALFCNT_END/2;reg rx_t;
reg rx_tt;
reg rx_tt_reg;
reg [12:0] cnt;
reg cnt_flag;
reg bit_flag;
reg [3:0]bit_cnt;
// rx_t
always (posedge clk) begin if(rst1b1) beginrx_t d1;end else beginrx_trx ;end
end// rx_tt
always (posedge clk) begin if(rst1b1) beginrx_tt d1;end else beginrx_ttrx_t ;end
end// rx_tt_reg
always (posedge clk) begin if(rst1b1) beginrx_tt_reg d1;end else beginrx_tt_regrx_tt ;end
end//cnt
always (posedge clk) begin if(rst1b1) begincnt d0;end else if (cnt_flag1b1 cntCNT_END) begincntd0;endelse if (cnt_flag1b1) begincntcnt1b1;endelse if (bit_cntd8 bit_flag1b1) begincntd0;end
end//bit_flag
always (posedge clk) begin if(rst1b1) beginbit_flag d0;end else if (cnt_flag1b1 cntCNT_END_HALF) beginbit_flagd1;endelse bit_flagd0;
end// cnt_flag
always (posedge clk) begin if(rst1b1) begincnt_flag d0;end else if (rx_tt1b0 rx_tt_reg1b1) begincnt_flagd1;endelse if (bit_cntd8 cntCNT_END_HALF) begincnt_flagd0;end
end// bit_cnt
always (posedge clk) begin if(rst1b1) beginbit_cnt d0;end else if (bit_cntd8 bit_flag1b1) beginbit_cntd0;endelse if (bit_flag1b1) beginbit_cntbit_cnt1b1;endend// po_data
always (posedge clk) begin if(rst1b1) beginpo_data d0;end else if (bit_cnt0 bit_flag1b1) beginpo_data{rx,po_data[7:1]};end
end// po_flag
always (posedge clk) begin if(rst1b1) beginpo_flag d0;end else if (bit_cntd8 bit_flag1b1) beginpo_flagd1;endelse po_flagd0;
end
endmodule
tb
timescale 1ns / 1psmodule tb_rx();reg clk;reg rst;reg rx;wire [7:0] po_data;wire po_flag;initial beginclk0;rst1;#100rst0;endinitial beginrx1;//空闲状态#100gen_rx();end//这里模拟发送20帧数据每次发送8个0~1的任意数发送前rx拉低表示起始位
//由于9600波特率需要计数5207次为了仿真方便假设只需要计数100次。task gen_rx;integer i;integer j;begin for (j 0; j 20; jj1) beginrx0;for ( i 0; i 8; ii1) begin repeat(100) begin //每隔100周期发送1bit数据(posedge clk); endrx{$random};endrx1; //每发送完一帧数据后rx恢复空闲状态维持10个周期后继续发送数据直到发够20帧数据。repeat(10) begin(posedge clk);end endendendtask always #5 clk~clk;uart_rx inst_uart_rx (.clk (clk),.rst (rst),.rx (rx),.po_data (po_data),.po_flag (po_flag));endmodule
1.2.4 仿真 1.3 tx模块设计
1.3.1 波形设计 1.3.2 代码实现与tb
module uart_tx(input wire clk,input wire rst,input wire[7:0] po_data,input wire po_flag,output reg tx);parameter CNT_END100; // bps为9600时这里为5207 为仿真方便设为100。reg [7:0] po_data_reg;
reg [12:0]cnt;
reg cnt_flag;
reg bit_flag;
reg [3:0] bit_cnt;
// po_data_reg
always (posedge clk) begin if(rst1b1) beginpo_data_reg d0;endelse po_data_regpo_data;
end// cnt
always (posedge clk) begin if(rst1b1) begincnt d0;end else if (cnt_flag1b1 cntCNT_END) begincnt d0;endelse if (cnt_flag1b1) begincntcnt1b1;endend//cnt_flag
always (posedge clk) begin if(rst1b1) begincnt_flag d0;end else if (po_flag1b1) begincnt_flagd1;endelse if (bit_cntd8 bit_flag1b1) begincnt_flagd0;end
end// bit_flag
always (posedge clk) begin if(rst1b1) beginbit_flag d0;end else if (cntCNT_END-1 cnt_flag1b1) beginbit_flagd1;endelse bit_flagd0;
end// bit_cnt
always (posedge clk) begin if(rst1b1) beginbit_cnt d0;end else if (bit_flag1b1 bit_cntd8) beginbit_cntd0;endelse if (bit_flag1b1) beginbit_cntbit_cnt1b1;end
end
// tx
always (posedge clk) begin if(rst1b1) begintx d1;end else if (po_flag1b1) begintxd0;endelse if (bit_flag1b1 bit_cntd8) begintxd1;endelse if (bit_flag1b1) begintxpo_data_reg[bit_cnt];end
end
endmodule
tb:
timescale 1ns / 1ps
module tb_rx();reg clk;reg rst;reg rx;wire tx;initial beginclk0;rst1;#100rst0;endinitial beginrx1;//空闲状态#100gen_rx();end//这里模拟发送20次数据每次发送8个0~1的任意数发送前rx拉低表示起始位
//由于9600波特率需要计数5207次为了仿真方便假设只需要计数100次。task gen_rx;integer i;integer j;begin for (j 0; j 20; jj1) beginrx0;for ( i 0; i 8; ii1) begin repeat(100) begin //每隔100周期发送1bit数据(posedge clk); endrx{$random};endrx1; //每发送完一帧数据后rx恢复空闲状态维持100个周期(方便tx端完整传输完一帧数据)后继续发送数据直到发够20帧数据。repeat(1000) begin(posedge clk);end endendendtask always #5 clk~clk;top_uart inst_top_uart (.clk(clk), .rst(rst), .rx(rx), .tx(tx));endmodule
1.3.4 顶层设计
module top_uart(input wire clk,input wire rst,input wire rx,output wire tx);wire [7:0] po_data;
wire po_flag;uart_rx inst_uart_rx (.clk (clk),.rst (rst),.rx (rx),.po_data (po_data),.po_flag (po_flag));uart_tx inst_uart_tx (.clk (clk),.rst (rst),.po_data (po_data),.po_flag (po_flag),.tx (tx));endmodule1.3.3 仿真 可以看到rx和tx波形一致则能实现传输要求。