verilog中几种实现计数器的方法

1、if语句实现计数器

module counter (

input clk,

output reg [3:0] count

);

always @(posedge clk)

begin

if (count == 4’hF)

begin

count <= 4’h0;

end

else

begin

count <= count + 4’b1;

end

end

endmodule

2、 for 循环语句实现计数器

integer      i ;

reg [3:0]    counter2 ;

initial begin

    counter2 = 'b0 ;

    for (i=0; i<=10; i=i+1) begin

        #10 ;

        counter2 = counter2 + 1'b1 ;

    end

End

3、while语句实现计数器

module test ;

      reg [3:0]    counter ;

    initial begin

        counter = 'b0 ;

        while (counter<=10) begin

            #10 ;

            counter = counter + 1'b1 ;

        end

    end

  

   //stop the simulation

    always begin

        #10 ;  if ($time >= 1000) $finish ;

    end

  

Endmodule

4、 repeat 循环语句实现计数器

reg [3:0]    counter3 ;

initial begin

    counter3 = 'b0 ;

    repeat (11) begin  //重复11次

        #10 ;

        counter3 = counter3 + 1'b1 ;

    end

End

//、for语句打印出count的值。

module for_example;

  reg [7:0] count;

  initial

    begin

      count = 0;

      for(count = 0; count < 8; count = count + 1)

        $display("count = %d", count);

    end

Endmodule

//while语句打印出count的值。

module while_example;

  reg [7:0] count;

  initial

    begin

      count = 0;

      while(count < 8)

        begin

          $display("count = %d", count);

          count = count + 1;

        end

    end

endmodule

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值