2014-02-18 160 views
2

我想要得到一个三角形波形,但我的代码不起作用!我认为,“如果条件”是有组织错了,但我无法找到我的波上升像它应该是错误的,它实现了顶级三角形波形verilog

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

详细阐述“不起作用”。 – toolic

+1

你看到'out2'后面有'00-> 01-> ...-> FE-> FF-> 00-> 01 - > ..'模式,但期待00-> 01 - > ...-> FE-> FF-> FE - > .. 01-> 00-> 01 - > ..,对吗? – Greg

+0

@Greg - 是的你是对的 – PYPL

回答

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

我得到了一个语法错误atif(out2 = 8'b11111111),它应该是如果(out2 == 8'b11111111),但它的工作!谢谢 – PYPL

+0

我更新了示例以反映您的更正。 – Russell

1

if(out2<=8'b11111111)条件后,由90°落下一直在评估为真。这是因为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])条件被声明是更常用于堆积阵列(以前称为载体)然后大端([0:7]),http://en.wikipedia.org/wiki/Endianness

工作例如:http://www.edaplayground.com/x/4_b

+0

你在edaplayground上的例子颠倒了'out2'的顺序,并且在模块中使用了'[7:0]',而在测试平台中使用了'[0:7]'。 – nguthrie

+0

@nguthrie固定。 – Greg