打印

module tri_gen

[复制链接]
9|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
34af9uc|  楼主 | 2025-6-17 23:50 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
/************最简单状态机, 三角波发生器**************/
/**************************************************/
/**************************************************/
/******************20250614************************/
`timescale 1ns/10ps
module tri_gen
                                (
                                clk,
                                res,
                                d_out
                                );

input                        clk;
input                        res;
output[8:0]                d_out;
               

reg[2:0]                state;                //主状态机寄存器;
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                <=3;
        end
end
/**************************************************/
3:                //平底
begin
if(con ==200)
        begin
        state                <=0;
        con                        <=0;
        end
else
        begin
        con                        <=con +1;
        end
end
/**************************************************/
                endcase
        end
endmodule
/****************testbench oftri_gen***************/
module tri_gen_tb;
reg                                clk, res;
wire[8:0]                d_out;
tri_gen tri_gen
                                (
                                .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
/**************************************************/
/**************************************************/
/**************************************************/


屏幕截图 2025-06-17 234146.png (19.47 KB )

屏幕截图 2025-06-17 234146.png

tri_gen1.zip

751 Bytes

使用特权

评论回复

相关帖子

发新帖
您需要登录后才可以回帖 登录 | 注册

本版积分规则

310

主题

477

帖子

63

粉丝