Search code examples
vhdlfpgaclock

Clock divider in vhdl from 100MHz to 1Hz code


I wrote this code for dividing the clock an a nexys4 fpga that has its integrated clock at 100Mhz frequency by default , and i need to divide it to 1hz. Can someone tell me if its correct or if not what needs to be changed ?

LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;


entity digi_clk is
port (clk1 : in std_logic;
       clk : out std_logic
     );
end digi_clk;

architecture Behavioral of digi_clk is

signal count : integer :=0;
signal b : std_logic :='0';
begin

 --clk generation.For 100 MHz clock this generates 1 Hz clock.
process(clk1) 
begin
if(rising_edge(clk1)) then
count <=count+1;
if(count = 50000000) then
b <= not b;
count <=0;

end if;
end if;
clk<=b;
end process;
end;

Solution

  • The code looks OK. However the existing code will produce an output frequency that is just below 1 Hz. To get a precise 100000000:1 ratio, you will want to change the conditional statement from:

        if(count = 50000000) then
    

    ... to:

        if(count = 50000000-1) then