0

我想制作一个可以在特定信号的位姿开始计数的计数器。一旦计数到 256,停止计数,将计数器设置为 0 并输出一些东西。

我编写了以下代码,但显然它会导致“多驱动程序”错误。

但我想不出别的办法。有人可以提供一个想法吗?

always@(posedge a_specific_signal)
begin
    counter_switch <= 1;
end

always@(posedge clk)
begin
if (counter_switch  == 1)
    counter <= counter + 1;
if (counter == 256)  
    begin   
        counter <= 0;  
        counter_switch <= 0; 
        something <= 1; 
    end
end
4

3 回答 3

1

您不能更改counter_switch两个不同always语句中的值。您需要在同always一块内执行此操作。

我会做这样的事情:(没有测试代码可能包含错误)

input clock, reset;

reg counter_switch, ready;
reg [7:0] counter;

always@(posedge clock) begin
    if (reset) begin
        counter_switch <= 0;
    end else begin 
        if (a_specific_signal) begin 
            counter_switch <= 1;
        end else if (ready) begin
            counter_switch <= 0;
        end 
    end 
end

always@(posedge clock) begin
    if (reset) begin 
        ready <= 0;
        counter <= 0;
    end else begin
        if (counter_switch) begin 
            if (counter == 255) begin   
                counter <= 0;  
                ready <= 1; 
            end else begin 
                counter <= counter + 1;
            end
        end else begin 
            ready <= 0;
        end
    end
end
于 2018-06-08T08:22:29.720 回答
0

“多驱动程序”错误是由于尝试修改来自不同进程/始终块的信号而引起的。你不应该这样做。

我习惯于使用完全同步的系统,所以我应该问你“a_specific_signal”是什么样的,以及与你的主时钟的关系是什么。

因此,我的方法是:1. 将“a_specific_signal”同步到我当前的时钟,2. 检测其正沿,3. 将其用作计数器标志的驱动器

reg a_spec_signal_reg0 = 1'b0, a_spec_signal_reg1 = 1'b0, a_spec_signal_reg2 = 1'b0;

always @(posedge clk) begin --synchronizer with 3 FFs
  a_spec_signal_reg0 <= a_specific_signal;
  a_spec_signal_reg1 <= a_spec_signal_reg0;
  a_spec_signal_reg2 <= a_spec_signal_reg1;
end

wire a_spec_signal_posedge;
assign a_spec_signal_posedge = a_spec_signal_reg1 & ~(a_spec_signal_reg2);

reg counter_switch = 1'b0;

always @(posedge clk) begin
  something <= 1'b0;
  if (a_spec_signal_posedge)
      counter_switch <= 1'b1;
  if (counter_switch) begin
      counter <= counter + 1;
      if (counter == 255) begin   
          counter <= 0;  
          counter_switch <= 0;
          something <= 1'b1;
      end
  end
end

一些注意事项:我假设您想要计算 256 个事件,这意味着使用从 0 到 255(8 位)的计数器。另外,something 信号在其默认状态下设置为 0,因此当条件“counter == 255”到达时,它仅输出一个时钟周期的滴答声,这是通常使用的。

于 2018-06-24T23:10:58.250 回答
0

这是您的计数器,用于0 to 256计数。

module counter (clk, reset, op);
  input logic clk, reset;
  output logic [16:0] op;

  logic [16:0] current_cnt, next_cnt;

  assign next_cnt = (current_cnt[16]) ? 17'd0 : (current_cnt + 17'd1);
  assign op = current_cnt;

  always @ (posedge clk, negedge reset)
  begin
    if (~reset)
      current_cnt <= 17'd0;
    else 
      current_cnt <= next_cnt;
  end
endmodule

因此,每当您达到 256 时,current_cnt 的第 17 位将为 1,这意味着您的 next_cnt 应为 0。在所有其他情况下,您的下一个计数应为 +1。

于 2018-06-13T05:17:09.020 回答