我正在尝试获取三角形波形,但我的代码不起作用!我认为“如果条件”组织错误,但我找不到错误我的波浪应该是这样的,它在达到顶部后下降了90°
module pila(clk,res,out2);
input clk,res;
output [0:7]out2;
reg [0:7]out2;
always @(posedge clk)
begin
if (res)
begin
if(out2<=8'b11111111)
out2=out2+1;
else if(out2>=8'b00000000)
out2=out2-1;
else out2=8'b00000000;
end
else out2=8'b00000000;
end
endmodule
module testbench;
reg clk,res;
wire [0:7]out2;
pila Sevo(clk,res,out2);
always #2 clk=~clk;
initial
begin
clk=0;res=0;
#2 res=1;
end
initial #5000 $finish;
endmodule
答案 0 :(得分:3)
您需要一些信号来指示您当前正在计算的方向。还使用非阻塞赋值运算符<=
而不是阻塞赋值运算符=
。
module pila(clk,res,out2);
input clk,res;
output [0:7]out2;
reg [0:7]out2 = 8'h00;
reg count_down = 1'b0;
always @(posedge clk)
begin
if (count_down == 1'b0)
begin
if (out2==8'b11111111) // check for top of count
begin
count_down <= 1'b1;
out2<=out2-1;
end
else
out2<=out2+1;
end
else
begin
if(out2==8'b00000000) // check for bottom of count
begin
count_down <= 1'b0;
out2<=out2+1;
end
else
out2<=out2-1;
end
end
endmodule
答案 1 :(得分:1)
if(out2<=8'b11111111)
条件始终评估为true。这是因为out2
范围是0到255.尝试添加另一个触发器来控制方向,例如downup
,其中1表示递减,0表示递增。
if (out2 == 8'h00) begin
downup <= 1'b0; // up
out2 <= 8'h01;
end
else if (out2 == 8'hFF) begin
downup <= 1'b1; // down
out2 <= 8'hFE;
end
else if (downup) begin // down
out2 <= out2 - 1;
end
else begin // up
out2 <= out2 + 1;
end
其他问题:
<=
)。[7:0]
)更常用于打包数组(以前称为矢量),然后是Big-Endian([0:7]
),http://en.wikipedia.org/wiki/Endianness