接着上一篇,今天我们来建立一个能用于实际工程中的DEMO。

  首先,为了使我们的发送机不像上一个DEMO一样无节制的循环发送,我们需要修改代码,增加使发送机停止发送的控制部分,修改后的代码如下:

  1 `timescale 1ns / 1ps
  2 //////////////////////////////////////////////////////////////////////////////////
  3 // Company: 
  4 // Engineer:         lwy
  5 // 
  6 // Create Date:    16:00:47 11/11/2013 
  7 // Design Name:    usb-uart tx device for NEXYS3
  8 // Module Name:    UART_CTRL 
  9 // Project Name: 
 10 // Target Devices: 
 11 // Tool versions: 
 12 // Description:This component may be used to transfer data over a UART device. It will
 13 //                    serialize a byte of data and transmit it over a TXD line. The serialized
 14 //                 data has the following characteristics:
 15 //                         *9600 Baud Rate
 16 //                         *8 data bits, LSB first
 17 //                         *1 stop bit
 18 //                         *no parity
 19 //
 20 // Dependencies: 
 21 // Port Descriptions:
 22 //                            clk           -- 外部100M时钟输入;
 23 //                            data          -- 需要传输的数据,8位;
 24 //                            send      -- 发送使能端,低电平开始一个字符传输;
 25 //                            tx        -- 向uart device发送的串行数据;
 26 //                            busy          -- 线路状态标志,高时表示线路忙,低时表示线路空闲
 27 // Revision: 
 28 // Revision 0.01 - File Created
 29 // Additional Comments: 
 30 //
 31 //////////////////////////////////////////////////////////////////////////////////
 32 module UART_CTRL(
 33     clk,
 34     data,
 35     send,
 36     tx,
 37     busy
 38     );
 39 
 40 input clk,send;
 41 input [7:0] data;
 42 output reg tx,busy;
 43 
 44 //状态机状态定义
 45 parameter Idel = 2'b00,//空闲状态
 46              Rdy = 2'b01,//数据准备完成
 47              LoadByte = 2'b10,//数据传入
 48              SendBit = 2'b11;//数据发送
 49 
 50 reg [13:0] BspClkReg;//波特率分频计数
 51 reg BspClk;//波特率时钟
 52 
 53 reg [9:0] tx_data;//发送的数据,加上起始位和停止位
 54 reg [3:0] tx_byte_count;//发送位数计数
 55 
 56 reg [1:0] state;//状态寄存器
 57 
 58 
 59 //波特率分频模块,波特率:9600
 60 always@(posedge clk)
 61 begin
 62     BspClkReg <= BspClkReg + 1;
 63     if(BspClkReg == 5208)
 64     begin
 65         BspClkReg <= 0;
 66         BspClk <= ~BspClk;
 67     end
 68 end
 69 //串口发送模块
 70 always@(posedge BspClk)
 71 begin
 72     case(state)
 73         Idel         : begin
 74                         tx <= 1;
 75                         busy <= 0;
 76                         tx_byte_count <= 0;
 77                         if(send) state <= Rdy;
 78                         end
 79         Rdy          : begin
 80                         if(~send)    state <= Idel;
 81                         else begin
 82                             tx_byte_count <= 0;
 83                             tx <= 1;
 84                             busy <= 1;
 85                             state <= LoadByte; 
 86                             end
 87                         end
 88         LoadByte    : begin
 89                         if(~send)    state <= Idel;
 90                         else begin
 91                             tx_data <= {1'b1,data,1'b0};
 92                             tx <= 1;
 93                             busy <= 1;
 94                             state <= SendBit;
 95                             end
 96                         end
 97         SendBit    : begin
 98                         if(~send)    state <= Idel;
 99                         else begin
100                             tx <= tx_data[0];
101                             busy <= 1;
102                             tx_data <= tx_data >> 1;
103                             tx_byte_count <= tx_byte_count + 1;
104                             if(tx_byte_count == 9)
105                                 state <= Idel;
106                             else
107                                 state <= SendBit;
108                             end
109                         end
110     endcase
111 end
112 
113 endmodule
View Code

相关文章:

  • 2021-04-03
  • 2021-07-10
  • 2022-12-23
  • 2021-06-20
  • 2022-03-04
猜你喜欢
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2022-01-01
  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案