I try to design simple circuit, I have two push buttons as an input and 8 leds as an output, whenever I press one of them I want to shift the bits to the left and the last bit will be also set to 1, if other push button is pressed the bits will be shift to right. Here is my code.
module project1(leds, pushbutton1, pushbutton2);
output reg [7:0] leds;
input pushbutton1, pushbutton2 ;
reg pb1, pb2;
always @(pushbutton1 or pushbutton2)
begin
pb1<=pushbutton1;
pb2<=pushbutton2;
if ((pb1==1)&&(pb2==0))
leds<=(leds<<1)+1;
else if ((pb1==0)&&(pb1==1))
leds<=(leds>>1);
else if (((pb1==0)&&(pb2==0)) ||((pb1==1)&&(pb2==1)))
leds<=leds;
end
initial begin
leds = 8'h00;
end
endmodule
I have read many pages and I guess there will be bouncing problem. To solve it I tried to use posedge
always @(posedge pushbutton1 or posedge pushbutton2)
but in this case I get an error message "Error (10200): Verilog HDL Conditional Statement error at myfirstproject.v(14): cannot match operand(s) in the condition to the corresponding edges in the enclosing event control of the always construct"
When I tried to implement just one push button I had no problem and the below code worked well
module project1(leds, pushbutton1);
output reg [7:0] leds;
input pushbutton1;
always @(posedge pushbutton1)
begin
if (leds==8'hFF)
leds<=8'h00;
else
leds<=(leds<<1)+1;
end
initial begin
leds=8'h00;
end
endmodule
I think If-else statements cover all possible conditions but it doesn't work. How can I check two inputs at the same time in one always block? I know it is not possible to change the value of any variable that is defined in different always block but is there any way to implement it by using two or more different always block like
always @(posedge pushbutton1)
..........
always @(posedge pushbutton2)
.........
Also I tried to use the system clock to trigger the button change like that, but still doesn't work
module project1 (leds, pushbutton1, pushbutton2, clk);
output reg [7:0] leds;
input clk, pushbutton1, pushbutton2 ;
always @(posedge clk)
begin
if (pushbutton1==1 && pushbutton2==0)
begin
if (leds==8'h00)
leds<=8'h01;
else if (leds==8'hff)
leds<=8'h00;
else
leds<= (leds<<1)+1;
end
if (pushbutton1==0 && pushbutton2==1)
begin
if ((leds==8'h00) ||(leds==8'h01))
leds<= 8'h00;
else
leds<=(leds>>1);
end
else
leds<=leds;
end
initial begin
leds = 8'h00;
end
endmodule
@(posedge clock)
. - Serge