大辉 发表于 2017-8-7 17:54:47

串行通信 大神帮忙看看我做的代码,

data没有波形,我是不是写错了,大佬帮我找找错误啊!!!!!9600 bit/s
module receive(clk,rst_n,rx,en,data);
        input clk;
        input rst_n;
        inputrx; //
       
        output reg data;
        output reg en;                //一帧数据接收完毕时能信号
        reg data_buf;        //数据寄存器相当于fifo
        reg cnt;                //序列计数器
        reg int1;                //计数使能
//----序列计数器---
always @(posedge clk,negedge rst_n)
                begin
                        if(rst_n==0)
                                cnt<=0;
                        else if(int1)
                                        cnt<=cnt+1;
                                  else
                                        cnt<=0;
                end
//---接收数据----
always @(posedge clk,negedge rst_n)
        begin
                if(!rst_n)
                        begin
                                data<=0;
                                data_buf<=0;
                                en<=0;
                                int1<=0;
                        end
                else
                case(cnt)
        0:begin
                if(!rx)   //检测RX是否为低电平
                        int1<=1 ; //计数使能拉高,开始计数
                end
        7812:data_buf<=rx;         //缓存第1个数据
        13020:data_buf<=rx;        //缓存第2个数据
        18228:data_buf<=rx;        //缓存第3个数据
        23436:data_buf<=rx;        //缓存第4个数据
        28644:data_buf<=rx;        //缓存第5个数据
        33852:data_buf<=rx;        //缓存第6个数据
        39060:data_buf<=rx;        //缓存第7个数据
        44268:data_buf<=rx;        //缓存第8个数据
        49476:data<=data_buf;//接收完一帧数据
        52079:en<=1;         //一帧接收完毕使能拉高,标志信号
        52080:begin en<=0;int1<=0;end
       
        endcase
end
endmodule
       

module send(clk,rst_n,en,tx,data);
        input clk;
        input rst_n;
        input en; //发送端使能信号
        input data; //串转并的数据
       
        output reg tx;
        reg cnt; //序列计数器
        reg int1; //计数器时能信号
       
        //---序列计数器---
        always @(posedge clk,negedge rst_n)
                begin
                        if(rst_n==0)
                                cnt<=0;
                        else if(int1)
                                cnt<=cnt+1;
                                else
                                cnt<=0;
                end
//---发送数据---
        always @(posedge clk,negedge rst_n)
                if(rst_n==0)
                        begin
                                tx<=1;
                                int1<=0;
                        end
                else
                case(cnt)
                0:begin
                        if(en)
                                begin
                                        tx<=0;
                                        int1<=1;
                                end
                        end
                5208:tx<=data;//发送第一个数据
                10416:tx<=data;
                15624:tx<=data;
                20832:tx<=data;
                26040:tx<=data;
                31248:tx<=data;
                36356:tx<=data;
                41664:tx<=data;//发送第八个数据
                46872:tx<=1;//发送停止位
                52080:int1<=0; //停止计数
                endcase
        endmodule        
                                       
       
                       
        module uart(clk,rst_n,rx,tx);
        input clk;
        input rst_n;
        input rx;
        output tx;
       
        wire data;
        wire en;
       
       
        receive receive(
        .clk        (clk)        ,
        .rst_n(rst_n)        ,
        .rx        (rx)        ,
        .en        (en)        ,
        .data(data));
       
       
        send send(
        .clk        (clk)        ,
        .rst_n(rst_n)        ,
        .en        (en)        ,
        .tx        (tx)        ,
        .data(data));
       
        endmodule
       
       
       
       
       
       
       
        `timescale 1ns/1ns
module tb;
        reg clk;
        reg rst_n;
        reg rx;
        wire tx;
always #10 clk=~clk;
        initial
                begin
                rst_n=0;
                clk=1;
                rx=1;
                #100.1
                rst_n=1;
                #200
                rx=0;
                #245
                rx=1;
                #500
                rx=0;
                #800
                rx=1;
                #1000.1
                rx=0;
                #1200.1 $stop;
                end
                uart uart(
                .clk(clk)        ,
                .rst_n(rst_n),
                .rx        (rx),
                .tx(tx));
       
                endmodule
                               
                       
                       
                       
                       
                       
                                                                                                                                                                                                                                                                                                                                                                                                                      
                               
                               
                               
                               
                               
                               
                               
                               
                               
       

大辉 发表于 2017-8-7 17:56:17

新手上路,大神多多指教:(

小00海 发表于 2017-8-7 20:59:31

你把仿真图形也放出来
页: [1]
查看完整版本: 串行通信 大神帮忙看看我做的代码,