⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart_v.htm

📁 VHDL串口程序,通过验证,识货的快下载,精品
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0048)http://www.asic-world.com/code/hdl_models/uart.v -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.3268" name=GENERATOR></HEAD>
<BODY><PRE>//-----------------------------------------------------
// Design Name : uart 
// File Name   : uart.v
// Function    : Simple UART
// Coder       : Deepak Kumar Tala
//-----------------------------------------------------
module uart (
reset          ,
txclk          ,
ld_tx_data     ,
tx_data        ,
tx_enable      ,
tx_out         ,
tx_empty       ,
rxclk          ,
uld_rx_data    ,
rx_data        ,
rx_enable      ,
rx_in          ,
rx_empty
);
// Port declarations
input        reset          ;
input        txclk          ;
input        ld_tx_data     ;
input  [7:0] tx_data        ;
input        tx_enable      ;
output       tx_out         ;
output       tx_empty       ;
input        rxclk          ;
input        uld_rx_data    ;
output [7:0] rx_data        ;
input        rx_enable      ;
input        rx_in          ;
output       rx_empty       ;

// Internal Variables 
reg [7:0]    tx_reg         ;
reg          tx_empty       ;
reg          tx_over_run    ;
reg [3:0]    tx_cnt         ;
reg          tx_out         ;
reg [7:0]    rx_reg         ;
reg [7:0]    rx_data        ;
reg [3:0]    rx_sample_cnt  ;
reg [3:0]    rx_cnt         ;  
reg          rx_frame_err   ;
reg          rx_over_run    ;
reg          rx_empty       ;
reg          rx_d1          ;
reg          rx_d2          ;
reg          rx_busy        ;

// UART RX Logic
always @ (posedge rxclk or posedge reset)
if (reset) begin
  rx_reg        &lt;= 0; 
  rx_data       &lt;= 0;
  rx_sample_cnt &lt;= 0;
  rx_cnt        &lt;= 0;
  rx_frame_err  &lt;= 0;
  rx_over_run   &lt;= 0;
  rx_empty      &lt;= 1;
  rx_d1         &lt;= 1;
  rx_d2         &lt;= 1;
  rx_busy       &lt;= 0;
end else begin
  // Synchronize the asynch signal
  rx_d1 &lt;= rx_in;
  rx_d2 &lt;= rx_d1;
  // Uload the rx data
  if (uld_rx_data) begin
    rx_data  &lt;= rx_reg;
    rx_empty &lt;= 1;
  end
  // Receive data only when rx is enabled
  if (rx_enable) begin
    // Check if just received start of frame
    if (!rx_busy &amp;&amp; !rx_d2) begin
      rx_busy       &lt;= 1;
      rx_sample_cnt &lt;= 1;
      rx_cnt        &lt;= 0;
    end
    // Start of frame detected, Proceed with rest of data
    if (rx_busy) begin
       rx_sample_cnt &lt;= rx_sample_cnt + 1;
       // Logic to sample at middle of data
       if (rx_sample_cnt == 7) begin
          if ((rx_d2 == 1) &amp;&amp; (rx_cnt == 0)) begin
            rx_busy &lt;= 0;
          end else begin
            rx_cnt &lt;= rx_cnt + 1; 
            // Start storing the rx data
            if (rx_cnt &gt; 0 &amp;&amp; rx_cnt &lt; 9) begin
              rx_reg[rx_cnt - 1] &lt;= rx_d2;
            end
            if (rx_cnt == 9) begin
               rx_busy &lt;= 0;
               // Check if End of frame received correctly
               if (rx_d2 == 0) begin
                 rx_frame_err &lt;= 1;
               end else begin
                 rx_empty     &lt;= 0;
                 rx_frame_err &lt;= 0;
                 // Check if last rx data was not unloaded,
                 rx_over_run  &lt;= (rx_empty) ? 0 : 1;
               end
            end
          end
       end 
    end 
  end
  if (!rx_enable) begin
    rx_busy &lt;= 0;
  end
end

// UART TX Logic
always @ (posedge txclk or posedge reset)
if (reset) begin
  tx_reg        &lt;= 0;
  tx_empty      &lt;= 1;
  tx_over_run   &lt;= 0;
  tx_out        &lt;= 1;
  tx_cnt        &lt;= 0;
end else begin
   if (ld_tx_data) begin
      if (!tx_empty) begin
        tx_over_run &lt;= 0;
      end else begin
        tx_reg   &lt;= tx_data;
        tx_empty &lt;= 0;
      end
   end
   if (tx_enable &amp;&amp; !tx_empty) begin
     tx_cnt &lt;= tx_cnt + 1;
     if (tx_cnt == 0) begin
       tx_out &lt;= 0;
     end
     if (tx_cnt &gt; 0 &amp;&amp; tx_cnt &lt; 9) begin
        tx_out &lt;= tx_reg[tx_cnt -1];
     end
     if (tx_cnt == 9) begin
       tx_out &lt;= 1;
       tx_cnt &lt;= 0;
       tx_empty &lt;= 1;
     end
   end
   if (!tx_enable) begin
     tx_cnt &lt;= 0;
   end
end

endmodule
</PRE></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -