An up counter counts numbers in increasing order. It helps keep track of events or time in digital circuits.
Counting seconds in a digital clock
Tracking the number of items passing on a conveyor belt
Measuring how many times a button is pressed
Generating timed signals in a circuit
Syntax
Verilog
module up_counter(
input clk,
input reset,
output reg [3:0] count
);
always @(posedge clk or posedge reset) begin
if (reset)
count <= 4'b0000;
else
count <= count + 1;
end
endmodule
The posedge clk means the counter updates on the rising edge of the clock signal.
The reset input sets the counter back to zero when activated.
Examples
This example shows an 8-bit up counter that counts from 0 to 255.
Verilog
module up_counter_8bit(
input clk,
input reset,
output reg [7:0] count
);
always @(posedge clk or posedge reset) begin
if (reset)
count <= 8'b00000000;
else
count <= count + 1;
end
endmodule
This counter only counts when the enable signal is high.
Verilog
module up_counter_with_enable(
input clk,
input reset,
input enable,
output reg [3:0] count
);
always @(posedge clk or posedge reset) begin
if (reset)
count <= 4'b0000;
else if (enable)
count <= count + 1;
end
endmodule
Sample Program
This testbench runs the 4-bit up counter. It resets the counter first, then lets it count up every clock cycle. The monitor shows the count value over time.
Verilog
module testbench;
reg clk = 0;
reg reset = 1;
wire [3:0] count;
up_counter uut(
.clk(clk),
.reset(reset),
.count(count)
);
always #5 clk = ~clk; // Clock toggles every 5 time units
initial begin
#10 reset = 0; // Release reset after 10 time units#100 $finish; // Stop simulation after 100 time units
end
initial begin
$monitor("Time=%0t count=%b", $time, count);
end
endmodule
OutputSuccess
Important Notes
Always use a reset signal to start the counter from a known value.
The counter width (number of bits) decides the maximum count value.
Clock signal must be stable and clean for correct counting.
Summary
An up counter increases its value on each clock pulse.
Use reset to set the counter back to zero.
You can add enable signals to control counting.
Practice
(1/5)
1. What does an up counter do in Verilog?
easy
A. It holds the value constant regardless of clock.
B. It decreases its value by 1 on each clock pulse.
C. It resets the value to zero on each clock pulse.
D. It increases its value by 1 on each clock pulse.
Solution
Step 1: Understand the function of an up counter
An up counter increments its stored value by 1 every time the clock signal pulses.
Step 2: Compare options with the definition
Only It increases its value by 1 on each clock pulse. describes increasing the value by 1 on each clock pulse, which matches the up counter behavior.
Final Answer:
It increases its value by 1 on each clock pulse. -> Option D
Quick Check:
Up counter = increments by 1 [OK]
Hint: Up counter always counts up by 1 each clock pulse [OK]
Common Mistakes:
Confusing up counter with down counter
Thinking reset happens every clock pulse
Assuming counter holds value without change
2. Which of the following is the correct Verilog syntax to define a 4-bit up counter with synchronous reset?
easy
A. always @(negedge clk) if (reset) count = 0; else count = count + 1;
B. always @(posedge clk) if (reset) count <= 0; else count <= count + 1;
C. always @(posedge clk) if (reset) count = 0; else count = count - 1;
D. always @(posedge clk) if (reset) count <= 1; else count <= count + 1;
Solution
Step 1: Identify correct clock edge and reset behavior
Up counters usually update on the positive edge of the clock and reset synchronously to zero.
Step 2: Check assignment operators and counting direction
Non-blocking assignment '<=' is preferred in sequential logic. Counting up means adding 1, and reset sets count to 0.
Final Answer:
always @(posedge clk) if (reset) count <= 0; else count <= count + 1; -> Option B
Quick Check:
posedge + non-blocking + reset to 0 + count up = A [OK]
Hint: Use posedge clk and non-blocking <= for counters [OK]
Common Mistakes:
Using negedge instead of posedge
Using blocking '=' in sequential logic
Resetting count to 1 instead of 0
Counting down instead of up
3. What will be the output sequence of a 3-bit up counter starting from 0 after 5 clock pulses (assuming reset is inactive)?
module counter(input clk, input reset, output reg [2:0] count);
always @(posedge clk) begin
if (reset) count <= 0;
else count <= count + 1;
end
endmodule
medium
A. [0, 1, 2, 3, 4]
B. [1, 2, 3, 4, 5]
C. [1, 2, 3, 4, 0]
D. [0, 1, 2, 3, 4, 5]
Solution
Step 1: Understand initial value and counting
The counter starts at 0 after reset. On first clock pulse, count increments to 1.
Since it's a 3-bit counter, it counts from 0 to 7 and then wraps around. After 5 pulses starting from 0, the sequence is [1, 2, 3, 4, 5]. However, the question asks for the output sequence starting from 0 after 5 pulses, which includes the initial 0 and the next 4 increments: [0, 1, 2, 3, 4].
Final Answer:
[0, 1, 2, 3, 4] -> Option A
Quick Check:
Count starts at 0, increments each pulse = [0..4] [OK]
Hint: Count increments after clock pulse, starts from 0 [OK]
Common Mistakes:
Including initial 0 before first pulse
Counting 6 values instead of 5
Confusing count start value
4. Identify the error in this Verilog code for a 4-bit up counter:
module counter(input clk, input reset, output reg [3:0] count);
always @(posedge clk) begin
if (reset == 1) count = 0;
else count = count + 1;
end
endmodule
medium
A. Output 'count' should not be declared as reg.
B. Reset condition should be 'if (reset != 1)'.
C. Using blocking assignments '=' instead of non-blocking '<=' inside always block.
D. Clock edge sensitivity should be negedge instead of posedge.
Solution
Step 1: Check assignment type in sequential always block
Inside always @(posedge clk), non-blocking assignments '<=' should be used to avoid race conditions.
Step 2: Verify other parts of the code
Reset condition and reg declaration are correct. Clock edge is correctly posedge.
Final Answer:
Using blocking assignments '=' instead of non-blocking '<=' inside always block. -> Option C
Quick Check:
Sequential logic uses '<=' not '=' [OK]
Hint: Use '<=' for sequential logic assignments [OK]
Common Mistakes:
Using '=' in always @(posedge clk)
Misunderstanding reset condition
Thinking reg is wrong for output
Changing clock edge unnecessarily
5. How can you modify a 4-bit up counter to count only when an enable signal is high?
module counter(input clk, input reset, output reg [3:0] count);
always @(posedge clk) begin
if (reset) count <= 0;
else count <= count + 1;
end
endmodule
hard
A. Add 'if (enable) count <= count + 1;' inside the always block after reset check.
B. Add 'if (enable) count = count + 1;' outside the always block.
C. Replace 'count <= count + 1;' with 'count <= count - 1;' when enable is high.
D. Add 'enable' to the sensitivity list of the always block.
Solution
Step 1: Understand how to control counting with enable
Counting should happen only when enable is high, so add a condition inside the always block after reset.
Step 2: Implement enable check correctly
Use 'if (enable) count <= count + 1;' with non-blocking assignment inside the always block.
Final Answer:
Add 'if (enable) count <= count + 1;' inside the always block after reset check. -> Option A
Quick Check:
Enable controls counting inside always block [OK]
Hint: Check enable inside always block after reset [OK]
Common Mistakes:
Using blocking assignment outside always block
Changing count direction instead of gating
Adding enable to sensitivity list (not needed)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.