VHDL Code for Clock Divider ( Frequency Divider )

Clock Divider is also known as frequency divider, which divides the input clock frequency and produce output clock. In our case let us take input frequency as 50Mhz and divide the clock frequency to generate 1Khz output signal. VHDL code consist of Clock and Reset input, divided clock as output. Count is a signal to generate delay, Tmp signal toggle itself when the counter value reaches 25000. Output produce 1Khz clock frequency.

VHDL Code for Clock Divider


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.ALL;

entity Clock_Divider is
port ( clk,reset: in std_logic;
clock_out: out std_logic);
end Clock_Divider;

architecture bhv of Clock_Divider is

signal count: integer:=0;
signal tmp : std_logic := '1';

begin

process(clk,reset)
begin
if(reset='0') then
count<=0;
tmp<='1';
elsif(clk'event and clk='1') then
count <=count+1;
if (counter = 25000) then
tmp <= NOT tmp;
count <= 0;
end if;
end if;
clock_out <= tmp;
end process;

end bhv;

 

You may also like...

10 Responses

  1. Mike says:

    Why we need tmp signal instead of directly inverting clkout?

  2. Admin says:

    Output can’t be inverted directly and assign to the same output port.. That’s why declared tmp signal and finally assigned to output.

  3. Serge says:

    Hi, i was trying to use your clock, but it keeps sending the error of ‘counter’ not being assigned, any ideas on how to fix it?

    Error (10482): VHDL error at Clock_divider.vhd(24): object “counter” is used but not declared

  4. Rakshith says:

    Pls suggest me program for output y =1 when s=1 for continuous 3 clock pulse… If S changes btwn 3 clk pulse then output should goes low… Sequential circuit

    • Admin says:

      Rakshith, try the following declare count as integer

      if s= ‘1’ then
      count = count + 1;
      if count = 3 then
      count = 0;
      y = ‘1’;
      end if;
      else
      y = ‘0’;
      count = 0;
      end if;

  5. saswata says:

    how do i generate the vhdl test bench code??for the above program…

  6. Admin says:

    Rafer the following post for How to create VHDL Testbench?
    http://allaboutfpga.com/vhdl-testbench-tutorial/

    You need to toggle reset from high to low after some delay in testbench code.

    I will update the code with testbench soon.

  7. Paebbels says:

    Your counter does not provide a 1kHz output signal, because it counts 25,001 cycles (0 .. 25k equals 25,001).

    Regards
    Patrick

Leave a Reply

Your email address will not be published. Required fields are marked *