名称:一种数据通信系统设计Verilog代码Quartus仿真
软件:Quartus
语言:Verilog
代码功能:
要求:系统为同步设计。TX模块通过串行通道向RX模块发送串行数据,串行数据中包含有效数据和无效数据,串行通道仅有1位时钟和1位数据,RX应正确接收TX发来的有效数据,不接收无效数据。自定义标记有效数据和无效数据的方法。
FPGA代码Verilog/VHDL代码资源下载:www.hdlcode.com
演示视频:
设计文档:
1. 发送端代码文件
2. 接收端代码文件
3. 发送端testbench
4. 接收端testbench
5. 仿真图
发送端仿真
发送接收仿真
部分代码展示:
//发送端 module source( input sys_clk, //系统时钟 input sys_rst_n, //系统复位,低电平有效 input TS_en, //发送使能信号 input [7:0] TS_data, //待发送数据 output reg clock, //串行通道发送时钟 output reg data //串行通道发送端口 ); //parameter define parameter CLK_FREQ = 50000000; //系统时钟频率 parameter BPS = 115200; //波特率 localparam BPS_CNT = CLK_FREQ/BPS; //为得到指定波特率,对系统时钟计数BPS_CNT次 //reg define reg TS_en_d0; reg TS_en_d1; reg [15:0] clk_cnt; //系统时钟计数器 reg [ 3:0] tx_cnt; //发送数据计数器 reg tx_flag; //发送过程标志信号 reg [ 7:0] tx_data; //寄存发送数据 //wire define wire en_flag; //***************************************************** //** main code //***************************************************** //对发送使能信号TS_en延迟两个时钟周期 always @(posedge sys_clk or negedge sys_rst_n) begin if (!sys_rst_n) begin TS_en_d0 <= 1'b0; TS_en_d1 <= 1'b0; end else begin TS_en_d0 <= TS_en; TS_en_d1 <= TS_en_d0; end end //捕获TS_en上升沿,得到一个时钟周期的脉冲信号 assign en_flag = (~TS_en_d1) & TS_en_d0; //当脉冲信号en_flag到达时,寄存待发送的数据,并进入发送过程 always @(posedge sys_clk or negedge sys_rst_n) begin if (!sys_rst_n) begin tx_flag <= 1'b0; tx_data <= 8'd0; end else if (en_flag) begin //检测到发送使能上升沿 tx_flag <= 1'b1; //进入发送过程,标志位tx_flag拉高 tx_data <= TS_data; //寄存待发送的数据 end else if ((tx_cnt == 4'd9)&&(clk_cnt == BPS_CNT/2)) begin //计数到停止位中间时,停止发送过程 tx_flag <= 1'b0; //发送过程结束,标志位tx_flag拉低 tx_data <= 8'd0; end else begin tx_flag <= tx_flag; tx_data <= tx_data; end end //进入发送过程后,启动系统时钟计数器与发送数据计数器 always @(posedge sys_clk or negedge sys_rst_n) begin if (!sys_rst_n) begin clk_cnt <= 16'd0; tx_cnt <= 4'd0; end else if (tx_flag) begin //处于发送过程 if (clk_cnt < BPS_CNT - 1) begin clk_cnt <= clk_cnt + 1'b1; tx_cnt <= tx_cnt; end else begin clk_cnt <= 16'd0; //对系统时钟计数达一个波特率周期后清零 tx_cnt <= tx_cnt + 1'b1; //此时发送数据计数器加1 end end else begin //发送过程结束 clk_cnt <= 16'd0; tx_cnt <= 4'd0; end end //根据clk_cnt计数器控制输出串行通道发送时钟 always @(posedge sys_clk or negedge sys_rst_n) if (!sys_rst_n) clock<=0; else if (clk_cnt > BPS_CNT/2) clock<=1;//高电平 else clock<=0;//低电平
点击链接获取代码文件:http://www.hdlcode.com/index.php?m=home&c=View&a=index&aid=588
阅读全文
532