当前位置:   article > 正文

FPGA 串口发送_fpga使用串口发送

fpga使用串口发送


module uart_tx(
        clk             ,
        rst             ,
        tx_busy         ,    
        data_in         ,
        data_out        ,
        tx_en           ,
        tx_done
   
    );
        input               clk                ;   //50Mhz
        input               rst                ;   //复位
        input               [7:0]data_in       ;   //发送的UART数据
        input               tx_en              ;   //发送EN
        
        output    wire       tx_busy            ;    //正在发送 忙
        output    reg        data_out           ;    //输出数据 BIT
        output    reg        tx_done            ;    //发送完成

        reg       [9:0]      send_buf           ;       //发送缓冲区
        reg       [12:0]     tx_bit_cnt         ;       //发送单个bit 需要的时钟周期
        reg       [4:0]      tx_cnt             ;       //发送计数
        reg                  start_cnt          ;     // 发送数据期间保持为1
        reg                  send_en            ;//收到发送信号开始发送


     

     parameter    tx_bit_cnt_time  = 433  ;
     
assign tx_busy = start_cnt;
//发送数据计时
        always@(posedge clk)
            if(!rst)    
                begin
                     tx_bit_cnt      <=      0        ;
                     tx_cnt          <=      0        ;
                end
            else if(start_cnt)
                begin
                    tx_bit_cnt <= tx_bit_cnt  + 1'b1 ;  
                    if(tx_bit_cnt == tx_bit_cnt_time)
                        begin
                            tx_bit_cnt <= 0 ;
                            tx_cnt     <= tx_cnt + 1'b1 ; 
                        end 
                end
            else begin
                tx_bit_cnt      <=      0        ;
                tx_cnt          <=      0        ;
            end

 always@(posedge clk)
    if(!rst)
        start_cnt<= 1'b0   ;    
    else begin
        case(start_cnt) 
            0:  begin//等待发送
                    if(send_en)
                        begin
                            send_buf <= {1'b1,data_in,1'b0};//位拼接
                            start_cnt <= 1   ;
                        end
                        else begin
                            send_buf  <= 0   ;
                            tx_done   <= 0   ;
                            data_out  <= 1   ;
                        end
                end
            1:  begin//开始发送数据
                    if(tx_bit_cnt == 0)//开始发送
                        begin
                            data_out <= send_buf[tx_cnt] ;  
                            end
                    if (tx_cnt == 9 && tx_bit_cnt == tx_bit_cnt_time -1)  //发送完成
                        begin
                           start_cnt <= 0;   
                           tx_done   <= 1;
                           end
                end
               default :start_cnt <= 0;
        endcase
    end     
    
    always@(posedge clk)
        if(!rst)
            send_en <= 0;
         else if(tx_en)
            send_en = 1;
            else if(start_cnt)
            send_en <= 0;
                        
endmodule
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/516597
推荐阅读
相关标签
  

闽ICP备14008679号