{"id":10745,"date":"2025-11-21T00:54:12","date_gmt":"2025-11-20T16:54:12","guid":{"rendered":"http:\/\/www.hellofpga.com\/?p=10745"},"modified":"2025-11-25T16:05:14","modified_gmt":"2025-11-25T08:05:14","slug":"uart_test-3","status":"publish","type":"post","link":"http:\/\/www.hellofpga.com\/index.php\/2025\/11\/21\/uart_test-3\/","title":{"rendered":"\u57fa\u4e8eSmart Artix \u7684FPGA\u5b9e\u9a8c\u5341\u4e94 \u57fa\u4e8eFPGA\u7684 UART\u4e32\u53e3\u901a\u8baf\u5b9e\u9a8c"},"content":{"rendered":"\n<p>\u672c\u6587\u5c06\u7b80\u5355\u4ecb\u7ecdUART\u534f\u8bae\uff0c\u5e76\u7528verilog\u4ee3\u7801\u53bb\u5b9e\u73b0\u4e00\u4e2aUART\u7684\u529f\u80fd\u6a21\u5757\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6b64\u7ae0\u8282\u5185\u5bb9\u9002\u7528\u4e8eSmart Artix \u7684\u4e3b\u677f \uff0c\u5982\u662f\u672c\u7ad9\u5176\u4ed6\u677f\u5b50\u8bf7\u770b\u5bf9\u5e94\u677f\u5b50\u76ee\u5f55<\/strong><\/li>\n\n\n\n<li><strong>\u672c\u6587\u5728 vivado2018.3\u7248\u672c\u4e0a\u6f14\u793a<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u5173\u4e8eUART<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4ec0\u4e48\u662fUART<\/strong><\/h3>\n\n\n\n<p>UART\u662f\u4e00\u79cd\u4e32\u884c\u901a\u4fe1\u534f\u8bae\uff0c\u7528\u4e8e\u5728\u8bbe\u5907\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u8fde\u63a5\u8ba1\u7b97\u673a\u4e0e\u5916\u90e8\u8bbe\u5907\uff08\u6bd4\u5982\u4f20\u611f\u5668\u3001\u663e\u793a\u5668\u3001\u6a21\u5757\u7b49\uff09\u3002<\/p>\n\n\n\n<p>UART\u4e0d\u50cf\u5176\u4ed6\u534f\u8bae\uff08\u6bd4\u5982SPI\u6216I2C\uff09\u9700\u8981\u540c\u6b65\u65f6\u949f\u4fe1\u53f7\uff0c\u56e0\u6b64\u88ab\u79f0\u4e3a\u5f02\u6b65\u534f\u8bae\u3002\u8fd9\u610f\u5473\u7740\u53d1\u9001\u548c\u63a5\u6536\u8bbe\u5907\u4e0d\u9700\u8981\u5171\u4eab\u540c\u6b65\u65f6\u949f\u3002\u6bcf\u4e2a\u8bbe\u5907\u6709\u81ea\u5df1\u7684\u65f6\u949f\uff0c\u5e76\u4e14\u901a\u8fc7\u8d77\u59cb\u4f4d\u6765\u540c\u6b65\u6570\u636e\u4f20\u8f93\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>UART\u7684\u534f\u8bae\u6784\u6210\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8d77\u59cb\u4f4d\uff08Start Bit\uff09<\/strong>\uff1a\u6307\u793a\u6570\u636e\u4f20\u8f93\u5f00\u59cb\u7684\u4f4d\u3002\u5b83\u662f\u903b\u8f91\u4f4e\u7535\u5e73\uff080\uff09\uff08\u603b\u7ebf\u7a7a\u95f2\u65f6\u4e3a\u9ad8\u7535\u5e73\uff0c\u6240\u4ee5\u5f00\u59cb\u901a\u4fe1\u65f6\u5148\u53d1\u9001\u4e00\u4e2a\u533a\u522b\u4e8e\u7a7a\u95f2\u72b6\u6001\u7684\u4fe1\u53f7\uff0c\u8d77\u59cb\u4f4d\u4e5f\u4f5c\u65f6\u949f\u540c\u6b65\u7528\uff09<\/li>\n\n\n\n<li><strong>\u6570\u636e\u4f4d\uff08Data Bits\uff09<\/strong>\uff1a\u5305\u542b\u8981\u4f20\u8f93\u7684\u6570\u636e\uff0c\u901a\u5e38\u4e3a8\u4f4d\uff0c\u5148\u53d1\u9001\u6570\u636e\u7684\u6700\u4f4e\u4f4d\uff0c\u6700\u540e\u53d1\u9001\u6570\u636e\u7684\u6700\u9ad8\u4f4d\uff0c<\/li>\n\n\n\n<li><strong>\u5947\u5076\u6821\u9a8c\u4f4d\uff08Parity Bit\uff09<\/strong>\uff08\u53ef\u9009\uff09\uff1a\u7528\u4e8e\u6570\u636e\u6821\u9a8c\uff0c\u6709\u5947\u5076\u4e24\u79cd\u7c7b\u578b\uff0c\u6709\u65f6\u7528\u4e8e\u68c0\u6d4b\u4f20\u8f93\u4e2d\u7684\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u505c\u6b62\u4f4d\uff08Stop Bit\uff09<\/strong>\uff1a\u6807\u5fd7\u6570\u636e\u4f20\u8f93\u7ed3\u675f\u7684\u4f4d\uff0c\u901a\u5e38\u662f\u903b\u8f91\u9ad8\u7535\u5e73\uff081\uff09<\/li>\n\n\n\n<li><strong>\u7a7a\u95f2\u65f6\u6bb5<\/strong>\uff1aUART\u534f\u8bae\u89c4\u5b9a\uff0c\u5f53\u603b\u7ebf\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\u65f6\u4fe1\u53f7\u7ebf\u7684\u72b6\u6001\u4e3a\uff081\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u4e5f\u5c31\u662f\u5982\u679c\u6211\u4eec\u89c2\u5bdfUART\u6ce2\u5f62\u53ef\u4ee5\u770b\u5230\u5728\u6ca1\u6709\u901a\u8baf\u7684\u65f6\u5019\uff0cUART\u4fe1\u53f7\u7ebf\u662f\u662f\u4fdd\u6301\u9ad8\u7535\u5e73\uff08\u5373\u7a7a\u95f2\u72b6\u6001\uff09\uff0c\u5f53\u4e00\u4e2a\u4e0b\u964d\u6cbf\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u6211\u4eec\u8ba4\u4e3a\u5c06\u8fdb\u884c\u4e00\u6b21\u6570\u636e\u7684\u4f20\u8f93\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"556\" src=\"http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-9-1024x556.png\" alt=\"\" class=\"wp-image-9665\" srcset=\"http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-9-1024x556.png 1024w, http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-9-300x163.png 300w, http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-9-768x417.png 768w, http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-9.png 1083w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u4e24\u4e2a\u8bbe\u5907\u8981\u7528UART\u8fdb\u884c\u901a\u8baf\uff0cTX\u548cRX\u4e24\u4e2a\u4fe1\u53f7\u7ebf\u662f\u9700\u8981\u4ea4\u53c9\u7684\uff0c\u9664\u4e86TX\u548cRX\u4fe1\u53f7\u5916\uff0c\u4e24\u4e2a\u8bbe\u5907\u95f4\u8fd8\u9700\u8981\u5171\u5730(GND)\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"282\" src=\"http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-10-1024x282.png\" alt=\"\" class=\"wp-image-9667\" srcset=\"http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-10-1024x282.png 1024w, http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-10-300x83.png 300w, http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-10-768x211.png 768w, http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-10.png 1231w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>UART \u901a\u8baf\u7535\u5e73<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UART\u7684\u7535\u5e73\uff0c\u5176\u5b9e\u662f\u6709\u4e24\u79cd\u89c4\u683c\u7684\uff0c\u4e24\u8005\u7535\u538b\u4e0d\u540c\uff0c\u903b\u8f91\u4e5f\u76f8\u53cd\uff0c\u4e0d\u80fd\u76f4\u63a5\u8fde\u63a5<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>RS232&nbsp;<\/strong>( \u00b112V, \u5176\u4e2d+12V\u4ee3\u8868\u903b\u8f911\uff0c-12V\u4ee3\u8868\u903b\u8f910) \u591a\u7528\u4e8e\u8001\u5f0f\u7535\u8111\u4e32\u53e3\u548c\u5de5\u4e1a\u8bbe\u5907\u7684\u901a\u8baf<\/li>\n\n\n\n<li><strong>TTL232<\/strong>\uff083.3V\/5V\u4ee3\u8868\u903b\u8f911\uff0c 0V\u4ee3\u8868\u903b\u8f910\uff09\u5e38\u7528\u4e8e\u5355\u7247\u673a\u3001\u5f00\u53d1\u677f\u7b49\u5d4c\u5165\u5f0f\u7cfb\u7edf<\/li>\n\n\n\n<li>RS232\u548cTTL232\u53ef\u901a\u8fc7\u4e13\u7528\u82af\u7247\u8fdb\u884c\u8f6c\u6362\uff0c\u5982\uff08MAX232\u82af\u7247\u7b49\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u5de5\u7a0b\u521b\u5efa\uff1a<\/strong><\/h2>\n\n\n\n<p><strong>\u5de5\u7a0b\u521b\u5efa\u7684\u8fc7\u7a0b\u53ef\u4ee5\u53c2\u8003\u5b9e\u9a8c\u4e00\u4e2d\u7684\u5185\u5bb9\uff0c\u8fd9\u91cc\u4e0d\u8be6\u7ec6\u63cf\u8ff0\u4e86\u3002<a href=\"http:\/\/www.hellofpga.com\/index.php\/2023\/04\/27\/smart-zynq_sp_sl_01_led\/\"><\/a><a href=\"http:\/\/www.hellofpga.com\/index.php\/2025\/11\/14\/led_test\/\">\u57fa\u4e8eSmart Artix \u7684FPGA\u5b9e\u9a8c\u4e00 \u7528FPGA\u8d44\u6e90\u70b9\u4eae\u4e00\u4e2aLED\uff08\u5b8c\u6574\u56fe\u6587\uff09<\/a>&nbsp;(\u82af\u7247\u578b\u53f7\u9009XC7A50TFGG484-2)<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5b9e\u9a8c\u5185\u5bb9\uff1a<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u672c\u6b21\u5b9e\u9a8c\u6211\u4eec\u5c06\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684UART\u6a21\u5757\uff0c\u5177\u4f53\u529f\u80fd\u5982\u4e0b\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u4e0a\u7535\u540e\uff0c\u4e32\u53e3\u81ea\u52a8\u53d1\u9001\u5b57\u7b26\u4e32\uff1a<strong><code>hellofpga.com<\/code><\/strong><\/li>\n\n\n\n<li>\u53d1\u9001\u5b8c\u6210\u540e\uff0c\u8fdb\u5165<strong>\u56de\u73af\u6a21\u5f0f<\/strong>\uff1a\u4e32\u53e3\u63a5\u6536\u7684\u6570\u636e\u76f4\u63a5\u539f\u6837\u8fd4\u56de\u53d1\u9001\uff08Loopback\uff09<\/li>\n\n\n\n<li>\u4e32\u53e3\u6a21\u5757\u7cfb\u7edf\u65f6\u949f50M\uff0c\u6ce2\u7279\u7387115200\uff0c8 \u4f4d\u6570\u636e\uff0c\u65e0\u6821\u9a8c\uff0c1 \u4f4d\u505c\u6b62\u4f4d\uff088N1\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5de5\u7a0b\u5c06\u5206\u4e3a3\u4e2a\u6a21\u5757\u6765\u8fdb\u884c\u8bbe\u8ba1\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>\u9876\u5c42\u6a21\u5757&nbsp;<code>uart_top.v<\/code><\/strong>\uff08\u521d\u59cb\u5316\u65f6\u53d1\u9001\u5b57\u7b26\u4e32&nbsp;<code>\"hellofpga.com\"<\/code>\uff0c\u53d1\u9001\u5b8c\u6210\u540e\u8fdb\u5165\u56de\u73af\u6a21\u5f0f\uff09<\/li>\n\n\n\n<li><strong>\u4e32\u53e3\u53d1\u9001\u6a21\u5757&nbsp;<code>uart_tx.v<\/code><\/strong>\uff08\u5c06\u8f93\u5165\u7684 8 \u4f4d\u6570\u636e\u4ee5 UART \u683c\u5f0f\uff081 \u8d77\u59cb\u4f4d + 8 \u6570\u636e\u4f4d + 1 \u505c\u6b62\u4f4d\uff09\u4e32\u884c\u53d1\u9001\uff09<\/li>\n\n\n\n<li><strong>\u4e32\u53e3\u63a5\u6536\u6a21\u5757&nbsp;<code>uart_rx.v<\/code><\/strong>\uff08\u5bf9\u8f93\u5165\u7684 RX \u4fe1\u53f7\u91c7\u6837\uff0c\u8bc6\u522b\u8d77\u59cb\u4f4d\uff0c\u63d0\u53d6 8 \u4f4d\u6570\u636e\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>a) \u521b\u5efa\u4e00\u4e2a\u9876\u5c42\u6a21\u5757\uff08<code>uart_top.v<\/code>\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">`timescale 1ns \/ 1ps<br>module uart_top(<br>    input wire clk,<br>    input wire rst_n,<br>    input wire rx, <br>    output wire tx <br>);<br><br>    localparam INIT    = 2'd0;<br>    localparam SENDING = 2'd1;<br>    localparam LOOP    = 2'd2;<br><br>    reg [1:0] state = INIT;<br>    reg start_tx = 0;<br>    reg [7:0] tx_data;<br>    wire tx_busy;<br>    wire [7:0] rx_data;<br>    wire rx_done;<br><br>    reg [3:0] send_index = 0;<br>    reg [7:0] send_buf [12:0];<br><br>    reg [7:0] tx_buf;<br>    reg tx_req;<br><br>    initial begin<br>        send_buf[0]  = \"h\";<br>        send_buf[1]  = \"e\";<br>        send_buf[2]  = \"l\";<br>        send_buf[3]  = \"l\";<br>        send_buf[4]  = \"o\";<br>        send_buf[5]  = \"f\";<br>        send_buf[6]  = \"p\";<br>        send_buf[7]  = \"g\";<br>        send_buf[8]  = \"a\";<br>        send_buf[9]  = \".\";<br>        send_buf[10] = \"c\";<br>        send_buf[11] = \"o\";<br>        send_buf[12] = \"m\";<br>    end<br><br>    always @(posedge clk or negedge rst_n) begin<br>        if (!rst_n) begin<br>            state &lt;= INIT;<br>            send_index &lt;= 0;<br>            start_tx &lt;= 0;<br>            tx_req &lt;= 0;<br>        end else begin<br>            case (state)<br>                INIT: begin<br>                    state &lt;= SENDING;<br>                    send_index &lt;= 0;<br>                end<br><br>                SENDING: begin<br>                    if (!tx_busy &amp;&amp; !start_tx &amp;&amp; send_index &lt; 13) begin<br>                        tx_data &lt;= send_buf[send_index];<br>                        start_tx &lt;= 1;<br>                        send_index &lt;= send_index + 1;<br>                    end else if (start_tx &amp;&amp; tx_busy) begin<br>                        start_tx &lt;= 0;<br>                    end else if (send_index &gt;= 13 &amp;&amp; !tx_busy) begin<br>                        state &lt;= LOOP;<br>                    end<br>                end<br><br>                LOOP: begin<br>                    if (rx_done &amp;&amp; !tx_req) begin<br>                        tx_buf &lt;= rx_data;<br>                        tx_req &lt;= 1;<br>                    end<br><br>                    if (tx_req &amp;&amp; !tx_busy &amp;&amp; !start_tx) begin<br>                        tx_data &lt;= tx_buf;<br>                        start_tx &lt;= 1;<br>                        tx_req &lt;= 0;<br>                    end else if (start_tx &amp;&amp; tx_busy) begin<br>                        start_tx &lt;= 0;<br>                    end<br>                end<br>            endcase<br>        end<br>    end<br><br>    uart_tx #(<br>        .CLK_FREQ(50000000),<br>        .BAUD_RATE(115200)<br>    ) u_tx (<br>        .clk(clk),<br>        .rst_n(rst_n),<br>        .tx_data(tx_data),<br>        .tx_start(start_tx),<br>        .tx(tx),<br>        .tx_busy(tx_busy)<br>    );<br><br>    uart_rx #(<br>        .CLK_FREQ(50000000),<br>        .BAUD_RATE(115200)<br>    ) u_rx (<br>        .clk(clk),<br>        .rst_n(rst_n),<br>        .rx(rx),<br>        .rx_data(rx_data),<br>        .rx_done(rx_done)<br>    );<br><br>endmodule<\/pre>\n\n\n\n<p>UART \u9876\u5c42\u6a21\u5757\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\uff0c\u4f1a\u5148\u901a\u8fc7\u4e32\u53e3\u53d1\u9001\u4e00\u4e32\u5b57\u7b26\u4e32 \u201chellofpga.com\u201d\uff0c\u53d1\u9001\u5b8c\u6210\u540e\u8fdb\u5165\u56de\u73af\u6a21\u5f0f\u3002\u6240\u8c13\u56de\u73af\u6a21\u5f0f\uff0c\u5c31\u662f\u6536\u5230\u4ec0\u4e48\u6570\u636e\uff0c\u5c31\u7acb\u5373\u4ece TX \u53d1\u56de\u53bb\uff0c\u5b9e\u73b0 UART \u7684\u6570\u636e\u56de\u9001\u529f\u80fd\uff0c\u65b9\u4fbf\u6d4b\u8bd5\u4e32\u53e3\u662f\u5426\u6b63\u5e38\u3002<\/p>\n\n\n\n<p>\u6574\u4e2a\u6a21\u5757\u5206\u4e3a\u4e09\u4e2a\u72b6\u6001\uff1a\u521d\u59cb\u5316\uff08INIT\uff09\u3001\u53d1\u9001\u56fa\u5b9a\u5b57\u7b26\u4e32\uff08SENDING\uff09\u548c\u56de\u73af\uff08LOOP\uff09\u3002\u4e3a\u907f\u514d\u53d1\u9001\u51b2\u7a81\uff0c\u56de\u73af\u90e8\u5206\u52a0\u5165\u4e86\u4e00\u4e2a\u53d1\u9001\u8bf7\u6c42\u6807\u5fd7&nbsp;<code>tx_req<\/code>\uff0c\u786e\u4fdd\u63a5\u6536\u6570\u636e\u540e\u4e0d\u4f1a\u4e22\u5305\u3002\u53d1\u9001\u548c\u63a5\u6536\u7684\u6ce2\u7279\u7387\u3001\u65f6\u949f\u9891\u7387\u90fd\u53ef\u901a\u8fc7\u53c2\u6570\u914d\u7f6e\uff0c\u4fbf\u4e8e\u7075\u6d3b\u4fee\u6539\u3002\uff08<code>tx_req<\/code>&nbsp;\u662f\u4e00\u4e2a&nbsp;<strong>\u201c\u53d1\u9001\u51c6\u5907\u6807\u5fd7\u201d<\/strong>\uff0c\u8868\u793a\u5df2\u7ecf\u6536\u5230\u6570\u636e\u4f46\u8fd8\u6ca1\u53d1\u9001\u51fa\u53bb\uff0c\u7b49\u5230\u53d1\u9001\u6a21\u5757\u7a7a\u95f2\u65f6\u518d\u542f\u52a8\u53d1\u9001\uff0c\u9632\u6b62\u6570\u636e\u51b2\u7a81\u6216\u9057\u6f0f\uff09<\/p>\n\n\n\n<p><strong>b) \u521b\u5efa\u4e32\u53e3\u53d1\u9001\u6a21\u5757 (<code>uart_tx.v<\/code><\/strong>)<\/p>\n\n\n\n<p>\u529f\u80fd\uff1a\u5c06\u8f93\u5165\u7684 8 \u4f4d\u6570\u636e\u4ee5 UART \u683c\u5f0f\uff081 \u8d77\u59cb\u4f4d + 8 \u6570\u636e\u4f4d + 1 \u505c\u6b62\u4f4d\uff09\u4e32\u884c\u53d1\u9001\uff0c\uff08<code>tx_start<\/code>&nbsp;\u62c9\u9ad8\u5f00\u59cb\u53d1\u9001\uff0c\u53d1\u9001\u671f\u95f4<code>tx_busy<\/code>&nbsp;\u4e3a\u9ad8\u8868\u793a\u5f53\u524d\u6b63\u5728\u53d1\u9001\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">`timescale 1ns \/ 1ps<br>module uart_tx #(<br>    parameter CLK_FREQ = 50000000,<br>    parameter BAUD_RATE = 115200<br>)(<br>    input wire clk,<br>    input wire rst_n,<br>    input wire [7:0] tx_data,<br>    input wire tx_start,<br>    output reg tx,<br>    output reg tx_busy<br>);<br><br>    localparam BAUD_TICK = CLK_FREQ \/ BAUD_RATE;<br><br>    reg [15:0] baud_cnt = 0;<br>    reg [3:0] bit_idx = 0;<br>    reg [9:0] tx_shift = 10'b1111111111;<br><br>    always @(posedge clk or negedge rst_n) begin<br>        if (!rst_n) begin<br>            tx &lt;= 1;<br>            tx_busy &lt;= 0;<br>            baud_cnt &lt;= 0;<br>            bit_idx &lt;= 0;<br>            tx_shift &lt;= 10'b1111111111;<br>        end <br>        else begin<br>            if (tx_start &amp;&amp; !tx_busy) begin<br>                tx_shift &lt;= {1'b1, tx_data, 1'b0};<br>                tx_busy &lt;= 1;<br>                baud_cnt &lt;= 0;<br>                bit_idx &lt;= 0;<br>            end <br>            else if (tx_busy) begin<br>                if (baud_cnt == BAUD_TICK - 1) begin<br>                    baud_cnt &lt;= 0;<br>                    tx &lt;= tx_shift[bit_idx];<br>                    bit_idx &lt;= bit_idx + 1;<br><br>                    if (bit_idx == 9) begin<br>                        tx_busy &lt;= 0;<br>                        tx &lt;= 1;<br>                    end<br>                end <br>                else begin<br>                    baud_cnt &lt;= baud_cnt + 1;<br>                end<br>            end<br>        end<br>    end<br>endmodule<\/pre>\n\n\n\n<p>\u5f53&nbsp;<code>tx_start<\/code>&nbsp;\u4fe1\u53f7\u4e3a\u9ad8\u5e76\u4e14\u5f53\u524d\u53d1\u9001\u5668\u7a7a\u95f2\u65f6\uff0c\u6a21\u5757\u5c06\u6570\u636e\u6253\u5305\u6210 10 \u4f4d\u53d1\u9001\u5e27\uff08\u8d77\u59cb\u4f4d + \u6570\u636e + \u505c\u6b62\u4f4d\uff09\uff0c\u5e76\u5f00\u59cb\u4ee5\u56fa\u5b9a\u6ce2\u7279\u7387\uff08\u7531&nbsp;<code>CLK_FREQ<\/code>&nbsp;\u4e0e&nbsp;<code>BAUD_RATE<\/code>&nbsp;\u53c2\u6570\u51b3\u5b9a\uff09\u9010\u4f4d\u8f93\u51fa\u5230&nbsp;<code>tx<\/code>\u3002\u53d1\u9001\u8fc7\u7a0b\u4e2d\uff0c<code>tx_busy<\/code>&nbsp;\u4f1a\u4fdd\u6301\u9ad8\u7535\u5e73\uff08\u8868\u793a\u53d1\u9001\u5668\u6b63\u5fd9\uff09\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u5b8c\u6210\u53d1\u9001\u3002<\/p>\n\n\n\n<p>\u6a21\u5757\u901a\u8fc7\u53c2\u6570&nbsp;<code>CLK_FREQ<\/code>&nbsp;\u548c&nbsp;<code>BAUD_RATE<\/code>&nbsp;\u63a7\u5236\u6ce2\u7279\u7387\uff0c\u5185\u90e8\u8ba1\u7b97\u6bcf\u4e2a\u4f4d\u7684\u53d1\u9001\u5468\u671f&nbsp;<code>BAUD_TICK = CLK_FREQ \/ BAUD_RATE<\/code>\u3002<strong>\u53ef\u5728\u4f8b\u5316\u6a21\u5757\u65f6\u4fee\u6539\u8fd9\u4e24\u4e2a\u53c2\u6570\u4ee5\u9002\u914d\u4e0d\u540c\u7684\u7cfb\u7edf\u65f6\u949f\u548c\u4e32\u53e3\u6ce2\u7279\u7387\u3002<\/strong><\/p>\n\n\n\n<p><strong>c) \u521b\u5efa\u4e32\u53e3\u63a5\u6536\u6a21\u5757(&nbsp;<code>uart_rx.v<\/code><\/strong>&nbsp;\uff09<\/p>\n\n\n\n<p>\u529f\u80fd\uff1a\u5bf9\u8f93\u5165\u7684 rx \u4fe1\u53f7\u91c7\u6837\uff0c\u8bc6\u522b\u8d77\u59cb\u4f4d\uff0c\u63d0\u53d6 8 \u4f4d\u6570\u636e\uff0c\u7136\u540e\u8f93\u51fa\u8fd9\u4e2a\u63d0\u53d6\u5230\u76848\u4f4d\u6570\u636e\uff08<code>rx_data<\/code>\uff09\uff0c\u5e76\u62c9\u9ad8<code>rx_done<\/code>\u4fe1\u53f7\uff08\u4e00\u4e2a\u5468\u671f\uff09\u4ee5\u8868\u793a\u63a5\u6536\u5b8c\u6210\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">module uart_rx #(<br>    parameter CLK_FREQ   = 50000000,<br>    parameter BAUD_RATE  = 115200<br>)(<br>    input wire clk,<br>    input wire rst_n,<br>    input wire rx,<br>    output reg [7:0] rx_data,<br>    output reg rx_done<br>);<br><br>    localparam BAUD_TICK = CLK_FREQ \/ BAUD_RATE;<br>    localparam HALF_BAUD = BAUD_TICK \/ 2;<br><br>    reg [15:0] baud_cnt = 0;<br>    reg [3:0]  bit_cnt = 0;<br>    reg [7:0]  rx_shift = 0;<br>    reg [1:0]  rx_sync = 2'b11;<br>    reg        rx_busy = 0;<br>    reg        start_edge = 0;<br><br>    always @(posedge clk) begin<br>        rx_sync &lt;= {rx_sync[0], rx};<br>    end<br><br>    always @(posedge clk or negedge rst_n) begin<br>        if (!rst_n)<br>            start_edge &lt;= 0;<br>        else<br>            start_edge &lt;= (!rx_busy &amp;&amp; rx_sync == 2'b10);<br>    end<br><br>    always @(posedge clk or negedge rst_n) begin<br>        if (!rst_n) begin<br>            rx_busy   &lt;= 0;<br>            rx_done   &lt;= 0;<br>            baud_cnt  &lt;= 0;<br>            bit_cnt   &lt;= 0;<br>            rx_shift  &lt;= 0;<br>            rx_data   &lt;= 0;<br>        end <br>        else begin<br>            rx_done &lt;= 0;<br><br>            if (start_edge) begin<br>                rx_busy   &lt;= 1;<br>                baud_cnt  &lt;= 0;<br>                bit_cnt   &lt;= 0;<br>            end<br><br>            if (rx_busy) begin<br>                baud_cnt &lt;= baud_cnt + 1;<br><br>                if ((bit_cnt == 0 &amp;&amp; baud_cnt == HALF_BAUD) || (bit_cnt &gt; 0 &amp;&amp; baud_cnt == BAUD_TICK)) begin<br>                    baud_cnt &lt;= 0;<br>                    bit_cnt &lt;= bit_cnt + 1;<br><br>                    if (bit_cnt &gt;= 1 &amp;&amp; bit_cnt &lt;= 8) begin<br>                        rx_shift[bit_cnt - 1] &lt;= rx_sync[1];<br>                    end<br><br>                    if (bit_cnt == 9) begin<br>                        rx_data &lt;= rx_shift;<br>                        rx_done &lt;= 1;<br>                        rx_busy &lt;= 0;<br>                    end<br>                end<br>            end<br>        end<br>    end<br><br>endmodule<\/pre>\n\n\n\n<p>\u7531\u4e8e&nbsp;<code>rx<\/code>&nbsp;\u5bf9\u4e8e\u7cfb\u7edf\u6765\u8bf4\u662f\u5f02\u6b65\u4fe1\u53f7\uff0c\u6240\u4ee5\u8fd9\u91cc\u9996\u5148\u5bf9\u8f93\u5165\u4fe1\u53f7&nbsp;<code>rx<\/code>&nbsp;\u8fdb\u884c\u540c\u6b65\u3002\u4f7f\u7528\u4e86&nbsp;<code>rx_sync<\/code>&nbsp;\u5bc4\u5b58\u5668\u8fdb\u884c\u540c\u6b65\uff0c\u4ee5\u6d88\u9664\u65f6\u949f\u57df\u4ea4\u53c9\u53ef\u80fd\u5e26\u6765\u7684\u95ee\u9898\u3002\u901a\u8fc7&nbsp;<code>start_edge<\/code>&nbsp;\u4fe1\u53f7\u68c0\u6d4b UART \u6570\u636e\u7684\u8d77\u59cb\u8fb9\u6cbf\uff08\u4e0b\u964d\u6cbf\uff09\uff0c\u4ee5\u786e\u5b9a\u63a5\u6536\u6570\u636e\u7684\u5f00\u59cb\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u5f53\u6570\u636e\u5f00\u59cb\u63a5\u6536\u540e\uff0c\u6a21\u5757\u4f1a\u6839\u636e\u6ce2\u7279\u7387\u8bbe\u7f6e\u7684\u65f6\u95f4\u95f4\u9694\uff08\u901a\u8fc7&nbsp;<code>baud_cnt<\/code>&nbsp;\u8ba1\u6570\u5668\uff09\u6765\u6309\u4f4d\u63a5\u6536\u6570\u636e\u3002\u6bcf\u5f53\u63a5\u6536\u5230\u4e00\u4f4d\u6570\u636e\uff0c\u5c31\u4f1a\u5c06\u6570\u636e\u5b58\u50a8\u5230\u4e00\u4e2a\u5bc4\u5b58\u5668&nbsp;<code>rx_shift<\/code>&nbsp;\u4e2d\uff0c\u76f4\u5230\u63a5\u6536\u5230 8 \u4f4d\u6570\u636e\uff08\u5373\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u8282\uff09\u3002\u4e00\u65e6\u63a5\u6536\u5b8c\u4e00\u4e2a\u5b57\u8282\uff0c\u5c31\u5c06\u8fd9\u4e2a\u5b57\u8282\u8f93\u51fa\u5230&nbsp;<code>rx_data<\/code>\uff0c\u5e76\u901a\u8fc7&nbsp;<code>rx_done<\/code>&nbsp;\u4fe1\u53f7\u544a\u77e5\u5916\u90e8\uff0c\u6570\u636e\u5df2\u7ecf\u51c6\u5907\u597d\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\u6570\u636e\u7684\u91c7\u6837\u662f\u5728\u6570\u636e\u7684\u4e2d\u5fc3\u70b9\u4f4d\u7f6e\u8fdb\u884c\u91c7\u6837\u7684\uff08\u5f53&nbsp;<code>baud_cnt<\/code>&nbsp;\u8ba1\u6570\u5230<code>HALF_BAUD<\/code>\u65f6\u8fdb\u884c\u91c7\u6837\uff0c\u5373\u6355\u6349\u6570\u636e\u4f4d\u7684\u4e2d\u5fc3\u70b9\uff09\u786e\u4fdd\u6570\u636e\u5728\u6700\u7a33\u5b9a\u7684\u65f6\u523b\u88ab\u91c7\u6837\uff0c\u4ee5\u4fdd\u8bc1\u4e86\u6570\u636e\u63a5\u6536\u7684\u53ef\u9760\u6027\u548c\u7cbe\u5ea6\u3002<\/p>\n\n\n\n<p><strong>d\uff09\u6700\u540e\u518d\u589e\u52a0\u7ea6\u675f\u6587\u4ef6\uff0c\u5bf9\u7ba1\u811a\u8fdb\u884c\u5b9a\u4e49 \uff08PIN.XDC\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Clock Constraints<br>set_property PACKAGE_PIN Y18 [get_ports clk]<br>set_property IOSTANDARD LVCMOS33 [get_ports clk]<br><br># Define the clock period for 50 MHz<br>create_clock -period 20.000 -name CLK -waveform {0.000 10.000} [get_ports CLK]<br><br># Reset Constraints (active-low reset)<br>set_property PACKAGE_PIN T20 [get_ports rst_n]<br>set_property IOSTANDARD LVCMOS33 [get_ports rst_n]<br><br># UART Constraints<br>set_property PACKAGE_PIN P14 [get_ports rx]<br>set_property PACKAGE_PIN R14 [get_ports tx]<br>set_property IOSTANDARD LVCMOS33 [get_ports rx]<br>set_property IOSTANDARD LVCMOS33 [get_ports tx]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001\u7f16\u8bd1\u7efc\u5408\uff0c\u5e76\u8fd0\u884c\u4ee3\u7801<\/strong><\/h2>\n\n\n\n<p>1\uff09\u5c06\u4e3b\u677f\u7684UART\u53e3\uff08TYPE C\u53e3\uff09\u7528\u6570\u636e\u7ebf\u4e0e\u7535\u8111\u8fdb\u884c\u8fde\u63a5 \u3002\uff08Smart Artix \u4e3b\u677f\u4e0b\u8f7d\u5668\u4e0eUART\u5171\u7528\u4e00\u4e2aTYPE C\u53e3\uff092\uff09\u5728\u7535\u8111\u7aef\u63d0\u524d\u6253\u5f00\u4e32\u53e3\u52a9\u624b\uff0c\u6ce2\u7279\u7387\u8bbe\u7f6e\u6210115200\uff0c\u5e76\u5f00\u542f\u4e32\u53e3\u529f\u80fd\u3002 3\uff09\u5728Vivado \u4e0a\u5bf9FPGA\u82af\u7247\u8fdb\u884cProgram\u3002<\/p>\n\n\n\n<p>\u6210\u529f\u540e\u53ef\u4ee5\u4ece\u4e32\u53e3\u52a9\u624b\u4e0a\u770b\u5230hellofpga.com\u7684\u5b57\u7b26\u5185\u5bb9\uff0c\u4e4b\u540e\u4e3b\u677f\u7cfb\u7edf\u8fdb\u5165\u56de\u73af\u6a21\u5f0f\uff0c \u56de\u73af\u6a21\u5f0f\u4e0b\u6211\u4eec\u901a\u8fc7\u4e32\u53e3\u52a9\u624b\u5411\u4e3b\u677f\u53d1\u9001\u4efb\u4f55\u5185\u5bb9\uff0c \u4e3b\u677f\u90fd\u4f1a\u5c06\u89e3\u6790\u5230\u7684\u6570\u636e\u8fdb\u884c\u56de\u4f20\u5e76\u663e\u793a\u5728\u4e32\u53e3\u52a9\u624b\u7684\u63a5\u6536\u9875\u9762\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u8bc1\u660e\u6211\u4eec\u7684\u4e32\u53e3\u6a21\u5757\u5de5\u4f5c\u6210\u529f\u3002<\/p>\n\n\n\n<p><strong>\u5907\u6ce8: \u7531\u4e8eFT\u82af\u7247\u4ef7\u683c\u6ce2\u52a8\u5389\u5bb3\uff0c\u6240\u4ee5\u4e3b\u677f\u5728\u8bbe\u8ba1\u4e0a\u517c\u5bb9\u4e86FT4232HL\u548cFT2232HL\u4e24\u4e2aJTAG\u82af\u7247\u578b\u53f7\uff0c\u5982\u679c\u4e3b\u677f\u662f\u8d34FT4232HL\uff0c\u90a3\u4e48\u63a5\u7535\u8111\u4e0a\u4f1a\u51fa\u73b03\u4e2a\u4e32\u53e3\uff08\u90fd\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\uff09\uff0c\u5982\u679c\u4e3b\u677f\u4e0a\u8d34\u7684\u662fFT2232HL\uff0c\u90a3\u4e48\u4e32\u53e3\u53ea\u6709\u4e00\u8def\uff08\u9996\u6279\u4e3b\u677f\u8d34\u7684\u662fFT4232HL\uff09<\/strong><\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-10d10b813090bda6b290ced4663c9c56\"><strong>\u5982\u679c\u662fFT4232HL\u7684\u7248\u672c\uff0c\u5982\u679c\u4e32\u53e3\u6570\u636e\u6ca1\u6709\u6b63\u786e\u663e\u793a\uff0c\u8bf7\u5c1d\u8bd5\u5207\u63623\u4e2a\u4e32\u53e3\u4e2d\u7684\u53e6\u4e24\u4e2a\uff0c\u76f4\u5230\u6253\u5f00\u6b63\u786e\u7684\u4e32\u53e3\u7aef\u53f7\u4e3a\u6b62\u3002<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"778\" height=\"675\" src=\"http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-11.png\" alt=\"\" class=\"wp-image-9676\" srcset=\"http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-11.png 778w, http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-11-300x260.png 300w, http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/04\/image-11-768x666.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e0b\u5217\u662f\u672c\u6b21\u5b9e\u9a8c\u7684\u5b8c\u6574\u5de5\u7a0b\uff1a<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u672c\u6587\u7684\u5b8c\u6574\u5de5\u7a0b\u4e0b\u8f7d:  <a href=\"http:\/\/www.hellofpga.com\/wp-content\/uploads\/2025\/11\/15_UART_TEST.zip\">15_UART_TEST<\/a> \uff08Smart Artix 50T\uff09<\/strong><\/li>\n\n\n\n<li><strong>VIVADO\u7684\u7248\u672c\uff1a2018.3<\/strong><\/li>\n\n\n\n<li><strong>\u5de5\u7a0b\u521b\u5efa\u76ee\u5f55\uff1aE:\\Smart_Artix\\4_Code\\15_UART_TEST<\/strong><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c06\u7b80\u5355\u4ecb\u7ecdUART\u534f\u8bae\uff0c\u5e76\u7528verilog\u4ee3\u7801\u53bb\u5b9e\u73b0\u4e00\u4e2aUART\u7684\u529f\u80fd\u6a21\u5757<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-10745","post","type-post","status-publish","format-standard","hentry","category-smart-artix"],"views":839,"_links":{"self":[{"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/posts\/10745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/comments?post=10745"}],"version-history":[{"count":5,"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/posts\/10745\/revisions"}],"predecessor-version":[{"id":10763,"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/posts\/10745\/revisions\/10763"}],"wp:attachment":[{"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/media?parent=10745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/categories?post=10745"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hellofpga.com\/index.php\/wp-json\/wp\/v2\/tags?post=10745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}