找回密码
 注册
关于网站域名变更的通知
查看: 202|回复: 1
打印 上一主题 下一主题

好用的Verilog串口UART程序

[复制链接]
  • TA的每日心情
    开心
    2019-11-20 15:00
  • 签到天数: 2 天

    [LV.1]初来乍到

    跳转到指定楼层
    1#
    发表于 2019-5-22 14:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    EDA365欢迎您登录!

    您需要 登录 才可以下载或查看,没有帐号?注册

    x
    好用的Verilog串口UART程序
    ==========================================================================
    ! r; V! r# x2 z( \2 B! a//-----------------------------------------------------7 D+ }8 n8 m$ B; L
    // Design Name : uart
    0 |' W5 ~. V( H, h8 \: K' B* I( Q// File Name   : uart.v6 y' H( S$ ?6 z% }
    //     : Simple UART
    7 H" l1 E" r/ K/ Q4 K3 W  O% e# }// Coder       : Deepak Kumar Tala
    / X: L+ X8 J3 `% j# c+ F9 r, b//-----------------------------------------------------
    2 t& d4 P! g: W2 \8 L& i) ]. [6 T6 pmodule uart (1 F. J- G# ?) O) p$ @$ N: T( g
    reset          ,
    ) y- Z& @# o5 Q' b# p) N% N4 C& G  Ltxclk          ,' x4 Z, h4 R" e9 A" E$ N; t+ T
    ld_tx_data     ,: k% L5 X# i1 {/ |
    tx_data        ,
    % F' }9 i& _3 ?  O! R9 Ptx_enable      ,
    - `6 Q# T  S( T" W! V% b$ Z6 otx_out         ,
    2 F& ^  `' Q+ Rtx_empty       ,
    ) _. j- B% N# K( J; Grxclk          ,
    / H' A( i6 A- s0 Z4 v  huld_rx_data    ,7 G1 `. e0 V. y3 M/ F5 G
    rx_data        ,
    & |9 y" o# K5 r1 q# ^rx_enable      ,
    $ y* N1 |8 Q3 A6 G/ W3 urx_in          ,
    : }8 a. H. j* w' {% ^  G3 erx_empty* B+ ^3 H3 ?9 r5 ?; ^
    );- o6 M  |/ f5 q/ b* ?- ^4 e
    // Port declarations
    ( P) N+ O. J8 h1 {# Rinput        reset          ;
    ' A( O  @. S$ f9 Q' U* }input        txclk          ;
    ! _; \: \5 Q/ Z/ Z1 D, I$ t6 Tinput        ld_tx_data     ;
    % ~, ]: b; @3 O$ E4 m; p5 sinput [7:0] tx_data        ;& X  L+ \/ z: A8 h' x2 Y
    input        tx_enable      ;1 {! T9 u$ w( c! K6 D
    output       tx_out         ;8 R" K2 d: U; V- g8 S) }0 z
    output       tx_empty       ;' ^4 ~5 X% k; I& H: }( A7 U
    input        rxclk          ;6 D9 ^. z3 c2 M- l/ V& k
    input        uld_rx_data    ;
    4 B% Q3 p+ F6 Voutput [7:0] rx_data        ;4 E: s1 }$ ^9 n
    input        rx_enable      ;
    0 {. p  P9 j/ C; J& hinput        rx_in          ;
    ! W& P" f2 \6 @- [output       rx_empty       ;
    7 ]9 _2 Y6 D. x3 U" h
    6 ~  h8 N% o9 m1 B$ E// Internal Variables
    9 z; c( A; @9 b( J/ Q  \" u! e1 Greg [7:0]    tx_reg         ;' U( {5 r3 q6 n1 a7 e5 e5 ]
    reg          tx_empty       ;
    . E; V: e; U1 ]* i7 Ireg          tx_over_run    ;  y9 u! P3 w5 @9 [+ q1 g
    reg [3:0]    tx_cnt         ;
    9 A: A. ]; h) Greg          tx_out         ;
    , H% y% T/ B4 j0 @reg [7:0]    rx_reg         ;- f, b$ I: I3 i8 [9 p1 ?0 Z# E
    reg [7:0]    rx_data        ;" f4 e9 Z- ~+ D: R/ t0 r7 m
    reg [3:0]    rx_sample_cnt ;
    , v6 U/ L  o9 v0 a1 {6 Zreg [3:0]    rx_cnt         ; 4 `. q" I4 c& \9 Q7 p$ e6 N6 w
    reg          rx__err   ;
    & f! D1 Z* a0 treg          rx_over_run    ;
    + J; U1 p# w% F  dreg          rx_empty       ;, n8 V9 H: A! Y+ k
    reg          rx_d1          ;
    . k! K; a0 V0 }2 {8 w$ v- _reg          rx_d2          ;3 z0 E* B4 k2 Q) o
    reg          rx_busy        ;
    - t* b0 }& ?9 V
    7 ]$ b2 o3 K: C- X) o9 z3 m// UART RX Logic
    ! t. W' H8 k/ J% E" Dalways @ (posedge rxclk or posedge reset)
    , j( L6 V2 @& jif (reset) begin
    5 t1 I% T2 e9 H1 B0 vrx_reg        <= 0; 5 K+ k7 a# l5 @* G3 j: \6 t* [: U
    rx_data       <= 0;0 [; v% Q  ]& O+ B: F! x
    rx_sample_cnt <= 0;1 L2 d, V1 k7 j3 j* X
    rx_cnt        <= 0;2 d3 I+ X4 |7 \! {
    rx__err <= 0;0 c$ t1 `- V+ G& T3 h, J
    rx_over_run   <= 0;
    , K( y% x; }6 f8 ?+ e7 vrx_empty      <= 1;* u, c( q' I" U# u% d& y4 c4 H9 \
    rx_d1         <= 1;
    & y6 w% t5 S- d0 z7 Irx_d2         <= 1;! v# a* C8 Y" A  s. Z6 A
    rx_busy       <= 0;
    ! c; Y" s$ m3 X: [. k; [3 eend else begin. h% b" S- L! H/ o( E- q; z
    // Synchronize the asynch signal
    # K2 f6 v+ V* n) Wrx_d1 <= rx_in;; T7 H( {9 @% K" i
    rx_d2 <= rx_d1;6 u' X  _. E- B- X
    // Uload the rx data
    & M4 u* B1 w, X; j% f3 @if (uld_rx_data) begin, d- [3 f0 e' U5 M' E
        rx_data <= rx_reg;
    - ], ^' J7 p' f* K. x6 T  i( `5 D    rx_empty <= 1;2 T+ u& E0 p8 h5 u5 P! |
    end  x" R/ o7 {6 o/ Q
    // Receive data only when rx is enabled& D2 Y# T& t& a* U9 m
    if (rx_enable) begin) H) a. r  Z4 ]' M
        // Check if just received start of
    : O( R+ c" Y+ Iif (!rx_busy && !rx_d2) begin! D( e9 J* m$ e: K* G- U
          rx_busy       <= 1;5 o8 S/ A2 u, b& A3 U- `
          rx_sample_cnt <= 1;: A5 y& x8 R4 n; I" |8 y/ z
          rx_cnt        <= 0;3 @( C- e; R# R" P+ [; Z" x0 R. a
        end
    % m% @- s5 I' H& x! {    // Start of detected, Proceed with rest of data
    1 t; _" D& v) ]  ~3 g    if (rx_busy) begin
    % u$ p, K  Y- B# P" l5 x       rx_sample_cnt <= rx_sample_cnt + 1;! I: K- \3 x, j/ A  z9 C
           // Logic to sample at middle of data
    - l5 q; t% H( v+ _8 B       if (rx_sample_cnt == 7) begin
    % W1 F7 w: d3 @' K# ^/ j. ]5 k          if ((rx_d2 == 1) && (rx_cnt == 0)) begin; h- F( l) ~8 _' i3 O
                rx_busy <= 0;7 R8 J! |0 K' L) G% I
              end else begin
    5 s  Q8 a0 p. F) z            rx_cnt <= rx_cnt + 1;
    ( P- l" H! W% W$ k4 E2 ^* {            // Start storing the rx data' `; u- O7 e4 I  p4 W) ^& f
                if (rx_cnt > 0 && rx_cnt < 9) begin/ n- M# [0 j  p: C/ s2 E
                  rx_reg[rx_cnt - 1] <= rx_d2;
    ' t/ }  Q2 y/ S! n            end) s. r' H; P8 w9 w
                if (rx_cnt == 9) begin4 _2 v- s) l2 F3 m- l: X
                   rx_busy <= 0;
    ! ^; A+ Z4 a% A/ s! y1 k6 s               // Check if End of received correctly
    4 _* h' r' ~  B9 p- W               if (rx_d2 == 0) begin5 L5 x' ]1 E3 h: U8 u9 E7 W
                     rx__err <= 1;- g8 S( I( }% u7 G! @) R
                   end else begin+ m8 f& Q$ F" B1 h6 C; z: F
                     rx_empty     <= 0;
    4 x, J5 f' J# k1 c                 rx__err <= 0;
    ) H" N3 @$ ~' `. Q5 Y/ j. }) T: m                 // Check if last rx data was not unloaded,
    * k* A( Y& |2 h* D; {6 \7 a                 rx_over_run <= (rx_empty) ? 0 : 1;' n8 t7 M% ?( }  D
                   end
    2 W8 O* a+ j( l2 n6 B9 k# t- C# `* j8 c            end
    + r  \; C3 }- t; X/ M          end3 V  ~( u% M/ n! x; a2 u8 {& r
           end 6 P7 `! P1 @% G) W5 I& Z8 [
        end , j6 O) \- U! n7 X
    end: ]) O8 k+ E* O
    if (!rx_enable) begin& H% P2 f3 R6 n# U& ^* E( e& x# I" w
        rx_busy <= 0;
    3 q7 g. `2 j: F5 T4 e: Gend
    1 A6 j8 G: B! C0 @end0 e; U9 D( h& z
    ( U9 e* k" U& k) k! L
    // UART TX Logic
    $ f2 q, V2 |3 Z7 B, U2 Q- Galways @ (posedge txclk or posedge reset)7 w' ?& N9 n% D! V6 y
    if (reset) begin: h0 q% z+ G0 N0 [
    tx_reg        <= 0;! F% H3 h* r, l+ ?4 T
    tx_empty      <= 1;& q$ \+ ~1 U# [$ E3 T
    tx_over_run   <= 0;
    , G1 R# Q2 o* t% Z3 k! P* c2 Gtx_out        <= 1;
      x; Q9 y0 t: B# x5 Htx_cnt        <= 0;
    ' j' U% o1 O( H- L( E7 L# q) @. {8 D( uend else begin
    6 ~  d0 v7 S: Z3 _   if (ld_tx_data) begin
    ! K8 f& t8 |1 @' m6 k      if (!tx_empty) begin1 H/ P7 L) b; k8 \( }
            tx_over_run <= 0;
    # l! W% R+ T, I, O! T! J      end else begin
    , A5 X6 f4 \, e        tx_reg   <= tx_data;  Y( }7 R! \4 p6 Y/ S+ T6 f4 J9 O
            tx_empty <= 0;) i$ y6 f# d% k$ |  R0 t
          end
    % x6 B8 i8 F- r9 M  `   end
    8 w( T  w$ ~4 ]) H) f. c   if (tx_enable && !tx_empty) begin; p( D7 c$ T7 E! z; Q
         tx_cnt <= tx_cnt + 1;/ W0 j4 T( i) I: S
         if (tx_cnt == 0) begin, q  |& G$ c' V2 K8 Q$ H, v- S
           tx_out <= 0;& J6 L" x5 o# R* A5 j* P
         end
      [! r' o& I- w2 F9 W3 ^     if (tx_cnt > 0 && tx_cnt < 9) begin
    + m/ D. _$ A9 _        tx_out <= tx_reg[tx_cnt -1];, X& E. b9 L! A
         end2 H6 X: X8 p  z# Y
         if (tx_cnt == 9) begin
    8 y  ]3 O# ~+ ^% i0 a4 P7 E       tx_out <= 1;: `* s* A3 I$ o+ ~9 @
           tx_cnt <= 0;! N/ `7 U) n1 j$ E7 y. q
           tx_empty <= 1;0 P5 I4 y5 N" g& X
         end( j9 M3 ^( t8 e. H; e+ \* R. ~% {
       end+ S, P6 e1 e! k) G' [7 k, q
       if (!tx_enable) begin- y& L/ j" H& |# g- V
         tx_cnt <= 0;
    $ U4 k" x+ k0 s2 V# p   end
    2 Q6 }2 R( R* M* R; Z4 Xend
    8 ?( Z  `2 U9 A4 T% h3 t7 `
    ; S  [8 K6 W6 O4 aendmodule
    / G, P' \5 A& ^) v7 v$ [
    ' Q& ]4 f( m7 u3 m' k
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-8-11 13:42 , Processed in 0.125000 second(s), 23 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表