0

我正在尝试建立一个脉冲,该脉冲在 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):靠近“=”:语法错误,意外'=',期待')'

需要帮忙。

4

3 回答 3

1

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

从您的问题描述、逻辑和尝试中可以清楚地看出,您不需要count作为输入(您无法初始化输入,因为它们是从模块外部触发的)。正如@Mortada 所说,你不应该把enable总是块的灵敏度列表,最好在总是块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
于 2019-01-22T08:44:17.127 回答
0

试试这个:改变

   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;

不确定这是否可行。

于 2017-10-05T03:22:54.963 回答
0
  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
    
于 2017-10-05T09:19:30.383 回答