集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 1112|回复: 2

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

[复制链接]
大辉 发表于 2017-8-7 17:54:47 | 显示全部楼层 |阅读模式
data没有波形,我是不是写错了,大佬帮我找找错误啊!!!!!9600 bit/s
module receive(clk,rst_n,rx,en,data);
        input clk;
        input rst_n;
        input  rx; //
       
        output reg [7:0]data;
        output reg en;                //一帧数据接收完毕时能信号
        reg [7:0]data_buf;        //数据寄存器相当于fifo
        reg [15:0]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[0]<=rx;         //缓存第1个数据
        13020:data_buf[1]<=rx;        //缓存第2个数据
        18228:data_buf[2]<=rx;        //缓存第3个数据
        23436:data_buf[3]<=rx;        //缓存第4个数据
        28644:data_buf[4]<=rx;        //缓存第5个数据
        33852:data_buf[5]<=rx;        //缓存第6个数据
        39060:data_buf[6]<=rx;        //缓存第7个数据
        44268:data_buf[7]<=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 [7:0]data; //串转并的数据
       
        output reg tx;
        reg [15:0]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[0];//发送第一个数据
                10416:tx<=data[1];
                15624:tx<=data[2];
                20832:tx<=data[3];
                26040:tx<=data[4];
                31248:tx<=data[5];
                36356:tx<=data[6];
                41664:tx<=data[7];//发送第八个数据
                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 [7:0]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 下一条

QQ|小黑屋|手机版|Archiver|fpga论坛|fpga设计论坛 ( 京ICP备20003123号-1 )

GMT+8, 2025-4-20 12:40 , Processed in 0.062597 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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