HDLBITS笔记27:计数器(4位计数器、计算0-9/0-10计数器、具有控制信号的计数器)

目录

题目1:4位二进制计数器(four-bit-binary counter)

题目2:计数器10(decade counter)

 题目3:计数器10(dacade counter again)

 题目4:能够暂停的计数器(slow decade counter)

题目1:4位二进制计数器(four-bit-binary counter)

构建一个 4 位二进制计数器,该计数器的计数范围为 0 到 15(包括 15),周期为 16。复位输入是同步的,应将计数器复位至0。

模块声明

module top_module (
    input clk,
    input reset,      // Synchronous active-high reset
    output [3:0] q);

通过阅读题意可知:计数器的构建是通过计算脉冲个数来计算。其中复位有效事件为最高优先级。

可得代码编写如下:

module top_module (
    input clk,
    input reset,      // Synchronous active-high reset
    output [3:0] q);
    always @(posedge clk)
        begin
            if(reset)
                q <= 4'b0;
            else
               q =q+1'b1; 
        end
endmodule

其中仿真结果如下:

 

题目2:计数器10(decade counter)

构建一个从 0 到 9(含)计数的十计数器,周期为 10。复位输入是同步的,应将计数器复位至0。

模块声明

module top_module (
    input clk,
    input reset,        // Synchronous active-high reset
    output [3:0] q);

分析:这题与上题的不同之处在于这个计数器最多计到9,而题目1最多可算到15。并且该题在算到9的时候计数器重新复位置0.

module top_module (
    input clk,
    input reset,        // Synchronous active-high reset
    output [3:0] q);
    always @(posedge clk)
        begin
            if(reset|q>=4'b1001)
                q <= 4'b0;
            else
                q <= q+1'b1;

        end
endmodule

仿真结果如下:

 题目3:计数器10(dacade counter again)

制作一个十计数器,包括 1 到 10。复位输入是同步的,应将计数器复位至1。

模块声明

module top_module (
    input clk,
    input reset,
    output [3:0] q);

分析:该题是计算到10的计数器,但是当复位时是复位至1.

代码编写如下:

module top_module (
    input clk,
    input reset,
    output [3:0] q);
    always @(posedge clk)
        begin
            if(reset | q>=4'b1010)
                q <= 1'b1;
            else
                q <= q+1'b1;
        end
endmodule

仿真结果如下:

 题目4:能够暂停的计数器(slow decade counter)

构建一个从 0 到 9(含)计数的十计数器,周期为 10。复位输入是同步的,应将计数器复位至0。我们希望能够暂停计数器,而不是总是在每个时钟周期递增,因此 slowena 输入指示计数器何时应该递增。

模块声明

module top_module (
    input clk,
    input slowena,
    input reset,
    output [3:0] q);

提示:这是一个带有使能控制信号的常规十进制计数器

代码编写如下:

module top_module (
    input clk,
    input slowena,
    input reset,
    output [3:0] q);
    always @(posedge clk)
        begin
            if(reset)
                q <= 4'b0;
            else if(slowena)
                begin
                    if(q>=4'b1001)
                q <= 4'b0;
                   else
                q <= q+1'b1;
        end
        end
endmodule

仿真结果如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值