Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
module bcd_count (
// Outputs
count,
// Inputs
clk, reset_n
);
input clk, reset_n;
output [15:0] count;
reg [15:0] count;
always @(posedge clk or negedge reset_n) begin
if (!reset_n) begin
count <= 0;
end else begin
if (count[3:0] == 9) begin
count[3:0] <= 0;
if (count[7:4] == 9) begin
count[7:4] <= 0;
if (count[11:8] == 9) begin
count[11:8] <= 0;
if (count[15:12] == 9) begin
count[15:12] <= 0;
end else begin
count[15:12] <= count[15:12] + 1;
end
end else begin
count[11:8] <= count[11:8] + 1;
end
end else begin
count[7:4] <= count[7:4] + 1;
end
end else begin
count[3:0] <= count[3:0] + 1;
end
end
end
endmodule // bcd_count
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity bcd_16 is
port(
clk, reset : in std_logic;
count : buffer std_logic_vector (15 downto 0)
);
end bcd_16;
architecture counter of bcd_16 is
begin
bcd_counting: process (clk, reset)
begin -- process bcd_counting
if reset = '1' then -- asynchronous reset (active high)
count <= ( others => '0');
elsif clk'event and clk = '1' then -- rising clock edge
if count ( 3 downto 0) = "1001" then
count(3 downto 0) <= "0000";
if count ( 7 downto 4) = "1001" then
count(7 downto 4) <= "0000";
if count ( 11 downto 8 ) = "1001" then
count(11 downto 8 ) <= "0000";
if count ( 15 downto 12) = "1001" then
count(15 downto 12) <= "0000";
else
count(15 downto 12) <= count(15 downto 12) + '1';
end if;
else
count(11 downto 8 ) <= count(11 downto 8 ) + '1';
end if;
else
count(11 downto 8 ) <= count(11 downto 8 ) + '1';
end if;
else
count(3 downto 0 ) <= count(3 downto 0 ) + '1';
end if;
end if;
end process bcd_counting;
end counter;