Verilog学习笔记HDLBits——Counters

本文档介绍了使用Verilog语言设计不同类型的计数器,包括二进制计数器、十进制计数器、带有使能和复位控制的计数器、BCD计数器以及12小时时钟计数器。通过实例化和逻辑门组合,实现了计数器的同步复位、使能控制以及频率分频等功能。同时,对于12小时时钟计数器,还考虑了AM/PM的切换。这些计数器的设计展示了在数字逻辑设计中如何处理计数和控制信号的交互。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

一、Counters

1. Four-bit binary counter

Practice:Build a 4-bit binary counter that counts from 0 through 15, inclusive, with a period of 16. The reset input is synchronous, and should reset the counter to 0.
翻译:构建一个4位二进制计数器,从0到15(包括15),周期为16。复位输入是同步的,应该将计数器复位为0。
在这里插入图片描述

Solution(不唯一,仅供参考):

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

Timing Diagram
在这里插入图片描述

2. Decade counter

Practice:Build a decade counter that counts from 0 through 9, inclusive, with a period of 10. The reset input is synchronous, and should reset the counter to 0.
翻译:同样是 4bit 计数器,本题只计数到 0~9,周期(period)为 10,同步复位且复位为 0。时序图如下:
在这里插入图片描述

Solution(不唯一,仅供参考):

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

Timing Diagram
在这里插入图片描述

3. Decade counter again

Practice:Make a decade counter that counts 1 through 10, inclusive. The reset input is synchronous, and should reset the counter to 1.
翻译:本题与上题类似,只需计数1-10,复位到1.
在这里插入图片描述

Solution(不唯一,仅供参考):

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

Timing Diagram
在这里插入图片描述

4. Slow decade counter

Practice:Build a decade counter that counts from 0 through 9, inclusive, with a period of 10. The reset input is synchronous, and should reset the counter to 0. We want to be able to pause the counter rather than always incrementing every clock cycle, so the slowena input indicates when the counter should increment.

翻译:设计一个 0~9 的计数器,共 10 个周期。该计数器采用同步复位且复位为 0。但是本题是希望该计数器并不是随着 clk 的变化而递增,而是随着一个 slowena 使能信号来控制增加。时序图如下图所示:
在这里插入图片描述
Solution(不唯一,仅供参考):

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

Timing Diagram
在这里插入图片描述
在这里插入图片描述

5. Counter 1-12

Practice:Design a 1-12 counter with the following inputs and outputs:
Reset: Synchronous active-high reset that forces the counter to 1
Enable:Set high for the counter to run
Clk :Positive edge-triggered clock input
Q[3:0] :The output of the counter
c_enable, c_load, c_d[3:0] :Control signals going to the provided 4-bit counter, so correct operation can be verified.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值