This has been far in the past already, but the solutions presented are perhaps not exactly what you were expecting. From what I could infer the solutions all consider that the LED will keep toggling as long as the pushbutton is being pressed (i.e. it will toggle at the clock's frequency), making it visually imperceptible if the clock frequency is high. However, I assume you would want something which toggles the LED only once each time the pushbutton is pressed, with the LED state being preserved during this period.
The example below toggles the states of 3 LEDs based on the activity of 3 pushbuttons.
- led0 is activated whenever pbutton0 is pressed.
- led1 keeps toggling periodically (based on the size of clk_div) and is reset whenever pbutton1 is pressed.
- led2 is toggled whenever pbutton2 is pressed.
Note that led0 is combinational, while the other two LEDs are sequential. For toggling led2, the previous state of pbutton2 must be stored; whenever pbutton2(t-1)==0 and pbutton2(t)==1, that means the button has just gone from low to high and, thus, the state of led2 must change.
Lastly, please ignore the clock source, since this was only used to demo the code on the Xilinx SP605 development kit.
////////////////////////////////////////////////////
// This project uses 3 pushbuttons and 3 LEDs.
// pbutton0 activates led0
// pbutton1 serves as reset for led1 periodic toggling
// pbutton2 toggles led2
//
// The clock source (divider+buffer) was created using the clocking IP wizard.
//
module xilinx_sp605_board_leds
(
input CLK_IN1_P,
input CLK_IN1_N,
input pbutton0,
input pbutton1,
input pbutton2,
output led0,
output reg led1,
output reg led2
);
// declarations
wire clk;
wire reset = pbutton1;
reg [19:0] clk_div;
reg pbutton2_reg;
// led0 = state of pbutton0
assign led0 = pbutton0;
// differential clock divider+buffer
clk_source CLK_SOURCE (
.CLK_IN1_P(CLK_IN1_P),
.CLK_IN1_N(CLK_IN1_N),
.CLK_OUT1(clk),
.RESET(reset));
// led1, led2 toggling
always @(posedge reset or posedge clk)
begin
if(reset)begin
clk_div <= 0;
led1 <= 0;
pbutton2_reg <= 0;
led2 <= 0;
end else begin
clk_div <= clk_div + 1;
if(clk_div==0)
led1 <= ~led1;
pbutton2_reg <= pbutton2;
if(~pbutton2_reg && pbutton2)
led2 <= ~led2;
end
end
endmodule