Thursday, 19 April 2012

Design of UART Transmitter in VERILOG


Abstract
            UART is usually an  individual  integrated  circuit  used  for  serial communications over  a  computer  or  peripheral  device  serial  port.  This  UART  is  designed  to  make  an interface  between  RS232  line  and  a  micro  controller  or  an  IP  core.  It works fine connected to a serial port of a PC for data exchange with custom electronic. UARTs are now commonly included in micro controllers.  In  this  paper  we  will  design  Uart transmitter  and  Uart  Receiver  module  .Design  coding  can  be  done  either  by  Verilog  or VHDL  by  using  Xilinx  ISE.  Examples  of  such  are  optical  fiber,  IrDA  (infrared),  and (wireless)  Bluetooth  in  its  Serial  Port  Profile  (SPP).  Some signaling schemes use modulation of a carrier signal (with or without wires). Examples are modulation of audio signals  with  phone  line  modems,  RF  modulation  with  data  radios,  and  the  DC  line  for power line communication

VERILOG CODING FOR UART TRANSMITTER

        TOP MODULE:

            module uart_tx(
                                clk,
                                rst_n,
                                write,
                                data,
                                txrdy,
                                tx);

         // Port declarations

              input       clk;
             input       rst_n;
             input       write;
             input [7:0] data;
             output      txrdy;
             output      tx;

       // Internal signal declarations

           reg         txdatardy;
          reg   [12:0] cnt;
          reg   [9:0]txdat;
          wire  [7:0] data_in;
          reg         baud_clk;
         wire        txrdy;
         reg         tx_sts;

        always @ (posedge clk)
             begin
                if(~rst_n)                                      tx_sts <= 1'b0;
                     else if(write&txrdy)               tx_sts <= 1'b1;
                    else if(txrdy)                            tx_sts <= 1'b0;
            end

       always @ (posedge clk)
           begin
                if(~rst_n)                                                        cnt <= 13'b0;
                 else if(tx_sts & (cnt[12:0] == 5208))           cnt <= 13'b0;
                 else if(tx_sts)                                                cnt <= cnt + 1;
             else                                                                   cnt <= 13'b0;
           end

          always @ (posedge clk)
              begin
                 if(~rst_n)                                             baud_clk <= 1'b0;
                else if(cnt[12:0] == 2601)                   baud_clk <= 1'b1;
                else                                                      baud_clk <= 1'b0;
             end

         always @ (posedge clk)
           begin
               if(~rst_n)                                             txdat <= 10'h0;
              else if(write & txrdy)                          txdat <= {1'b1,data,1'b0};
              else if(baud_clk)                                 txdat <= txdat>>1;
           end

     //assign data_in = {data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]};
                assign tx      = txrdy ? 1'b1 : txdat[0];
                assign txrdy   = (!(|txdat));

           endmodule

TEST BENCH:

          module uart_tx_tb_v;

                   // Inputs

             reg clk;
            reg rst_n;
            reg write;
            reg [7:0] data;

            // Outputs
            wire txrdy;
            wire tx;           

            // Instantiate the Unit Under Test (UUT)
            uart_tx uut (
                        .clk(clk),
                        .rst_n(rst_n),
                        .write(write),
                        .data(data),
                        .txrdy(txrdy),
                        .tx(tx)
            );
            initial begin
                        // Initialize Inputs
                        clk = 0;
                        rst_n = 0;
                        write = 0;
                        data = 0;
                        // Wait 100 ns for global reset to finish
                        #100;
                        rst_n = 1;
                        #20
                        write = 1'b1;
                        data = 8'b00011111;
                                                #120 data = 8'b10101010;
                         // Add stimulus here
            end
           
            always
              begin
                #5        clk<=1'b0;
               #5 clk<=1'b1;
            end
     endmodule




5 comments:

  1. How to set baud rate in this code?

    ReplyDelete
  2. Change the external CLK value which will change the bit sample rates which is effectively changing the baud rate.

    You can also view the following commented source code for an UART (Verilog) for additional details:

    https://github.com/cyrozap/osdvu

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. how did you assign the pins? may you give me the files? i really hope that you can give it to me since i need it for my final project, hmmmm

    ReplyDelete
  5. What is the logic for coding the receiver part, can u just me..

    ReplyDelete