侧边栏壁纸
    • 累计撰写 296 篇文章
    • 累计收到 520 条评论
    【6】Verilog练习-三角波梯形波发生器
    我的学记|刘航宇的博客

    【6】Verilog练习-三角波梯形波发生器

    刘航宇
    2022-06-02 / 0 评论 / 406 阅读 / 正在检测是否收录...

    三角波发生器理论

    Test

    代码

    //三角波发生器
    `timescale 1ns/10ps
    module tri_gen(
                    clk,
                    res,
                    d_out
                    );
    input            clk;
    input            res;
    output[8:0]        d_out;
    reg                state;
    reg[8:0]        d_out;
    always@(posedge clk or negedge res)
    if(~res) begin
        state<=0;d_out<=0;
    end
    else begin
                    case(state)
                    0://上升
                    begin
                      d_out<=d_out+1;
                      if(d_out==299)begin
                       state<=1;
                      end
                    end
                    1://下降
                    begin
                      d_out<=d_out-1;
                      if(d_out==1)begin
                        state<=0;
                      end
                    end
                    endcase
    end
    endmodule
    //------testbench of tri_gen----
    module tri_gen_tb;
    reg                clk,res;
    wire[8:0]        d_out;
    tri_gen U1(
                    .clk(clk),
                    .res(res),
                    .d_out(d_out)
                    );
    initial begin
                    clk<=0;res<=0;
            #17        res<=1;
            #8000    $stop;
    end
    always #5 clk<=~clk;
    endmodule

    现象

    Test

    梯形波发生器

    //三角波发生器->改进为梯形波
    `timescale 1ns/10ps
    module tri_gen(
                    clk,
                    res,
                    d_out
                    );
    input            clk;
    input            res;
    output[8:0]        d_out;
    reg[1:0]        state;//3个状态2位,不在是原来的1bit
    reg[8:0]        d_out;
    reg[7:0]        con;//计数器,记录平顶周期个数
    always@(posedge clk or negedge res)
    if(~res) begin
        state<=0;d_out<=0;con<=0;
    end
    else begin
                    case(state)
                    0://上升
                    begin
                      d_out<=d_out+1;
                      if(d_out==299)begin
                       state<=1;
                      end
                    end
                    1://平顶
                    begin
                      if(con==200) begin
                        state<=2;
                        con<=0;
                      end
                      else begin
                         con<=con+1;
                      end
                    end
                    2://下降
                    begin
                      d_out<=d_out-1;
                      if(d_out==1)begin
                        state<=0;
                      end
                    end
                    default://3状态
                    begin
                      state<=0;
                      con<=0;
                    end
                    endcase
    end
    endmodule
    //------testbench of tri_gen----
    module tri_gen_tb;
    reg                clk,res;
    wire[8:0]        d_out;
    tri_gen U1(
                    .clk(clk),
                    .res(res),
                    .d_out(d_out)
                    );
    initial begin
                    clk<=0;res<=0;
            #17        res<=1;
            #20000    $stop;
    end
    always #5 clk<=~clk;
    endmodule

    现象

    3
    【7】Verilog练习-串口数据接收
    « 上一篇 2022-06-15
    【5】Verilog练习-相邻16点相加
    下一篇 » 2022-05-25

    评论 (0)

    取消