
Creating Counters Using Verilog
The following code shows how to implement up, down, and bidirectional counters in Verilog:
always@(posedge CLK or posedge CLEAR)
begin
if(CLEAR)
COUNT=1'b0;
else //if(CLK)
begin
if(LOAD)
COUNT = DIN;
else
begin
if(CE)
begin
if(UP) //Up Counter
COUNT=COUNT+1;
else
COUNT=COUNT-1; //Down Counter
end
end
end
end