在Verilog中添加延迟

时间:2016-09-20 03:04:31

标签: timer verilog delay

我正在使用verilog中的秒表和反应计时器。我的秒表工作,但我遇到了反应计时器的一部分问题。目标是点击一个按钮,随后一段时间后,一个LED开启并启动计时器,你自己计时在led开启后停止计时器需要多长时间。无论如何,当我按下反应计时器开始按钮时(当Cen == 2' b10)时,我就会这样,计时器开始计时。我想知道如何在按钮按下和定时器启动之间添加延迟。你现在可以看到当Cen == 2&#b; b10时,我试图延迟使用寄存器" count,"但它似乎没有用。我现在只是试着做一个固定的延迟,然后我会稍后实现随机发生器,但延迟使用我的"计数"方法不起作用。有任何想法吗?如果您需要任何澄清,请告诉我。谢谢!

module Counter4dig(
    input [1:0] Cen,
    //input incIn,
    input clk, rst, inc,
    output reg[3:0] Dig0,
    output reg[3:0] Dig1,
    output reg[3:0] Dig2, 
    output reg[3:0] Dig3
);

reg ReactionCounter;
reg RandomValue;
reg [30:0] count = 1'b0;

always @ (posedge(clk), posedge(rst))
begin 

if (rst == 1'b1)begin
        Dig0 <= 4'b0000;
        Dig1 <= 4'b0000;
        Dig2 <= 4'b0000;
        Dig3 <= 4'b0000;
    end 

//increment if inc
else if(inc == 1'b1) 
begin
   Dig0 <= Dig0 + 1'b1;

   if(Dig0 == 4'b1001)
   begin
       Dig0 <= 4'b0000;

       //add 1 to second digit (when first resets) up till 9
       Dig1 <= Dig1 + 1'b1;
   end
   //reset if == 10
   if(Dig1 == 4'b1001 && Dig0 == 4'b1001)
   begin
       Dig1 <= 4'b0000;

       //add 1 to third digit (when second reset) up till 9
       Dig2 <= Dig2 + 1'b1;
   end
   //reset if == 10
   if(Dig2 == 4'b1001 && Dig1 == 4'b1001 && Dig0 == 4'b1001)
   begin
       Dig2 <= 4'b0000;

       //add 1 to fourth digit (when third reset) up till 9
       Dig3 <= Dig3 + 1'b1;
   end

   //reset if == 10
   if(Dig3 > 4'b1001)
   begin
       Dig3 <= 4'b0000;
   end            

end

else if (Cen == 2'b10)
begin
  if (count != 50000)
    count <= count + 1;

  else
  begin
    Dig0 <= Dig0 + 1'b1;

       if(Dig0 == 4'b1001)
       begin
           Dig0 <= 4'b0000;

           //add 1 to second digit (when first resets) up till 9
           Dig1 <= Dig1 + 1'b1;
       end
       //reset if == 10
       if(Dig1 == 4'b1001 && Dig0 == 4'b1001)
       begin
           Dig1 <= 4'b0000;

           //add 1 to third digit (when second reset) up till 9
           Dig2 <= Dig2 + 1'b1;
       end
       //reset if == 10
       if(Dig2 == 4'b1001 && Dig1 == 4'b1001 && Dig0 == 4'b1001)
       begin
           Dig2 <= 4'b0000;

           //add 1 to fourth digit (when third reset) up till 9
           Dig3 <= Dig3 + 1'b1;
       end

       //reset if == 10
       if(Dig3 > 4'b1001)
       begin
           Dig3 <= 4'b0000;
       end    
end
end

//only continue if Cen is 01 & not inc
else if(Cen == 2'b01)
begin   

    //add 1 to first digit up till 9
    Dig0 <= Dig0 + 1'b1;

    //reset if == 10    
        if(Dig0 == 4'b1001)
        begin
            Dig0 <= 4'b0000;

            //add 1 to second digit (when first resets) up till 9
            Dig1 <= Dig1 + 1'b1;
        end
            //reset if == 10
            if(Dig1 == 4'b1010)
            begin
                Dig1 <= 4'b0000;

                //add 1 to third digit (when second reset) up till 9
                Dig2 <= Dig2 + 1'b1;
            end
                //reset if == 10
                if(Dig2 == 4'b1010)
                begin
                    Dig2 <= 4'b0000;

                    //add 1 to fourth digit (when third reset) up till 9
                    Dig3 <= Dig3 + 1'b1;
                end

                    //reset if == 10
                if(Dig3 > 4'b1001)
                begin
                    Dig3 <= 4'b0000;
                end         
    end 
//end

endmodule

1 个答案:

答案 0 :(得分:0)

您指定reg [30:0]计数= 1&#39; b0;首先,您只分配一个位,而不是31位。使用31&#39; b0。接下来,您将使用阻塞赋值在always语句之外分配计数,然后在语句内部使用非阻塞赋值。我不确定这是否可以合成。

当按钮按下发生然后开始计数时,你是否需要将计数器重置为零才是公平的?假设在按下按钮时它会继续计数,你可以试试这个。

reg[1:0] Cen_d;  // Used to capture the Cen for use in a making a pulse 
reg      Cen_pulse;
generator

always @ (posedge clk)
begin 
   Cen_d <= Cen; // capture the old Cen value
   Cen_pulse <= Cen == 2'b10 & (Cen != Cen_d); // Look for Cen == 2'b01 and
                                               // the old Cen (in Cen_d) is
                                               // is not current Cen.  If so
                                               // pulse once.
end

always @ (posedge clk or posedge reset)
begin 
   if (reset)
      count <= 31'h0000_0000;
   else
      begin 
      count = Cen_pulse      ? 31'h0000_0000 : // Return to zero on pulse
              count >= 50000 ? 31'd500000    : // Stop counting at 50000
              count = count + 1;               // Otherwise increment
      end
end