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.

hdlbits平台上关于顺序计数器的相关内容丰富,涵盖多种类型的计数器题目及对应代码。 在计数器类型方面,有四位二进制计数器(Count15)、十进制计数器(Count 10)、从1到10的十进制计数器(Count1to10)、慢速十进制计数器(countslow)、1 - 12计数器(Counter 1 - 12)、1000计数器(Counter 1000)、4位十进制计数器(Countbcd)以及12小时时钟计数器等[^1]。 以下是部分计数器的代码示例: - **十进制计数器(Count 10)**: ```verilog module top_module ( input clk, input slowena, input reset, output [3:0] q ); always @(posedge clk) begin if (reset) begin q <= 'd0; end else if (q == 'd9 && slowena) begin q <= 'd0; end else if (q < 'd9 && slowena) begin q <= q + 'd1; end end endmodule ``` 该代码在时钟上升沿触发操作,当复位信号有效时,计数器清0;当计数值达到9且使能信号有效时,计数器也清0;当计数值小于9且使能信号有效时,计数器加1[^2]。 - **4位十进制计数器(Countbcd)**: ```verilog module top_module ( input clk, input reset, output OneHertz, output [2:0] c_enable ); // reg [3:0] q0, q1, q2; assign c_enable[0] = 1; assign c_enable[1] = (q0 == 9) ? 1 : 0; assign c_enable[2] = (q0 == 9) && (q1 == 9) ? 1 :0; assign OneHertz = (q0 == 9 && q1 == 9 && q2 == 9); bcdcount counter0 (clk, reset, c_enable[0], q0/*one*/); bcdcount counter1 (clk, reset, c_enable[1], q1/*ten*/); bcdcount counter2 (clk, reset, c_enable[2], q2/*onehundred*/); endmodule ``` 此代码中,通过组合逻辑生成不同位的使能信号,当三个计数器都计到9时,输出1Hz信号,同时调用bcdcount模块实现计数功能[^3]。 - **1 - 12计数器(Counter 1 - 12)**: ```verilog module top_module ( input clk, input reset, input enable, output [3:0] Q, output c_enable, output c_load, output [3:0] c_d ); // assign c_enable = enable; assign c_d = 4'b0001; assign c_load = reset | (enable & (Q==4'b1100)); count4 the_counter ( .clk(clk), .enable(c_enable), .load(c_load), .d(c_d), .Q(Q) ); endmodule ``` 该代码围绕count4模块构建1 - 12计数器,根据复位信号和计数值来控制加载信号,确保计数器从1开始计数,当计到12或复位时重新加载初始值[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值