Verilog HDL Code for BCD and Gray Counters
Advertisement
This document presents Verilog HDL code for both a BCD counter and a Gray counter.
BCD Counter
Symbol

Truth Table
| Rst | Clk | Q |
|---|---|---|
| 1 | X | 0000 |
| 0 | 1 | 0001 |
| 0 | 1 | 0010 |
| 0 | 1 | 0011 |
| 0 | 1 | 0100 |
| 0 | 1 | 0101 |
| 0 | 1 | 0110 |
| 0 | 1 | 0111 |
| 0 | 1 | 1000 |
| 0 | 1 | 1001 |
Verilog Code
module bcd(clr, clk, dir, tc, q);
input clr, clk, dir;
output reg tc;
output reg[3:0] q;
always @(posedge clk, posedge clr) begin
if (clr == 1)
q = 4'd0;
else begin
if (dir == 1)
q = q + 1;
else if (dir == 0)
q = q - 1;
if (dir == 1 & q == 4'd10) begin
q = 4'd0;
tc = 1'b1;
end else if (dir == 0 & q == 4'd15) begin
q = 4'd9;
tc = 1'b1;
end else
tc = 1'b0;
end
end
endmodule
Simulation Result

Gray Counter
Symbol

Truth Table
| Rst | Clk | En | B3 | B2 | B1 | B0 | G3 | G2 | G1 | G0 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | X | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | 1 |
| 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
Verilog Code
module gray(clr, clk, q);
input clr, clk;
output reg[2:0] q;
reg temp=3'd0;
always @(posedge clk, posedge clr) begin
if (clr == 0) begin
case(temp)
3'd0: q=3'd1;
3'd1: q=3'd3;
3'd2: q=3'd6;
3'd3: q=3'd2;
3'd6: q=3'd7;
3'd7: q=3'd5;
3'd5: q=3'd4;
3'd4: q=3'd0;
endcase
end else
q=3'd0;
end
endmodule
Explore Verilog Logic Gates, Mux & Encoders
- Verilog HDL Code for All Logic Gates
- 2-to-4 Decoder (Verilog HDL Code)
- 4-to-1 Multiplexer and 1-to-4 Demultiplexer (Verilog Code)
- Verilog Code: 1-to-4 Demultiplexer
- 8-to-1 Multiplexer (Verilog HDL Code)
- 8-to-3 Encoder Without Priority (Verilog)
- 8-to-3 Priority Encoder (Verilog)
- 8-to-3 Priority Encoder (Verilog Code)
- 1-Bit and 4-Bit Comparator Design (Verilog)
Explore Verilog Flip-Flops, Counters & Registers
- T, D, SR, JK Flip Flop (Verilog HDL Code)
- D Flip Flop Synchronous Reset (Verilog)
- D Flip Flop Without Reset (Verilog)
- Verilog Code for Binary Up-Down Counter
- Verilog HDL: BCD & Gray Counters
- 4-bit Down Counter (Verilog Code Test Bench)
- 4-bit Binary Asynchronous Reset Counter (Verilog)
- 4-bit Binary Synchronous Reset Counter (Verilog)
- 4-bit BCD Asynchronous Reset Counter (Verilog)
- 4-bit BCD Synchronous Reset Counter (Verilog Code)
- 4-bit Mod-13 Counter (Verilog Code Test Bench)
- Shift Left / Shift Right Register (Verilog)
- Parallel Load Shift Left Register (Verilog Code)
- PRBS Generator (Verilog Code Test Bench)
Explore Verilog Math, Processing & Memory
- Full Adder (Verilog HDL Code)
- Verilog Code: Half Adder, Half Subtractor, Full Subtractor
- 32-Bit ALU (Verilog Code)
- RAM / ROM (Verilog Code)
- Asynchronous FIFO (Verilog Code Test Bench)
- Asynchronous FIFO Design (Verilog Code and Explanation)
- Mealy / Moore Machine (Verilog)
- Low Pass FIR Filter (Verilog Code)
- 4-bit Binary to Gray Counter Converter in Verilog
