TransWikia.com

Clock divider in vhdl from 100MHz to 1Hz code

Stack Overflow Asked by mariaaaa on February 13, 2021

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;

2 Answers

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

Correct answer by rtx13 on February 13, 2021

The program seems correct, but you should be declaring the internal signal (count) as an integer. Then your code should compile successfully. But you will get some warnings and will find some problems in testbech simulation. To avoid that you need to declare the internal signal ( count ) as: signal count : std_logic_vector (25 downto 0); because 100MHz coded in 26 bits. I prefer to convert the 50000000 to Hexadecimal format and it should work without any problem.

Answered by Joseph Dougmi on February 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP