lg3
Newbie level 3
- Joined
- Feb 1, 2013
- Messages
- 3
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,308
Hi everybody,
I'm trying to implement in an Altera's DE2-70 board (which has an embedded Cyclone II) an up/down counter,
which counts up when a press one pushbutton, and count down when a press another pushbutton. First I tried implementing just
the up counter, which looked like this :
It worked correctly. However, when trying to implement both increment and decrement I was tempted to write the following
code, which did not work:
Googlin, I found out it has something to do with the negedge inc or negedge dec, which seems to cause a combinational loop
when synthesized. I have tried many different ways to implement it, but I do not know how to treat this kind of event,
because I want to count just once, when the user triggers the pushbutton (which in the DE2-70 board already have debouncing).
Using a "clk" doesn't seem intuitive to me. Can someone propose another approach which will work on both pushbutton edges?
Thanks in advance,
lgb.
I'm trying to implement in an Altera's DE2-70 board (which has an embedded Cyclone II) an up/down counter,
which counts up when a press one pushbutton, and count down when a press another pushbutton. First I tried implementing just
the up counter, which looked like this :
Code:
module top(input inc, rst,
output[6:0] segment_display);
reg[3:0] counter;
always @(negedge inc or negedge rst) begin
if(~rst) begin
counter <= 4'd0;
end else begin
if(~inc) begin
counter <= counter + 1;
end
end
end
endmodule
It worked correctly. However, when trying to implement both increment and decrement I was tempted to write the following
code, which did not work:
Code:
always @(negedge inc or negedge dec or negedge rst) begin
if(~rst) begin
counter <= 4'b0;
end else begin
if (~inc) begin
counter <= counter + 1;
end else begin
counter <= counter - 1;
end
end
end
Googlin, I found out it has something to do with the negedge inc or negedge dec, which seems to cause a combinational loop
when synthesized. I have tried many different ways to implement it, but I do not know how to treat this kind of event,
because I want to count just once, when the user triggers the pushbutton (which in the DE2-70 board already have debouncing).
Using a "clk" doesn't seem intuitive to me. Can someone propose another approach which will work on both pushbutton edges?
Thanks in advance,
lgb.