Counters in Verilog
Counters in Verilog
1. What is a counter ?
Counters are sequential circuits used to count events , and they are commonly used
in digital systems. Counters can be implemented in various ways in Verilog and can
be classified into different types.
Synchronous Counters :
All flips flops are clocked simultaneously.
Count transitions occur at the same time , making them faster.
Asynchronous Counters
Flip flops are clocked one after another , causing a ripple effect.
Slower than synchronous counters but easier to implement.
2. Types of counters
Binary Counter : counts in binary numbers (e.g. 0 ,1,10,11,…)
Up counter : counts upwards(incrementing count)
Down Counter : counts downwards (decrementing count)
Up/down Counter: counts both up and down , based on control input.
Ring Counter : a type of circular shift register; only flip flop is high at any time.
Johnson Counter: a modified ring counter ; the complemented output is fed
back to the input
VERILOG CODES
counter_basic dut(clk,rst,count);
always@(posedge clk or posedge rst)
end
endmodule
2. Down counter
module downcounter(clk,rst,count); module tb;
parameter n = 4; reg clk, rst;
input clk , rst; 3. Up down counter wire [3:0] count;
end
endmodule
4. Ring counter
begin
else rst=1;
end
end
initial begin
end
time=0 , count = 1000 always #5 clk = ~clk;
time=5 , count = 0100
time=15 , count = 0010
time=25 , count = 0001
initial begin
time=35 , count = 1000
$dumpfile("dump.vcd");
$dumpvars(0,dut);
end
endmodule
5. Johnson counter
end #5rst=0;
endmodule
#80 $finish;
end
initial begin
time=0 , count=0000
time=5 , count=1000 $monitor("time=%0d , count=%b",
time=15 , count=1100 $time,count);
time=25 , count=1110
time=35 , count=1111 end
time=45 , count=0111
time=55 , count=0011
time=65 , count=0001 always #5 clk=~clk;
time=75 , count=0000 initial begin
$dumpfile("dump.vcd");
$dumpvars(0,dut);
end
endmodule
6. Design a 2 bit counter which counts 0-1-2-3-2-1-0-1-2 (gradually
increment and decrement counter)
begin
count<=2'b00; clk=0;
count<=count+1; up=1;
else #5rst=0;
count <=count -1 ;
#20 $finish;
end
initial begin
$dumpvars(0,dut);
end
endmodule
7. Design a BCD counter
begin
count<=0; clk=0;
else rst=1;
end
end
initial begin
time=0,count= 0
time=5,count= 1 $monitor("time=%0d,count=%d",$time,count);
time=15,count= 2 end
time=25,count= 3
time=35,count= 4
time=45,count= 5
always #5 clk=~clk;
time=55,count= 6
time=65,count= 7 initial begin
time=75,count= 8
$dumpfile("dump.vcd");
time=85,count= 9
$dumpvars(0,dut);
end
endmodule