9 - 状态机代码设计与仿真

9 - 状态机代码设计与仿真

串口数据发送

image-20211123182922971

状态规划:

image-20211123182958124

代码:

//2021.11.23 lyw
//Serial port data sending

`timescale 1ns/10ps

module UART_TXer (
                clk,
                res,
                data_in,
                en_data_in,
                TX,
                rdy
);

input           clk;
input           res;
input[7:0]      data_in;    //data ready to send 
input           en_data_in;     //enable to send
output          TX;
output          rdy;    //free flag: 0 means free

reg [3:0]       state;  //main state machine
reg [9:0]       send_buf;   //sending register
assign          TX=send_buf[0];     //connect TX

reg [12:0]      con;    //Calculating Porter period
reg [9:0]       send_flag;  //right shift is over
reg             rdy;    //ready,also busy

always @(posedge clk or negedge res) begin
    if(~res) begin
        state<=0; send_buf<=1;con<=0;rdy<=0;
        send_flag<=10'b10_0000_0000;
    end
    else begin
        case(state)
        0://wait enable signal
        begin
            if (en_data_in) begin
                send_buf={1'b1,data_in,1'b0};
                send_flag<=10'b10_0000_0000;
                rdy<=1;
                state<=1;
            end
        end
        1://Serial port send, right shift register 
        begin
            if (con==5000-1) begin
                con<=0;
            end
            else begin
                con<=con+1;
            end

            if (con==5000-1) begin
                send_buf[8:0]=send_buf[9:1];
                send_flag[8:0]=send_flag[9:1];
            end
            if (send_flag[0]) begin
                state<=0;
                rdy<=0;
            end
            
        end
        endcase
end 

end
endmodule


//-----testbench of UART_TXer-----
module UART_TXer_tb;

reg             clk,res;
reg [7:0]       data_in;
reg             en_data_in;
wire            TX;
wire            rdy; 

UART_TXer UART_TXer (
                clk,
                res,
                data_in,
                en_data_in,
                TX,
                rdy
);

initial begin
                clk<=0;res<=0;data_in<=8'h0a;en_data_in<=0;
    #17             res<=1;
    #30             en_data_in<=1;
    #400000         en_data_in<=0;

    #150000         $stop;
end

always #5 clk<=~clk;

endmodule

仿真波形:

image-20211123221320720

en_data_in 为 1 时,使能发送,下一周期立马开始发送,同时忙信号为 1。

image-20211123221549931

开始发送后,缓冲寄存器为{1,data,0},以 0 开始,发送 data 后,以 1 结束。

data 是 8’h0a 即 8’b00001010,右移发送,发送顺序是 01010000。

发送 data 结束后,TX=1,同时忙信号为 0。

如果改成发送的 data 为:8‘h7f

image-20211123222824321

可以清楚看到 0_1111_1110_1

如果 data 为:8’h55

image-20211123223201533

也可以看到 0_1010_1010_1

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值