Continue to Site

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.

Simulation about 30MHz -> 1Hz clock divider

Status
Not open for further replies.

Xilinx_Modelsim

Newbie level 4
Joined
Sep 28, 2019
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
34
Hi everyone,

I have a question about simulation (30MHz -> 1Hz clock divider).

I used a VHDL code and Modelsim Tool for clock divider.
Then, I want to get a simulation result about 50% duty cycle of 1Hz clock.
I got a result for my simulation about 0.499s rising clock and 0.98 falling clock.

*How can I get a accurate simulation result of 50% duty cycle 1Hz clock?

Thanks for your advices.
 

Code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
 
entity Clock_Divider is
port ( clk,reset: in std_logic;
clk_out: out std_logic);
end Clock_Divider;
 
architecture bhv of Clock_Divider is
 
signal count: integer:=0;
signal tmp : std_logic := '0';
 
begin
 
process(clk)
begin
if(clk'event and clk='1') then
  count <=count+1;
  if (count = 15015015) then
    tmp <= NOT tmp;
    count <= 1;
  end if;
end if;
clk_out <= tmp;
end process;
 
end bhv;
-----------------------------------------------------------------------------
*** testbench ***
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
 
ENTITY Tb_clock_divider IS
END Tb_clock_divider;
 
ARCHITECTURE behavior OF Tb_clock_divider IS
 
-- Component Declaration for the Unit Under Test (UUT)
 
COMPONENT Clock_Divider
PORT(
clk : IN std_logic;
reset : IN std_logic;
clock_out : OUT std_logic
);
END COMPONENT;
 
--Inputs
signal clk : std_logic := '0';
signal reset : std_logic := '0';
 
--Outputs
signal clk_out: std_logic;
 
-- Clock period definitions
constant clk_period : real := 30000000;
constant clk_1 : time := 1/clk_period;
constant rising_time : time := clk_1/2;
constant falling_time : time := clk_1 - rising_time;
 
BEGIN
 
-- Instantiate the Unit Under Test (UUT)
uut: Clock_Divider PORT MAP (
clk => clk,
reset => reset,
clk_out=> clk_out
);
 
-- Clock process definitions
clk_process :process
begin
clk <= '0';
wait for falling_time;
clk <= '1';
wait for rising_time;
end process;

END;

Here is my VHDL code and testbench.
 

The test bench has been never compiled. Component declaration doesn't fit, clock_out versus clk_out.

No valid syntax in standard VHDL:
Code:
-- Clock period definitions
constant clk_period : real := 30000000;
constant clk_1 : time := 1/clk_period;
Apart from this points, I don't see a problem to get clk_out edges at exactly 0.5 s / 1.0 s.

The purpose of the clock divider isn't clear. Nobody uses a divided 1 Hz clock in a synchronous design, but it may be used in special cases. Normally we would implement an 1 Hz (or 2 Hz, depending on the purpose) clock enable pulse in the 30 MHz clk domain.
 

Thanks for your reply.
I even tried to simulate my VHDL code and testbench, but I couldn`t get a accurate 50% duty cycle of 1Hz.
How can I do?
 

Hi,

The usual way is to use a counter.
Count from 0 to 14,999,999, then toggle the output and clear the counter.

May I ask why you use 15015015?
 

Hi, KlausST

Because I calculated clock cycle like this : 0.0333us * n = 0.5s
So, I got counter number 15015015.
Is it a problem with simulation?
 

Re: Simulation about 30MHz -&gt; 1Hz clock divider

Hi, KlausST

Because I calculated clock cycle like this : 0.0333us * n = 0.5s
So, I got counter number 15015015.
Is it a problem with simulation?
!? I don't get how you came up with that number

30 MHz clock has 30,000,000 cycles per second....
So if you count for 30,000,000 clock cycles you have 1 second
if you count for 15,000,000 clock cycles you have 1/2 second and using that to toggle the clock is what you want not 15,015,015, and I still have no clue how you came up with that value.

- - - Updated - - -

Hmm, I have an idea how you got there...too much rounding of significant digits in the calculations. That is why you never never do 1/xxxx in a calculation unless you have a lot of precision and lots of significant digits.
You 15015015 is a 66.6ns period or 33.3ns clock period instead of the 33.333333333333333333333333333333333333333.....ns clock period for 30 MHz (which is why you don't want to calculate repeating decimal rational numbers using limited precision as you will end up getting with errors like this unless you properly round the results. Since your equation (0.0333 * n = 0.5) has only 3 significant digits you should only use 3 significant digits in the result, i.e. the answer should be 15.0
 

Thanks for your reply.
I just stimulated clock period in testbench.
As your opinion, how can I get a 1Hz clock?
 

Thanks for your reply.
I just stimulated clock period in testbench.
As your opinion, how can I get a 1Hz clock?

Didn't I just explain that in the second paragraph above!?
 

ads-ee explained well how the erroneous number of 15015015 came up. The claimed period of 0.98 s can't be explained this way. As mentioned in post #4, I get exact 50% duty cycle, no matter if the divider is 15015015 or 15000000.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top