5

我想知道如何为滴答计数器编写一个verilog程序。当快速输入为低电平时,输出滴答为高电平,每 150 ms 一个周期(每 7500000 个周期) clk 周期为 20ns。如果快速输入为高电平,则滴答应每隔一个时钟周期变为高电平一个周期。

我在想我应该计算 clk 周期并在满足周期数时使用该计数来输出最高的滴答声,但我似乎无法让它工作。

这是我的代码:

module tick_counter(
  input  clk,
  input  reset,
  input  fast,
  output reg tick
);

reg count;

always @(posedge clk) begin
  count <= count + 1;
  if((fast == 1)&&(count == 2)) begin
    tick <= 1;
  end
  else if(fast == 0)&&(count == 7500000)) begin
    tick <= 1;
  end
end
endmodule
4

2 回答 2

4

您的计数器只有 1 位宽,您没有包含重置,您也没有在需要时将计数器归零。==2 将只是 == 7500000 的相移。尝试:

module tick_counter(
  input  clk,
  input  reset,
  input  fast,
  output reg tick
);

reg [22:0] count;

always @(posedge clk or negedge reset) begin
  if (~reset) begin
    count <= 'd0;
    tick  <=   0;
  end
  else begin
    if((fast == 1)&&(count == 2)) begin
      tick  <= 1;
      count <= 'd0;
    end
    else if(fast == 0)&&(count == 7500000)) begin
      tick  <= 1;
      count <= 'd0;
    end
    else begin
      tick  <= 0;
      count <= count + 1;
    end
  end
end
endmodule

或者类似以下的东西可能会合成更小:

reg  [22:0] count;

wire [22:0] comp = (fast) ? 23'd2: 23'd7500000 ;
wire        done = count >= comp               ;

always @(posedge clk or negedge reset) begin
  if (~reset) begin
    count <= 'd0;
    tick  <=   0;
  end
  else begin
    if(done) begin
      tick  <= 1;
      count <= 'd0;
    end
    else begin
      tick  <= 0;
      count <= count + 1;
    end
  end
end
于 2012-09-06T09:40:55.327 回答
0

更少的门 - 没有比较器 - 只需使用递减计数器:

module tick_counter(  
  input  wire clk,  
  input  wire resetn,  
  input  wire fast,  
  output reg  tick);  

  reg  [22:0] count;  

  wire [22:0] load = (fast) ? 23'd2: 23'd7500000;  
  wire        done = !count;  

  always @(posedge clk or negedge resetn) begin  
    if (!resetn) begin  
      count <= 23'd0;  
      tick  <= 1'b0;  
    end else begin  
      tick  <= 1'b0;  
      count <= count - 23'd1;  
      if(done) begin  
        tick  <= 1'b1;  
        count <= load;  
      end  
    end  
  end  
endmodule//tick_counter  

否则,如果您更喜欢向上计数器,请反转文字。

于 2017-03-28T01:37:30.310 回答