总和:状态机名为“ WaitS2 ”的部件仅会使“ 计数”增加一次。
我正在尝试在 systemverilog 中控制超声传感器 HC-SR04 。正如我在数据表中看到的那样,此传感器创建一个信号“ 触发”,该信号创建声音,并且在生成声音传感器后创建一个逻辑“ 回声”,我需要对时间,以便我创建可以在代码中看到的状态机,但是问题是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”的状态
答案 0 :(得分:2)
您在count++;
中使用always_comb
创建了一个异步反馈循环。您需要注册count
。
此外,trig
和distance
当前是级别敏感的锁存器。 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