明るさを変えてみる-その2。

シミュレーションではちゃんと波形が出ていたので、変だなぁ変だなぁと思いながらピンアサインを眺めたりしてたんですが。

なぜかいきなりパッとひらめきました。

出力端子の論理が逆じゃん!

指定した時間分だけ出力端子をHにするように書いていたんですが、LEDはLでドライブされるんですね。Brevia2の回路上もそうなってます。
ということで、コードを修正して試してみると、ちゃんと明るさが変わりました。が、変化の度合いが少ないため、クロックをさらに細かくしてデューティを0.01msec単位で変更できるようにしました。

twincle_led.v:

// vim: set fenc=utf-8 filetype=verilog expandtab ts=4 sts=0 sw=4:
//
// generate 1000Hz clock from 50MHz osc input.
//
module LED_TOP(clk, reset,
    LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8);
    input clk;
    input reset;
    output LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8;
    wire c1kout;

    reg [15:0] count1;
    reg [15:0] count2;
    reg [15:0] count3;
    reg [15:0] count4;
    reg [15:0] count5;
    reg [15:0] count6;
    reg [15:0] count7;
    reg [15:0] count8;

    reg [15:0] duty1 = 1;
    reg [15:0] duty2 = 5;
    reg [15:0] duty3 = 10;
    reg [15:0] duty4 = 30;
    reg [15:0] duty5 = 70;
    reg [15:0] duty6 = 100;
    reg [15:0] duty7 = 500;
    reg [15:0] duty8 = 1000;

    reg  _out1;
    reg  _out2;
    reg  _out3;
    reg  _out4;
    reg  _out5;
    reg  _out6;
    reg  _out7;
    reg  _out8;
    
assign LED1 = _out1;
assign LED2 = _out2;
assign LED3 = _out3;
assign LED4 = _out4;
assign LED5 = _out5;
assign LED6 = _out6;
assign LED7 = _out7;
assign LED8 = _out8;

CLK10000HZ c1k(
    .clk(clk),
    .reset(reset),
    .clk1m(c1kout));

// LED1
always @ (posedge c1kout or negedge reset) begin
    if (reset == 0)
    begin
        _out1 <= 0;
        count1 <= 0;
    end else
    if (count1 < 100 - 1) // 10msec周期
    begin
        count1 <= count1 + 1;
        if (count1 < duty1) // デューティ時間はONする
            _out1 <= 0;
        else
            _out1 <= 1;
    end
    else
    begin
        count1 <= 0;
    end
end

// LED2
always @ (posedge c1kout or negedge reset) begin
    if (reset == 0)
    begin
        _out2 <= 0;
        count2 <= 0;
    end else
    if (count2 < 100 - 1) // 10msec周期
    begin
        count2 <= count2 + 1;
        if (count2 < duty2) // デューティ時間はONする
            _out2 <= 0;
        else
            _out2 <= 1;
    end
    else
    begin
        count2 <= 0;
    end
end

// LED3
always @ (posedge c1kout or negedge reset) begin
    if (reset == 0)
    begin
        _out3 <= 0;
        count3 <= 0;
    end else
    if (count3 < 100 - 1) // 10msec周期
    begin
        count3 <= count3 + 1;
        if (count3 < duty3) // デューティ時間はONする
            _out3 <= 0;
        else
            _out3 <= 1;
    end
    else
    begin
        count3 <= 0;
    end
end

// LED4
always @ (posedge c1kout or negedge reset) begin
    if (reset == 0)
    begin
        _out4 <= 0;
        count4 <= 0;
    end else
    if (count4 < 100 - 1) // 10msec周期
    begin
        count4 <= count4 + 1;
        if (count4 < duty4) // デューティ時間はONする
            _out4 <= 0;
        else
            _out4 <= 1;
    end
    else
    begin
        count4 <= 0;
    end
end

// LED5
always @ (posedge c1kout or negedge reset) begin
    if (reset == 0)
    begin
        _out5 <= 0;
        count5 <= 0;
    end else
    if (count5 < 100 - 1) // 10msec周期
    begin
        count5 <= count5 + 1;
        if (count5 < duty5) // デューティ時間はONする
            _out5 <= 0;
        else
            _out5 <= 1;
    end
    else
    begin
        count5 <= 0;
    end
end

// LED6
always @ (posedge c1kout or negedge reset) begin
    if (reset == 0)
    begin
        _out6 <= 0;
        count6 <= 0;
    end else
    if (count6 < 100 - 1) // 10msec周期
    begin
        count6 <= count6 + 1;
        if (count6 < duty6) // デューティ時間はONする
            _out6 <= 0;
        else
            _out6 <= 1;
    end
    else
    begin
        count6 <= 0;
    end
end

// LED7
always @ (posedge c1kout or negedge reset) begin
    if (reset == 0)
    begin
        _out7 <= 0;
        count7 <= 0;
    end else
    if (count7 < 100 - 1) // 10msec周期
    begin
        count7 <= count7 + 1;
        if (count7 < duty7) // デューティ時間はONする
            _out7 <= 0;
        else
            _out7 <= 1;
    end
    else
    begin
        count7 <= 0;
    end
end

// LED8
always @ (posedge c1kout or negedge reset) begin
    if (reset == 0)
    begin
        _out8 <= 0;
        count8 <= 0;
    end else
    if (count8 < 100 - 1) // 10msec周期
    begin
        count8 <= count8 + 1;
        if (count8 < duty8) // デューティ時間はONする
            _out8 <= 0;
        else
            _out8 <= 1;
    end
    else
    begin
        count8 <= 0;
    end
end

endmodule

clk1000hz.vのほうは parameter CNT = 250; に変更。これでやってみたのが下の写真です。


ちゃんと輝度が変わってきていますね。一番上のLEDはフル点灯です。

これを動かしたらKnight 2000のLED Flasherみたいにできるかな、というのが先週のLチカの直後に思い浮かんだので、次はこれをやってみましょう。

ということで巫女さんと遊ぶのはもうちょっと後にしましょう。

0 件のコメント:

コメントを投稿

Windowsでシンボリックリンクを試してみる。

きっかけは、1つのファイルを別の名前で起動したら違う動きになるようなスクリプトを書く、でした。  busybox なんかでは、同じ実行形式ファイルの名前を、lsにすればlsと同じ、cpとすればcpと同じ動作をするようにしてますが、Pythonスクリプトでそれと同じように argv...