Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Verilog codes for clock divider

Status
Not open for further replies.

kaiser

Newbie level 5
Joined
Jul 5, 2005
Messages
9
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,452
I have a 50 Mhz clock on my board and i need a 60Hz clock for my design......
Somebody can suggest anything to do...?
A verilog descripiton it will be very nice....(with comments- general mods)

THX IN ADVANCE!!
 

nand_gates

Advanced Member level 3
Joined
Jul 19, 2004
Messages
899
Helped
175
Reputation
350
Reaction score
53
Trophy points
1,308
Activity points
7,020
verilog code to measure the clk frequency

Here goes the Verilog code!


Code:
module clk_div(clk_50mhz, rst_n, clk_60hz);
   input clk_50mhz, rst_n;
   output      clk_60hz;
   reg         clk_60hz;
   
   reg [47:0]  counter;

   always @(posedge clk_50mhz or negedge rst_n) begin
      if (!rst_n) begin
         counter <= 48'h00000;
         clk_60hz <= 1'b0;
      end else begin
        if (counter == 48'h65B9A) begin
           counter <= 48'h00000;
           clk_60hz <= ~clk_60hz;
        end else begin
           counter <= counter + 1'b1;
        end
      end // else: !if(!rst_n)
   end // always @ (posedge clk_50mhz or negedge rst_n)
endmodule // clk_div
 

    kaiser

    Points: 2
    Helpful Answer Positive Rating

sp

Full Member level 6
Joined
Jan 1, 2004
Messages
395
Helped
23
Reputation
46
Reaction score
2
Trophy points
1,298
Location
Floating Garden
Activity points
4,044
Re: CLK Divider HELP

i get it from one university... dunno its name D...

VHDL

Code:
If you need a slower clock, here is a simple clock divider algorithm, that divides the clock by 2N.

Where :

N = f(clk) / (2* f(desired))

 

-- file "clk_div.vhd"

-- a generic clock divider, divides by 2*N

-- adapted from "VHDLL Primer" by J. Bhasker, p. 295

------------------------------------------------------------------------------------------

  
library ieee; 
use ieee.std_logic_1164.all; 
  
entity clk_div is 
  generic(N: positive:= 2); 
  port 
    (fast_clk, reset: in std_logic; 
     slow_clk: buffer std_logic 
    ); 
end clk_div; 
  
architecture behavioural of clk_div is 
begin 
  process(reset, fast_clk) 
    variable count: natural; 
  begin 
    if reset = '1' then 
       count := 0; 
       slow_clk <= '0'; 
    elsif rising_edge(fast_clk) then 
       count := count + 1; 
          if count = N then 
             slow_clk <= not slow_clk; 
             count := 0; 
          end if; 
   end if; 
  end process; 
end behavioural;


regards,
sp
 

    kaiser

    Points: 2
    Helpful Answer Positive Rating

angelote

Advanced Member level 4
Joined
Apr 28, 2005
Messages
108
Helped
12
Reputation
24
Reaction score
1
Trophy points
1,298
Location
Spain
Activity points
2,099
Re: CLK Divider HELP

always @(posedge clk_50mhz or negedge rst_n) begin
if (!rst_n) begin


You will need dual edge filp-flop. Is your CPLD/FPGA capable?[/u]
 

    kaiser

    Points: 2
    Helpful Answer Positive Rating

adap

Member level 5
Joined
Feb 16, 2005
Messages
93
Helped
12
Reputation
24
Reaction score
1
Trophy points
1,288
Activity points
2,261
Re: CLK Divider HELP

always @(posedge clk_50mhz or negedge rst_n) begin
if (!rst_n) begin


You will need dual edge filp-flop. Is your CPLD/FPGA capable?

There is no need to use dual edge flip-flop. That part of the verilog code is as writing in VHDL the following:
Code:
process(clk_50mhz, rst_n)
  begin
    if reset = '0' then 
      ........
   elsif rising_edge(clk_50mhz) then   
      .......
 

    kaiser

    Points: 2
    Helpful Answer Positive Rating

kaiser

Newbie level 5
Joined
Jul 5, 2005
Messages
9
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,452
Re: CLK Divider HELP

Thank you very much....

it was helpfully..


After i was looking over your suggestions, i made this(from 50Mhz downto 1Hz with 50% dutty cycle)

module clk_div ( clk_in, reset,clk_1hz);
input clk_in, reset;
output clk_1hz;
reg clk_1hz;


reg [25] counter;

always @(posedge clk_in or posedge reset)
begin
if (reset)
begin
counter <= 26'h0;
clk_1hz <= 1'b0;
end
else
begin
if (counter == 26'h17D7840)
begin
counter <= 26'h0;
clk_1hz <= ~clk_1hz;
end
else
counter <= counter + 1'b1;
end
end

endmodule


any comments it will be appreciated...

Added after 11 minutes:

nand_gates said:
Here goes the Verilog code!


Code:
module clk_div(clk_50mhz, rst_n, clk_60hz);
   input clk_50mhz, rst_n;
   output      clk_60hz;
   reg         clk_60hz;
   
   reg [47:0]  counter;

   always @(posedge clk_50mhz or negedge rst_n) begin
      if (!rst_n) begin
         counter <= 48'h00000;
         clk_60hz <= 1'b0;
      end else begin
        if (counter == 48'h65B9A) begin
           counter <= 48'h00000;
           clk_60hz <= ~clk_60hz;
        end else begin
           counter <= counter + 1'b1;
        end
      end // else: !if(!rst_n)
   end // always @ (posedge clk_50mhz or negedge rst_n)
endmodule // clk_div



Thank you for your code....
But i dont understand something.......when i divide from 50 Mhz downto 60 hz ..well...i need "800.000 cycle of 50 MHz clock" /2= 400.000
and 400.000 = 48'h61A80 and in your code it a little bigger..so is not exactly 60Hz...right?
Why do you do that...? It's a trick and i dont know about it?
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,933
Helped
638
Reputation
1,274
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,176
CLK Divider HELP

50 MHz does not divide evenly to 60 Hz. The ratio is 833333.333...

You could build a counter that counts up from 0 through 2499999 and outputs a pulse as it passes 0, 833333, and 1666666. That will give you three pulses every 2500000 clocks which is exactly 60 Hz. But the pulses are not evenly spaced, there is one-cycle jitter (20 ns).
Code:
module top (clk, hz60);
  input             clk;
  reg        [21:0] count = 0;
  output reg        hz60 = 0;

  always @ (posedge clk) begin
    count <= (count == 2499999) ? 0 : count + 1;
    hz60 <= (count == 0) | (count == 833333) | (count == 1666666);
  end
endmodule
If you can't tolerate the 20 ns jitter, then you need to use a DLL or PLL to change the 50 MHz into something that can be divided cleanly.
 

omara007

Advanced Member level 4
Joined
Jan 6, 2003
Messages
1,237
Helped
50
Reputation
102
Reaction score
16
Trophy points
1,318
Location
Cairo/Egypt
Activity points
9,715
Re: CLK Divider HELP

echo47 said:
That will give you three pulses every 2500000 clocks which is exactly 60 Hz. But the pulses are not evenly spaced, there is one-cycle jitter (20 ns).

How will it exactly be 60 Hz ? ..

don't you measure the frequency on ONE cycle basis ? .. can you tell me the duty cycle ?
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,933
Helped
638
Reputation
1,274
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,176
CLK Divider HELP

My counter has 20ns period jitter. The period is 16666660ns, 16666660ns, 16666680ns, and then it repeats. The average of those three periods is 16666666.6666... nanoseconds, which is exactly 60 Hz.

Even the best clock oscillators have some jitter (usually picoseconds) due to thermal noise. You should always measure a clock's frequency by averaging many cycles.
 

mountain

Member level 2
Joined
May 22, 2004
Messages
49
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
649
Re: CLK Divider HELP

nand_gates said:
Here goes the Verilog code!


Code:
module clk_div(clk_50mhz, rst_n, clk_60hz);
   input clk_50mhz, rst_n;
   output      clk_60hz;
   reg         clk_60hz;
   
   reg [47:0]  counter;

   always @(posedge clk_50mhz or negedge rst_n) begin
      if (!rst_n) begin
         counter <= 48'h00000;
         clk_60hz <= 1'b0;
      end else begin
        if (counter == 48'h65B9A) begin
           counter <= 48'h00000;
           clk_60hz <= ~clk_60hz;
        end else begin
           counter <= counter + 1'b1;
        end
      end // else: !if(!rst_n)
   end // always @ (posedge clk_50mhz or negedge rst_n)
endmodule // clk_div

1.
In the port definition, thers is
output clk_60hz;
reg clk_60hz;
why the "reg clk_60hz;" is needed? I met this type many times, but I don't know the reason.

2.
The counter is so long(48bit), so it will work well?
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,933
Helped
638
Reputation
1,274
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,176
CLK Divider HELP

mountain,

1. Without the reg statement, the output signal would default to a wire instead of a register.
You can combine the two statements: output reg clk_60hz;

2. No, 48 bits is overkill. The upper 29 bits are always zero.

By the way, this module does not generate 60 Hz. It generates approximately 59.999952 Hz.
 

blueskystone

Newbie level 4
Joined
Dec 4, 2005
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,371
CLK Divider HELP

always @(posedge clk_50mhz or negedge rst_n) begin
if (!rst_n) begin

this design is ok , it is just an asynchronous RESET
, not a dual edge filp-flop.
it's suit for CPLD/FPGA .
 

kumar_eee

Advanced Member level 3
Joined
Sep 22, 2004
Messages
814
Helped
139
Reputation
276
Reaction score
113
Trophy points
1,323
Location
Bangalore,India
Activity points
4,676
Re: CLK Divider HELP

Can any1 explain abt Clock Doubler & How to implement it?....
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,933
Helped
638
Reputation
1,274
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,176
Re: CLK Divider HELP

kumar_eee said:
Can any1 explain abt Clock Doubler & How to implement it?....
Implement it in what type of chip?
Modern FPGAs have built-in frequency synthesizers, such as the DCM in Xilinx devices. These can be used to multiply/divide the input clock frequency by various ratios.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top