源代码 module uart_byte_tx( clk, reset_n, data, send_en, baud_set, uart_tx, tx_done ); input clk; input reset_n; input [7:0] data; input send_en; input [2:0] baud_set; output reg uart_tx; output reg tx_done; //设置可选择选择波特率的模式 reg [17:0] bps_DR; always@(*) case(baud_set) 0:bps_DR=1000000000/9600/20; 1:bps_DR=1000000000/19200/20; 2:bps_DR=1000000000/38400/20; 3:bps_DR=1000000000/57600/20; 4:bps_DR=1000000000/115200/20; default:bps_DR=1000000000/9600/20; endcase //设置分频计数器,满足最低的波特率的位数,18位。 reg [17:0] div_cnt; always @(posedge clk or negedge reset_n) if(!reset_n) div_cnt<=0; else if(send_en)begin if(div_cnt==bps_DR-1) div_cnt<=0; else div_cnt<=div_cnt+1'b1; end else div_cnt<=0; wire bps_clk; assign bps_clk=(div_cnt==1); //开始讲每个时间段连接起来。 reg [3:0] bps_cnt; always@(posedge clk or negedge reset_n) if(!reset_n) bps_cnt<=0; //1、这里的等于div_cnt==1是为了让计数器从0计数到1时时间比较段,二不产生太大延迟, //2、加入send_en是为了让计数完及时清零。 else if(send_en)begin if(div_cnt==1) begin//每次计数到11清零,其他时间进行加一的操作。 if(bps_cnt==11) bps_cnt<=0; else bps_cnt<=bps_cnt+1'b1; end end else bps_cnt<=0; //开始发送数据 always@(posedge clk or negedge reset_n) if(!reset_n)begin uart_tx<=1'b1; tx_done<=0; end else case(bps_cnt) //如果从0开始的话,会在中间间隔时间(空窗期),使得uart_tx=0; //因为发送初始位置时(0时刻),就让uart_tx<=0了. 1:begin uart_tx<=0;tx_done<=0; end 2:uart_tx<=data[0]; 3:uart_tx<=data[1]; 4:uart_tx<=data[2]; 5:uart_tx<=data[3]; 6:uart_tx<=data[4]; 7:uart_tx<=data[5]; 8:uart_tx<=data[6]; 9:uart_tx<=data[7]; 10:uart_tx<=1; 11:begin uart_tx<=1;tx_done<=1;end default uart_tx<=1; endcase endmodule 仿真代码 `timescale 1ns/1ns module uart_byte_tx_tb(); reg clk; reg reset_n; reg [7:0] data; reg send_en; reg [2:0] baud_set; wire uart_tx; wire tx_done; uart_byte_tx uart_byte_tx_inst0( .clk(clk), .reset_n(reset_n), .data(data), .send_en(send_en), .baud_set(baud_set), .uart_tx(uart_tx), .tx_done(tx_done) ); initial clk=1; always #10 clk=!clk; initial begin reset_n=0; data=0; baud_set=4; send_en=0; #201; reset_n=1; #20; data=8'h57; send_en=1; @(posedge tx_done) send_en=0; #2000; data=8'h75; send_en=1; #20; @(posedge tx_done) send_en=0; #20000; $stop; end endmodule
标签:20,tx,1000000000,bps,调试,串口,input,DR,小梅哥 From: https://www.cnblogs.com/wangwin4/p/17141109.html