【问题标题】:Pulse counter in verilogVerilog中的脉冲计数器
【发布时间】:2017-10-04 17:43:04
【问题描述】:

我正在尝试构建一个脉冲,该脉冲在 8 个时钟脉冲时变为高电平,在休息时变为低电平。因此,当启用和时钟为高电平时,脉冲变为高电平,而在 8 个时钟脉冲后变为低电平。我如何在 verilog 中实现和处理这个问题。到目前为止,这是我所做的。

module clkgenerator(
  input clk,
  input [3:0] count = 4'b0,
  input enable,
  output andpulse
   );
  always@(posedge enable and posedge clk)
  begin
    andpulse <= 1;
      if(count == 4'b1000);
        andpulse <= 0;
        count <= 4'b0;
      else
        count <= count + 1;
      end 
  endmodule

但这会引发错误

错误:C:\altera\14.0\clkgenerator.v(3): near "=": 语法错误, 意外'=',期待')'

需要帮助。

【问题讨论】:

  • 这是一个正确的信息,你不能初始化模块端口。

标签: verilog hdl vlsi


【解决方案1】:

此代码在检测到enable 的上升沿时生成 8 个时钟周期的 HIGH 输出(这似乎是问题所要求的)。

从您的问题描述、逻辑和尝试中可以清楚地看出,您不需要 count 作为输入(您无法初始化输入,因为它们是从模块外部触发的)。正如@Mortada 所说,您不应将enable 放在always 块的灵敏度列表中,最好在always 块内检测enable 信号的上升沿。 enable 的上升沿意味着 => enable 之前的值是 0,现在是 1。此外,您应该使用初始块来初始化您的寄存器。所以下面的代码应该没问题:

module clkgenerator(
   input clk,
   input enable,
   output reg andpulse
); 

reg [3:0] count;
reg previous_enable; //stores previous value of enable (1 clock earlier) 
reg pulse_enable; //enables the pulse if positive edge of enable is detected
initial // this block is used to initialize the registers
begin
  count <= 4'b0000;
  andpulse <= 1'b0;
  pulse_enable <= 1'b0;
  previous_enable <= 1'b0;
end
always@(posedge clk)
begin
if(enable > previous_enable) //if enable > previous_enable it means positive edge was detected
     pulse_enable <= 1'b1; //makes if condition that generates the pulse True for 8 clock cycles
if(pulse_enable)
begin
  andpulse <= 1;
  if(count == 4'b1000)
  begin
    andpulse <= 0;
    count <= 4'b0;
    pulse_enable <= 1'b0;
  end
  else
    count <= count + 1;
end
else
  count <= 1'b0;

previous_enable <= enable; //to be used in next stage
end //end of always block
endmodule

//This code is error free

【讨论】:

    【解决方案2】:

    试试这个: 改变

       module clkgenerator(
      input clk,
      input [3:0] count = 4'b0,
      input enable,
      output andpulse
       );
    

    到:

    module clkgenerator(clk,count, enable, andpulse);
    input clk, enable;
    input [3:0] count = 4'b0000;
    output andpulse;
    

    不确定这是否可行。

    【讨论】:

    • 不,这行不通。您也不能使用旧的端口语法初始化 input
    【解决方案3】:
    1. 您必须将 count 和 andpulse 声明为寄存器:

      module clkgenerator(
         input clk,
         input reg [3:0] count = 4'b0,
         input enable,
         output reg andpulse
      ); 
      
    2. 您不应该将 enable 放在 always 块的敏感度列表中。将其置于 if 条件中:

      always@(posedge clk)
      if(enable)
      begin
        andpulse <= 1;
        if(count == 4'b1000)
        begin
          andpulse <= 0;
          count <= 4'b0;
        end
        else
          count <= count + 1;
        end
      endmodule
      

    【讨论】:

    • 您的第一点给出了错误“端口模式与声明不兼容:计数”。不知道这是什么意思。
    • Verilog 不支持输入的默认值。 input [3:0] count = 4'b0, 需要更改为 input [3:0] count,。也不能分配输入。
    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2022-11-10
    • 2018-07-28
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多