/* * LED_control.v * Author: S. Klewin * * This module takes care of the LED. It forces them to blink if * the input on is active. If the corresponding input is more * often then ~100,000 times active per second, the LED will be * on permanently. If it is less often, the LED will blink. * Additionaly there is a test mode implemented. Here the LEDs * will blink in a specific order to test if the comunication * with the LTU-T is working and to find the correct device if * you have a lot of them. */ module LED_control( input clk, input reset, input test, input [9:0] on, output reg [9:0] LEDs); reg test_mode; parameter test_counter_1 = 24'h2AAAAA; parameter test_counter_2 = 2*test_counter_1; parameter test_counter_3 = 3*test_counter_1; parameter test_counter_4 = 4*test_counter_1; parameter test_counter_5 = 5*test_counter_1; parameter test_state_0 = 3'd0, test_state_1 = 3'd1, test_state_2 = 3'd2, test_state_3 = 3'd3, test_state_4 = 3'd4, test_state_5 = 3'd5, test_state_6 = 3'd6, test_state_7 = 3'd7; reg side; reg side_changed; parameter left = 1'b0, right = 1'b1; reg [9:0] cnt_en; reg [9:0] sclr; wire [15:0] counter_9; wire [15:0] counter_8; wire [15:0] counter_7; wire [15:0] counter_6; wire [15:0] counter_5; wire [15:0] counter_4; wire [15:0] counter_3; wire [15:0] counter_2; wire [15:0] counter_1; wire [15:0] counter_0; input_counter c9( .clock(clk), .cnt_en(cnt_en[9]), .sclr(sclr[9]), .q(counter_9)); input_counter c8( .clock(clk), .cnt_en(cnt_en[8]), .sclr(sclr[8]), .q(counter_8)); input_counter c7( .clock(clk), .cnt_en(cnt_en[7]), .sclr(sclr[7]), .q(counter_7)); input_counter c6( .clock(clk), .cnt_en(cnt_en[6]), .sclr(sclr[6]), .q(counter_6)); input_counter c5( .clock(clk), .cnt_en(cnt_en[5]), .sclr(sclr[5]), .q(counter_5)); input_counter c4( .clock(clk), .cnt_en(cnt_en[4]), .sclr(sclr[4]), .q(counter_4)); input_counter c3( .clock(clk), .cnt_en(cnt_en[3]), .sclr(sclr[3]), .q(counter_3)); input_counter c2( .clock(clk), .cnt_en(cnt_en[2]), .sclr(sclr[2]), .q(counter_2)); input_counter c1( .clock(clk), .cnt_en(cnt_en[1]), .sclr(sclr[1]), .q(counter_1)); input_counter c0( .clock(clk), .cnt_en(cnt_en[0]), .sclr(sclr[0]), .q(counter_0)); reg [23:0] time_counter; reg [2:0] test_state; reg test_save; reg test_first; initial begin LEDs <= 10'b11_1111_1111; test_mode <= 1; test_first <= 1; test_save <= 0; cnt_en <= 0; time_counter <= 0; test_state <= 0; side <= 0; side_changed <= 0; end parameter max = 16'hFFFF; parameter threshold = 16'd10000; always @(posedge clk) begin cnt_en <= on; if (counter_9 == max) cnt_en[9] <= 0; if (counter_8 == max) cnt_en[8] <= 0; if (counter_7 == max) cnt_en[7] <= 0; if (counter_6 == max) cnt_en[6] <= 0; if (counter_5 == max) cnt_en[5] <= 0; if (counter_4 == max) cnt_en[4] <= 0; if (counter_3 == max) cnt_en[3] <= 0; if (counter_2 == max) cnt_en[2] <= 0; if (counter_1 == max) cnt_en[1] <= 0; if (counter_0 == max) cnt_en[0] <= 0; end always @(posedge clk) begin sclr <= 0; time_counter <= time_counter + 1'b1; test_save <= test; if (reset == 1) begin LEDs <= 10'b11_1111_1111; if (test_first == 0) test_mode <= 0; sclr <= 10'b11_1111_1111; time_counter <= 0; test_state <= 0; side <= 0; side_changed <= 0; end if (test_mode) begin case (test_state) test_state_0: begin LEDs <= 10'b00_0000_0000; if (time_counter >= test_counter_1) test_state <= test_state_1; end test_state_1: begin LEDs <= 10'b11_0000_0000; if (time_counter >= test_counter_2) test_state <= test_state_2; end test_state_2: begin LEDs <= 10'b00_1100_0000; if (time_counter >= test_counter_3) test_state <= test_state_3; end test_state_3: begin LEDs <= 10'b00_0011_0000; if (time_counter >= test_counter_4) test_state <= test_state_4; end test_state_4: begin LEDs <= 10'b00_0000_1100; if (time_counter >= test_counter_5) test_state <= test_state_5; end test_state_5: begin LEDs <= 10'b00_0000_0011; if (time_counter == 16'hFFFF) test_state <= test_state_0; if (test_first == 1'b1) begin test_first <= 0; test_state <= test_state_6; end end test_state_6: begin LEDs <= 0; test_mode <= 0; test_state <= test_state_0; sclr <= 10'b11_1111_1111; end test_state_7: begin LEDs <= 0; test_mode <= 0; sclr <= 10'b11_1111_1111; end endcase end else if (side_changed == 1'b1) begin case (side) left: begin LEDs <= 10'b00_0000_0000; if (counter_9 > 0) LEDs[9] <= 1'b1; if (counter_7 > 0) LEDs[7] <= 1'b1; if (counter_5 > 0) LEDs[5] <= 1'b1; if (counter_3 > 0) LEDs[3] <= 1'b1; if (counter_1 > 0) LEDs[1] <= 1'b1; if (counter_8 > threshold) LEDs[8] <= 1'b1; if (counter_6 > threshold) LEDs[6] <= 1'b1; if (counter_4 > threshold) LEDs[4] <= 1'b1; if (counter_2 > threshold) LEDs[2] <= 1'b1; if (counter_0 > threshold) LEDs[0] <= 1'b1; sclr <= 10'b10_1010_1010; end right: begin LEDs <= 10'b00_0000_0000; if (counter_9 > threshold) LEDs[9] <= 1'b1; if (counter_7 > threshold) LEDs[7] <= 1'b1; if (counter_5 > threshold) LEDs[5] <= 1'b1; if (counter_3 > threshold) LEDs[3] <= 1'b1; if (counter_1 > threshold) LEDs[1] <= 1'b1; if (counter_8 > 0) LEDs[8] <= 1'b1; if (counter_6 > 0) LEDs[6] <= 1'b1; if (counter_4 > 0) LEDs[4] <= 1'b1; if (counter_2 > 0) LEDs[2] <= 1'b1; if (counter_0 > 0) LEDs[0] <= 1'b1; sclr <= 10'b01_0101_0101; end endcase end side_changed <= 1'b0; if (time_counter == 24'h7FFFFF) begin side <= left; side_changed <= 1'b1; end if (time_counter == 24'hFFFFFF) begin side <= right; side_changed <= 1'b1; end if (test > test_save) begin test_mode <= 1'b1; test_state <= test_state_0; time_counter <= 0; end else if (test < test_save) begin test_mode <= 1'b0; time_counter <= 0; end end endmodule