下列代码有什么错误:always @(posedge clk or negedge rst) begin if(rst == 1'b0) begin pwm<=2'b00; pwm1<=2'b00; pwm2<=2'b00; pwm3<=2'b00; run <= 8'b00000000; end else if(dt < 20'd32) pwm<=2'b01; pwm1<=2'b00; pwm2<=2'b00; pwm3<=2'b00; run <= 8'b10000000; else if(dt < 20'd128) pwm<=2'b01; pwm1<=2'b00; pwm2<=2'b00; pwm3<=2'b00; run <= 8'b10100000; else if(dt < 20'd256) pwm<=2'b10; pwm1<=2'b00; pwm2<=2'b00; pwm3<=2'b00; run <= 8'b10101000; else pwm<=2'b11; pwm1<=2'b00; pwm2<=2'b00; pwm3<=2'b00; run <= 8'b10101010; end
时间: 2024-02-02 12:03:00 浏览: 57
基于vivado2019.2的交通灯模拟系统verilog开发
5星 · 资源好评率100%
这段代码有一个常见的错误:在第 10 行到第 22 行的 if 语句中,每一个 if 语句后面只有一行代码被执行,其他的代码都没有被包含在 begin-end 块中。
正确的做法是,在每一个 if 语句后面添加一个 begin-end 块,例如:
```
else if(dt < 20'd32) begin
pwm<=2'b01;
pwm1<=2'b00;
pwm2<=2'b00;
pwm3<=2'b00;
run <= 8'b10000000;
end
```
这样才能保证 if 语句中的所有代码都被正确地执行。
阅读全文