2014-02-18 161 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