FPGA——UART串口通信實現(xiàn)
掃描二維碼
隨時隨地手機看文章
一、RTL Viewer
UART(Universal Asynchronous Receiver/Transmitter)是一種通信協(xié)議,用于在電子設備之間傳輸數(shù)據(jù)。它是一種串行通信協(xié)議,意味著數(shù)據(jù)位按順序一個接一個地傳輸。
在串行通信中,每個數(shù)據(jù)位按照順序傳輸,而在并行通信中,多個數(shù)據(jù)位可以同時傳輸。串行通信更適合長距離傳輸和連接設備之間的通信。
UART是異步通信協(xié)議,這意味著數(shù)據(jù)傳輸不依賴于定時時鐘。相反,發(fā)送和接收設備之間的通信通過起始位、數(shù)據(jù)位、奇偶校驗位和停止位的組合來同步。這種異步性使得UART協(xié)議在不同速率和不同設備之間的通信更為靈活。
UART通信通常涉及兩個設備,一個充當發(fā)送器,一個充當接收器。它們通過兩根線(TX線和RX線)連接。TX線用于發(fā)送數(shù)據(jù),RX線用于接收數(shù)據(jù)。
UART廣泛用于嵌入式系統(tǒng)、傳感器、模塊之間的通信,以及計算機系統(tǒng)中串行端口的實現(xiàn)。它是一種簡單而可靠的通信方式,適用于各種不同的應用場景。
二、引腳描述
三.波特率計算
系統(tǒng)時鐘50MHz
(1bit/波特率bit/s)秒 / (1/50MHz)秒
四.UART時序
UART數(shù)據(jù)幀包括起始位(Start Bit)、數(shù)據(jù)位、奇偶校驗位(Parity Bit,可選)、停止位(Stop Bit)。起始位指示數(shù)據(jù)幀的開始,停止位表示數(shù)據(jù)幀的結束。數(shù)據(jù)位的數(shù)量決定了可以傳輸?shù)臄?shù)據(jù)范圍,而奇偶校驗位用于檢測傳輸錯誤。
五.verilog代碼
module uart_rx( clk , rst_n , rx_uart , rx_data); parameter DATA_R = 8;parameter DATA_0 = 13;parameter DATA_1 = 4; input clk;input rst_n;input rx_uart;output [DATA_R-1:0] rx_data; reg [DATA_R-1:0] rx_data; reg [DATA_0-1:0] cnt0;wire add_cnt0;wire end_cnt0; reg [DATA_1-1:0] cnt1 ;wire add_cnt1;wire end_cnt1; wire nedge;reg flag_add; //9600比特率計數(shù)always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt0 <= 0; end else if(add_cnt0)begin if(end_cnt0) cnt0 <= 0; else cnt0 = cnt0 + 1'b1; endendassign add_cnt0 = flag_add;assign end_cnt0 = add_cnt0 && cnt0 == 5208 - 1; //9比特串口數(shù)據(jù)計數(shù)always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt1 <= 0; end else if(add_cnt1)begin if(end_cnt1) cnt1 <= 0; else cnt1 <= cnt1 + 1'b1; endendassign add_cnt1 = end_cnt0;assign end_cnt1 = add_cnt1 && cnt1 == 9 - 1; //邊沿檢測,接D觸發(fā)器//異步信號同步化,防止亞穩(wěn)態(tài),打兩拍:0] uart_sync;always @(posedge clk or negedge rst_n)begin if(!rst_n)begin uart_sync <= 3'b111; end else uart_sync <= {uart_sync[1:0],rx_uart};endassign nedge = uart_sync[2:1] == 2'b10; //當接收到的串口由1變0時,flag_add置位//當計數(shù)結束時,flag_add復位always @(posedge clk or negedge rst_n)begin if(!rst_n)begin flag_add <= 0; end else if(nedge)begin flag_add <= 1; end else if(end_cnt1)begin flag_add <= 0; endend == 0時,接收的是起始位0,1-8才是數(shù)據(jù)位always @(posedge clk or negedge rst_n)begin if(!rst_n)begin rx_data <= 8'h00; end else if (add_cnt0 && cnt0 == 5208/2 - 1 && cnt1 > 0) begin - 1] <= rx_uart; endend endmodule
module uart_tx( clk , rst_n , tx_vld , tx_data , uart_tx); parameter DATA_T = 8;parameter DATA_0 = 13;parameter DATA_1 = 4; input clk;input rst_n;input tx_vld;input [DATA_T-1:0] tx_data;output uart_tx; reg uart_tx; reg [DATA_0-1:0] cnt0;wire add_cnt0;wire end_cnt0; reg [DATA_1-1:0] cnt1;wire add_cnt1;wire end_cnt1; reg add_flag; reg [9:0] tx_data_temp;wire load_data; wire en_send; //9600波特率計數(shù)器always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt0 <= 0; end else if(add_cnt0)begin if(end_cnt0) cnt0 <= 0; else cnt0 <= cnt0 + 1'b1; endendassign add_cnt0 = add_flag;assign end_cnt0 = add_cnt0 && cnt0 == 5208 - 1; //10比特數(shù)據(jù)發(fā)送計數(shù)器always @(posedge clk or negedge rst_n)begin if(!rst_n)begin cnt1 <= 0; end else if(add_cnt1)begin if(end_cnt1) cnt1 <= 0; else cnt1 <= cnt1 + 1'b1; endendassign add_cnt1 = end_cnt0;assign end_cnt1 = add_cnt1 && cnt1 == 10 - 1; //add_flagalways @(posedge clk or negedge rst_n)begin if(!rst_n)begin add_flag <= 0; end else if(tx_vld)begin add_flag <= 1; end else if(end_cnt1)begin add_flag <= 0; endend //裝載數(shù)據(jù)always @(posedge clk or negedge rst_n)begin if(!rst_n)begin tx_data_temp <= 0; end else if(load_data)begin tx_data_temp <= {1'b1,tx_data,1'b0}; endendassign load_data = tx_vld && !add_flag; //發(fā)送數(shù)據(jù)always @(posedge clk or negedge rst_n)begin if(!rst_n)begin uart_tx <= 1;//1空閑位 end else if(en_send)begin uart_tx <= tx_data_temp[cnt1]; endendassign en_send = add_cnt0 && cnt0 == 0; endmodule
module UART_PORT( clk , rst_n , rx_uart , tx_vld , tx_data , rx_data , uart_tx);input clk;input rst_n;input rx_uart;input tx_vld;input [8-1:0] tx_data;output [8-1:0] rx_data;output uart_tx; :0] rx_data;wire uart_tx; uart_rx UART_RX( (clk), (rst_n), .rx_uart(rx_uart), .rx_data(rx_data)); uart_tx UART_TX( (clk), (rst_n), (tx_vld), .tx_data(tx_data), .uart_tx(uart_tx)); endmodule





