【问题标题】:increment operation in Systemverilog Vivado not working as expectedSystemverilog Vivado 中的增量操作未按预期工作
【发布时间】:2019-07-18 19:42:28
【问题描述】:

Sum:状态机名为“WaitS2”的部分仅将“count”递增一次。

我正在尝试在 systemverilog 中控制超声波传感器 HC-SR04。正如我在数据表中看到的那样,这个传感器会创建一个信号“trigger”来产生声音,并且在产生声音传感器后会创建逻辑“echo”,我需要计算时间,所以我创建了你可以在代码中看到的状态机,但问题是 count++ 没有像预期的那样工作,它只会增加一次 count 变量,不管回显信号有多长

我使用了来自互联网的另一个名为 32 位加法器的模块,它没有改变任何东西。 我将所有语句更改为非块语句它不起作用。 我什至尝试将count++ 更改为count = count + 1 不起作用

module sensorFSM(
    input logic echo , clk ,reset, 
    output logic trig,
    output logic[31:0] distance,
    output logic[1:0] presentState
    );
    /*typedef enum logic[1:0] {BeginS , WaitS , ReturnS } states;
    states presentState , nextState;
    */
    logic[31:0] count , count1; 
    logic[1:0] BeginS , WaitS, WaitS2 , ReturnS  , nextState;
    assign BeginS = 2'b00;
    assign WaitS = 2'b01; 
    assign WaitS2 = 2'b10;
    assign ReturnS = 2'b11; 

    // clk and state change
    always_ff @( posedge clk )
        begin
            if ( reset == 1'b1 )
            presentState <= BeginS;
            else
            presentState <= nextState;
        end

    // real state changes    
    always_comb 
        begin            
            case(presentState)           
            BeginS:
                begin
                    trig = 1'b1;
                    count = 32'b0;
                    nextState = WaitS;
                end
            WaitS:
                begin
                    trig = 1'b0;
                    distance = 32'b0;
                    //#5000;
                    nextState = WaitS2;
                end
            WaitS2:
                begin
                    if( echo  == 1 )
                      begin
                        if ( count < 24'b101100111000000100100000 )
                            begin 
                            // here is the problem count is only incrementing 
                            //once
                                count++;                               
                                nextState = WaitS2; 
                            end 
                         else 
                            begin
                                distance = count;
                                nextState = BeginS;
                            end 
                      end
                    else // echo == 0   
                        begin
                            nextState = ReturnS;
                        end
                   end 
            ReturnS:
                begin
                    //count =  count / 1470;
                    distance = count;
                    nextState = BeginS;
                end
            default:
                nextState = BeginS;
            endcase
         end  
endmodule

我预计模拟的计数大约为 1 miliion,但它始终输出 1,但我可以看到名为“WaitS2”的状态在 echo 也处于活动状态时存在很长时间

【问题讨论】:

    标签: verilog system-verilog vivado


    【解决方案1】:

    您创建了一个异步反馈循环,其中 count++;always_comb 内。您需要注册count

    此外,trigdistance 目前是推断的电平敏感锁存器。 distance 需要失败。 trig 可以写成纯组合逻辑,但由于它是一个输出,我强烈建议让它成为一个触发器,以消除输出毛刺的上升。

    always_ff @( posedge clk )
        begin
          if ( reset == 1'b1 ) begin
            presentState <= BeginS;
            trig <= 0; // <-- reset
            distance <= 0; // <-- reset
            count <= 0; // <-- reset
          end else begin
            presentState <= nextState;
            trig <= next_trig; // <-- flop trig
            distance <= next_distance; // <-- flop distance
            count <= next_count; // <-- flop count
          end
        end
    
    // real state changes    
    always_comb 
        begin
          next_trig = trig; // <-- default value is flopped value
          next_distance = distance; // <-- default value is flopped value
          next_count = count; // <-- default value is flopped value
            case(presentState)           
            BeginS:
                begin
                    //trig = 1'b1; // <-- is now a flop assigned in always_ff
                    //count = 32'b0; // <-- is now a flop assigned in always_ff
                    next_trig = 1'b1; // <-- update value
                    next_count = 32'b0; // <-- update value
                    nextState = WaitS;
                end
            WaitS:
                // ... replace trig/distance with next_trig/next_distance in here ...
            WaitS2:
                begin
                  // ...
                  //count++;  // <-- NOPE would be asynchronous feedback
                  next_count = count + 1; // <-- synchronous increment
                  // ...
                end
            ReturnS:
                //  ... replace distance with next_distance in here ...
            default:
                nextState = BeginS;
            endcase
         end  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2021-07-18
      • 2011-05-19
      相关资源
      最近更新 更多