我正在尝试构建一个脉冲,该脉冲在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&#34; =&#34;:语法错误, 意外的&#39; =&#39;,期待&#39;)&#39;
需要帮助。
答案 0 :(得分:1)
此代码在检测到enable
的上升沿时会生成8个时钟周期的HIGH输出(这似乎是问题所在)。
从问题描述,逻辑和尝试显而易见,您不需要count
作为输入(您无法初始化输入,因为它们是从模块外部触发的)。就像@Mortada所说的那样,您不应该将enable
放在Always Block的灵敏度列表中,最好在Always Block中检测enable
信号的上升沿。 enable
的上升沿意味着=> enable
的先前值为0,现在为1。此外,您应该使用一个初始块来初始化寄存器。因此,以下代码应该可以: / p>
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
答案 1 :(得分:0)
您必须将count和andpulse声明为寄存器:
module clkgenerator(
input clk,
input reg [3:0] count = 4'b0,
input enable,
output reg andpulse
);
您不应将启用设置在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
答案 2 :(得分:-1)
试试这个: 变化
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;
不确定这是否有效。