This website uses cookies. By using this site, you consent to the use of cookies. For more information, please take a look at our Privacy Policy.
Home > FPGA Technical Tutorials > Design Recipes for FPGAs Using Verilog and VHDL > Counters > BCD Counter

TABLE OF CONTENTS

Xilinx FPGA FPGA Forum

BCD Counter

FONT SIZE : AAA

The BCD (Binary Coded Decimal) counter is simply a counter that resets when the decimal value 10 is reached instead of the normal 15 for a 4-bit binary counter. This counter is often used for decimal displays and other human interface hardware. The VHDL for a BCD counter is very similar to that of a basic binary counter except that the maximum value is 10 (hexadecimal A) instead of 15 (hexadecimal F). The VHDL for a simple BCD counter is given in the following listing. The only change is that the counter has an extra check to reset when the value of the count variable is greater than 9 (the counter range is 0 to 9). 

1 library ieee;

2 use ieee.std_logic_1164.all;

3 use ieee.numeric_std.all;

4

5 entity counter is

6 generic (

7 n : integer := 4;

8 port (

9 clk : in std_logic;

10 rst : in std_logic;

11 output : out std_logic_vector((n−1) downto 0)

12 );

13 end;

14

15 architecture simple of counter is

16 begin

17 process(clk, rst)

18 variable count : unsigned((n−1) downto 0);

19 begin

20 if rst = ’0’ then

21 count := (others => ’0’);

22 elsif rising_edge(clk) then

23 count := count + 1;

24 if count > 9 then

25 count := 0;

26 else if

27 end if;

28 output <= std_logic_vector(count);

29 end process;

30 end;

In a similar manner we can implement a BCD counter in Verilog using the code given here:

1 module bcd_counter (

2 clk, / clock input

3 rst, / reset (active low)

4 counter_output / counter output

5 );

6

7 input clk;

8 input rst;

9

10 output [3:0] counter_output;

11

12 wire clk;

13 wire rst;

14

15 reg [3:0] counter_output ;

16

17 always @ (posedge clk)

18 begin : count

19 if (rst == 1’b0) begin

20 counter_output <= #1 4’b0000;

21 end

22 else begin

23 if(counter_output < 9) begin

24 counter_output <= #1 counter_output + 1;

25 end

26 else

27 counter_output <= #1 4’b0000;

28 end

29 end

30

31 endmodule

Figure 24.7 BCD counter simulation.

and test it using the same basic counter test bench created for the simple counter, giving the simulation results as shown in Figure 24.7. In the results this time you can see the counter variable in binary and also in unsigned decimal counting up to 1001 (binary) and 9 (decimal), then returning back to 0, giving the decimal counter. 

  • XC4003E-2PQ100C

    Manufacturer:Xilinx

  • FPGA XC4000E Family 3K Gates 238 Cells 0.35um Technology 5V 100-Pin PQFP EP
  • Product Categories: FPGAs (Field Programmable Gate Array)

    Lifecycle:Obsolete -

    RoHS: No RoHS

  • XC4028XLA-08HQ208C

    Manufacturer:Xilinx

  • FPGA XC4000XLA Family 28K Gates 2432 Cells 263MHz 0.35um Technology 3.3V 208-Pin HSPQFP EP
  • Product Categories:

    Lifecycle:Obsolete -

    RoHS: No RoHS

  • XC5VLX110-2FF1760C

    Manufacturer:Xilinx

  • FPGA Virtex-5 LX Family 110592 Cells 65nm Technology 1V 1760-Pin FCBGA
  • Product Categories: FPGAs (Field Programmable Gate Array)

    Lifecycle:Active Active

    RoHS: No RoHS

  • XC5VLX110-2FFG1153I

    Manufacturer:Xilinx

  • FPGA Virtex-5 LX Family 110592 Cells 65nm Technology 1V 1153-Pin FCBGA
  • Product Categories: Industrial components

    Lifecycle:Active Active

    RoHS:

  • XC5VLX110-3FFG1153C

    Manufacturer:Xilinx

  • FPGA Virtex-5 LX Family 110592 Cells 65nm Technology 1V 1153-Pin FCBGA
  • Product Categories: FPGAs (Field Programmable Gate Array)

    Lifecycle:Active Active

    RoHS:

Need Help?

Support

If you have any questions about the product and related issues, Please contact us.