HDLBits 系列(16)Something about Counter

目录

原题复现

题目1

我的设计

题目2

审题

我的设计


原题复现

题目1

一般的计数器我就不说了,这里看下面的要求:

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.

HDLBits 系列(16)Something about Counter_第1张图片

我的设计

也就是计数不仅由时钟控制,还需要判断计数使能是否有效,在这里就是slowena,下面就设计一个计数器技术从0到9,且计数由使能控制。

module top_module (
    input clk,
    input slowena,
    input reset,
    output [3:0] q);
    always@(posedge clk) begin
        if(reset) q <= 0;
        else if(slowena & q < 9) q <= q + 1;
        else if(slowena & q >= 9) q <= 0;
    end

endmodule

HDLBits 系列(16)Something about Counter_第2张图片


题目2

再来一题稍微更难一点的计数器相关的题目:

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.

You have the following components available:

  • the 4-bit binary counter (count4) below, which has Enable and synchronous parallel-load inputs (load has higher priority than enable). The count4 module is provided to you. Instantiate it in your circuit.
  • logic gates
module count4(
	input clk,
	input enable,
	input load,
	input [3:0] d,
	output reg [3:0] Q
);

Module Declaration

module top_module (
    input clk,
    input reset,
    input enable,
    output [3:0] Q,
    output c_enable,
    output c_load,
    output [3:0] c_d
); 

审题

如何设计这么一个计数器呢?

难点在于c_enable, c_load, c_d[3:0三个控制变量,我们先来看看他是什么意思吧。

这个计数器的功能是实现从1到12的计数,因此,如果复位reset有效,则计数复位为1,我们需要做的不是自己设计一个计数器,而是需要例化题目给的一个计数器,题目给的计数器需要有一个输入信号为load信号,我们需要产生一个load信号给予它,何时给予load信号有效呢?

当然是复位信号有效或者计数满(12)且计数使能有效;

因此,我们可以设计如下电路:

我的设计

module top_module (
    input clk,
    input reset,
    input enable,
    output [3:0] Q,
    output c_enable,
    output c_load,
    output [3:0] c_d
); //

   //4-bit计数器的控制信号
    assign c_enable = enable;
    //带复位和置位,
    assign c_load   = reset | (Q == 4'd12 & enable == 1'b1);
  // assign c_d      = c_load ? 4'b1: 4'bx;

    assign c_d      = 4'b1;

    count4 inst_count4
    (
        .clk(clk),
        .enable(c_enable),
        .load(c_load),
        .d(c_d),
        .Q(Q)
    );

endmodule

HDLBits 系列(16)Something about Counter_第3张图片

 

 

 

你可能感兴趣的:(#,HDLBits)