0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

解析常用串行總線——UART協(xié)議(下)

jf_78858299 ? 來源:Cascatrix ? 作者:Carson ? 2023-01-21 16:53 ? 次閱讀

2. 接收模塊 (uart_rx):

module uart_rx (

clk_40k, //clock signal, 40kHz

rst_n, //reset signal, active low

bit_in, //the input serial bit,

dout_vld,//the output valid signal, active high,the dout is valid when this signal is high.

dout //received data, 8 bit width

);

input bit_in;

input clk_40k;

input rst_n;

output reg dout_vld;

output reg [7:0] dout;

reg rx_flag;

reg [6:0] cnt;

reg [5:0] rx_cnt;

reg [7:0] dout_temp;

//rx_flag: 接收過程rx_flag始終拉高

always @ (posedge clk_40k)

begin

if(~rst_n)

rx_flag <= 1'b0;

else if(bit_in == 1'b0)

rx_flag <= 1'b1;

else if(rx_cnt == 6'd9)

rx_flag <= 1'b0;

end

//cnt: 接收數(shù)據(jù)計(jì)數(shù),clk_40k分頻至1k波特率對接收數(shù)據(jù)進(jìn)行計(jì)數(shù)

always @ (posedge clk_40k)

begin

if(~rst_n)

cnt <= 7'b0;

else if(rx_flag == 1'b1 && cnt != 7'd39)

cnt <= cnt + 1'b1;

else if(rx_flag == 1'b0 || cnt == 7'd39)

cnt <= 7'b0;

end

always @ (posedge clk_40k)

begin

if(~rst_n)

rx_cnt <= 6'b0;

else if(rx_flag == 1'b1 && cnt == 7'd39)

rx_cnt <= rx_cnt + 1'b1;

else if(rx_flag == 1'b0)

rx_cnt <= 6'b0;

end

//dout_temp: 將串行接收數(shù)據(jù)轉(zhuǎn)換還原為8bit數(shù)據(jù)

always @ (posedge clk_40k)

begin

if(~rst_n)

dout_temp <= 8'b0;

else if(rx_flag == 1'b1 && cnt == 7'd39)

begin

dout_temp[7] <= bit_in;

dout_temp[6:0] <= dout_temp[7:1];

end

end

//dout_vld: 傳輸完成標(biāo)識,8bit數(shù)據(jù)傳輸結(jié)束拉高

always @ (posedge clk_40k)

begin

if(~rst_n)

dout_vld <= 1'b0;

else if(rx_cnt == 6'd9 && cnt == 7'b0)

begin

dout <= dout_temp;

dout_vld <= 1'b1;

end

else

dout_vld <= 1'b0;

end

endmodule

3. Testbench(tb):

`timescale 1us/1us

module tb();

reg clk_40k;

reg rst_n;

reg [7:0] din;

reg send_start;

wire bit_out;

wire bit_in;

wire dout_vld;

wire [7:0] dout;

assign bit_in = bit_out;

uart_tx i_uart_tx(

.clk_40k (clk_40k ),

.rst_n (rst_n ),

.din (din ),

.send_start (send_start),

.bit_out (bit_out )

);

uart_rx i_uart_rx(

.clk_40k (clk_40k ),

.rst_n (rst_n ),

.bit_in (bit_in ),

.dout_vld (dout_vld ),

.dout (dout )

);

initial

begin

rst_n = 1'b0;

#10

rst_n = 1'b1;

end

initial

begin

clk_40k = 1'b0;

forever

#1

clk_40k = ~clk_40k;

end

initial

begin

send_start = 1'b0;

din = 8'd0;

forever

begin

    #1000

    din = $random()%256;

    send_start = 1'b1;

    #2

    send_start = 1'b0;

end

end

endmodule

4. 仿真結(jié)果 :

按照testbench對UART收發(fā)端進(jìn)行仿真,仿真結(jié)果如圖:

圖片

  1. 系統(tǒng)時(shí)鐘和傳輸速率通常不一致,clk_40k為高頻系統(tǒng)時(shí)鐘,利用計(jì)數(shù)器分頻實(shí)現(xiàn)1k波特率;
  2. 復(fù)位信號rst_n低電平有效,正常傳輸時(shí)始終處于高電平。
  3. 開始傳輸時(shí)send_start信號拉高,傳輸結(jié)束時(shí)dout_vld信號拉高;
  4. 輸入數(shù)據(jù)din在傳輸結(jié)束后在輸出數(shù)據(jù)dout體現(xiàn)出來,傳輸時(shí)延為1個(gè)數(shù)據(jù)長度。

04

UART的優(yōu)缺點(diǎn)

4.1 UART協(xié)議優(yōu)點(diǎn)

  1. 通信只需要兩條數(shù)據(jù)線;
  2. 無需時(shí)鐘信號;
  3. 有奇偶校驗(yàn)位,方便通信的差錯(cuò)檢查;
  4. 只需要接收端和發(fā)送端設(shè)置好數(shù)據(jù)包結(jié)構(gòu),即可穩(wěn)定通信。

4.2 UART協(xié)議缺點(diǎn)

  1. 數(shù)據(jù)幀最大支持9位數(shù)據(jù);
  2. 不支持多主機(jī)或多從機(jī)的主從系統(tǒng)。
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • 串行
    +關(guān)注

    關(guān)注

    0

    文章

    237

    瀏覽量

    33737
  • uart
    +關(guān)注

    關(guān)注

    22

    文章

    1214

    瀏覽量

    100997
  • 通訊接口
    +關(guān)注

    關(guān)注

    2

    文章

    80

    瀏覽量

    16158
收藏 人收藏

    評論

    相關(guān)推薦

    常用的串口通信協(xié)議UART異步串行通信簡析

    UART是異步串行通信口的總稱。它所包含的RS232RS449RS423等等是對應(yīng)各種異步串行通信口的接口標(biāo)準(zhǔn)和總線標(biāo)準(zhǔn)。他們規(guī)定了通信口的電氣特性、傳輸速率、連接特性和機(jī)械特性等一系
    發(fā)表于 07-10 09:06 ?3081次閱讀

    常用串行總線(一)——UART協(xié)議(Verilog實(shí)現(xiàn))

    通用異步收發(fā)傳輸器(Universal Asynchronous Receiver/Transmitter),通常稱作UART。它將要傳輸?shù)馁Y料在串行通信與并行通信之間加以轉(zhuǎn)換。作為把并行輸入信號轉(zhuǎn)成串行輸出信號的芯片,
    發(fā)表于 01-05 09:48 ?3481次閱讀

    UART串口通訊協(xié)議解析

    ,協(xié)議如下: 起始位 數(shù)據(jù)位 奇偶校驗(yàn)位 停止位 1bit 5-9bit 0-1bit 1-2bit 空閑位 :UART協(xié)議規(guī)定,當(dāng)總線處于空閑狀態(tài)時(shí)信號線的狀態(tài)為‘1’即高電平,表示
    發(fā)表于 09-12 16:01

    SPI、I2C、UART三種串行總線協(xié)議的區(qū)別

    實(shí)現(xiàn)從設(shè)備,則只需輸入口即可。I2C總線是雙向、兩線(SCL、SDA)、串行、多主控(multi-master)接口標(biāo)準(zhǔn),具有總線仲裁機(jī)制,非常適合在器件之間進(jìn)行近距離、非經(jīng)常性的數(shù)據(jù)通信。在它的
    發(fā)表于 11-02 09:48

    UART串口通訊協(xié)議是什么

    UART串口通訊協(xié)議解析概述接口通信協(xié)議概述通用異步收發(fā)傳輸器(Universal Asynchronous Receiver/Transmitter),通常稱作
    發(fā)表于 07-29 08:07

    常用串行總線協(xié)議有哪些

    一、常用串行總線協(xié)議目前常用的微機(jī)與外設(shè)之間進(jìn)行數(shù)據(jù)傳輸?shù)?b class='flag-5'>串行
    發(fā)表于 11-03 07:14

    常用串行總線協(xié)議有哪些

    常用串行總線協(xié)議I2C總線、SPI總線、SCI總線
    發(fā)表于 11-19 06:46

    串行通信中的IIC總線工作原理是什么

    串行通信中的IIC總線工作原理51本身不帶IIC總線 ,使用程序模擬IIC通信協(xié)議常用串行
    發(fā)表于 12-08 07:52

    高速串行總線常用測試碼型

      本文討論了高速串行鏈路中常用的測試碼型偽隨機(jī)碼流的原理,以及不同的測試碼型對物理層測試結(jié)果的影響。   高速串行總線常用測試碼型
    發(fā)表于 01-04 10:40 ?4136次閱讀

    SPI、I2C、UART串行總線協(xié)議

    串口通訊通信協(xié)議UART,以及常用外設(shè)通信協(xié)議 SPI、I2C的介紹與他們之間的區(qū)別
    發(fā)表于 11-30 11:28 ?93次下載

    SPI、I2C、UART三種串行總線協(xié)議的區(qū)別

    SPI、I2C、UART三種串行總線協(xié)議的區(qū)別
    發(fā)表于 07-17 17:23 ?0次下載

    解析常用串行總線——UART協(xié)議(上)

    通用異步收發(fā)傳輸器** (Universal Asynchronous Receiver/Transmitter),通常稱作UART。它將要傳輸?shù)馁Y料在串行通信與并行通信之間加以轉(zhuǎn)換。作為把并行輸入信號轉(zhuǎn)成串行輸出信號的芯片,
    的頭像 發(fā)表于 01-21 16:50 ?1691次閱讀
    <b class='flag-5'>解析</b><b class='flag-5'>常用</b><b class='flag-5'>串行</b><b class='flag-5'>總線</b>——<b class='flag-5'>UART</b><b class='flag-5'>協(xié)議</b>(上)

    常用串行總線——SPI協(xié)議()

    SPI(Serial Perripheral Interface, 串行外圍設(shè)備接口)** 是 Motorola 公司推出的一種同步串行接口技術(shù)。SPI 總線在物理上是通過接在外圍設(shè)備微控制器
    的頭像 發(fā)表于 01-21 17:03 ?914次閱讀
    <b class='flag-5'>常用</b><b class='flag-5'>串行</b><b class='flag-5'>總線</b>——SPI<b class='flag-5'>協(xié)議</b>(<b class='flag-5'>下</b>)

    討論使用UART通信協(xié)議的基本原則

    UART,即通用異步接收器/發(fā)送器,是最常用的設(shè)備間通信協(xié)議之一,正確配置后,UART可以配合許多不同類型的涉及發(fā)送和接收串行數(shù)據(jù)的
    的頭像 發(fā)表于 02-01 17:54 ?950次閱讀

    UART協(xié)議的工作原理和應(yīng)用場景

    UART(Universal Asynchronous Receiver/Transmitter,通用異步收發(fā)傳輸器)協(xié)議是一種廣泛使用的串行通信協(xié)議,它允許計(jì)算機(jī)與外部設(shè)備之間通過
    的頭像 發(fā)表于 08-25 17:15 ?1906次閱讀