Reputation: 93
I'm very new to HDL coding. I'm using Xilinx verilog for HDL coding.
I'm trying to implement a push btn to activate the LED blinking code below and when button is released, blinking stops. Below is the blinking LED code
`timescale 1ns / 1ps
module blinkyslow(
input CLOCK, SW,
output LED
);
reg [24:0] COUNT = 25'b0000000000000000000000000;
reg LED=1;
always @(posedge CLOCK) begin
COUNT <= COUNT + 1;
LED <= (COUNT == 25'b0000000000000000000000000)?~LED:LED;
end
endmodule
I'm using basys 3 board and i'm trying to link variable SW as the switch
Upvotes: 2
Views: 3940
Reputation: 311
You can use this code:
`timescale 1ns / 1ps
module blinkyslow(
input CLOCK, SW,reset,
output reg LED
);
reg [24:0] COUNT;
always @(posedge CLOCK) begin
if ( reset ==1'b1)
LED <= 1'b0;
COUNT <= 0;
else
begin
COUNT <= COUNT + 1;
LED <= SW & ((COUNT == 25'b0)?~LED:LED);
end
end
endmodule
Since the always block is only sensitive to the rising edge of the CLOCK signal, the reset signal is a synchronous reset.
Upvotes: 2
Reputation: 674
@Nicholas Chan. In your original code, you have defined the following line not in initial block. Hence, LED is hardwired to zero.
reg LED=1;
so either define a reset signal for LED and COUNT (as in earlier answer )or you can try using the following code using initial block
timescale 1ns / 1ps
module blinkyslow(
input CLOCK, SW,
output LED
);
initial:begin
reg [24:0] COUNT = 25'b0000000000000000000000000;
reg LED=1;
end
always @(posedge CLOCK) begin
COUNT <= COUNT + 1;
LED <= (COUNT == 25'b0000000000000000000000000)?~LED:LED;
end
endmodule
Upvotes: 0
Reputation: 75062
For example, you can implement it by having the counter increment only if the button is pressed.
(Strictly speaking, this is not good because if COUNT
stays zero, LED
will be toggled at evely clock. Toggling LED
only if the button is pressed will solve this)
`timescale 1ns / 1ps
module blinkyslow(
input CLOCK, SW,
output LED
);
reg [24:0] COUNT = 25'b0000000000000000000000000;
reg LED=1;
always @(posedge CLOCK) begin
if (SW) begin
COUNT <= COUNT + 1;
LED <= (COUNT == 25'b0000000000000000000000000)?~LED:LED;
end
end
endmodule
(not tested, assuming SW = 1
means the switch on the board is pressed)
Upvotes: 1