How to write a VHDL code for 1Hz signal?

V

Vagant

Hurrah! It works! I have used 50MHz clock as input and my 1Hz output
signal was connected to a LED. So it flashes every second now! Thanks
to all for encouragement, criticism, real help and suggestions!

P.S. Final version of the VHDL code used is:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;


entity sig_gen is
Port (clk : in STD_LOGIC;
reset_n : in STD_LOGIC;
clk_out : out STD_LOGIC);
end entity sig_gen;


architecture Behavioral of sig_gen is
signal clk_sig : std_logic;
begin
process(reset_n,clk)
variable cnt : integer;
begin
if (reset_n='0') then
clk_sig<='0';
cnt:=0;
elsif rising_edge(clk) then
if (cnt=24999999) then
clk_sig<=NOT(clk_sig);
cnt:=0;
else
cnt:=cnt+1;
end if;
end if;
end process;


clk_out <= clk_sig;


end Behavioral;
 
N

Nicolas Matringe

Vagant a écrit :
Hurrah! It works! I have used 50MHz clock as input and my 1Hz output
signal was connected to a LED. So it flashes every second now! Thanks
to all for encouragement, criticism, real help and suggestions!

P.S. Final version of the VHDL code used is:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;


Drop these last two non-standard libraries and use ieee.numeric_std instead.
The code you posted doesn't even need it anyway.

Nicolas
 
P

Ponceludon de Malavoy

50Mhz is 50 000 000 period per second. So you have to divide it by 50 000 000 go
get 1Hz, or to count two times from 0 to 24 999 999.
I hope the error was to see if someone follows ?
:)

Pascal


Correct. My mistake, sorry. :(
 
F

Florian Teply

No, on second thought, you're right. For a 50 MHz clock, you would
count to 24999 before toggling clk_out.

Well, not quitre right yet. 50MHz is 50 MILLION Hz, so for 1 Hz output
clock you're a factor of a thousand off track...

HTH,
Florian
 
V

Vagant

(e-mail address removed) schrieb:







Well, not quitre right yet. 50MHz is 50 MILLION Hz, so for 1 Hz output
clock you're a factor of a thousand off track...

HTH,
Florian- Hide quoted text -

- Show quoted text -

I used 24999999 (which is 50 millions divided by 2 minus 1) and it
really produces 1 Hz signal. :)
 
K

kennheinrich

I used 24999999 (which is 50 millions divided by 2 minus 1) and it
really produces 1 Hz signal. :)

Actually, the difference between 25e6 and 25e6-1 is well under 1 part
per million. You're unlikely to be generating 1 Hz with either
divisor; I'll go out on a limb and guess you're not using a calibrated
(atomic, GPS) 50 MHz oscillator and are probably lucky to be within 10
ppm to begin with. Using N-1 is clearly the theoretically correct
solution (for a piece of code in an appropriate style), as well as
being necessary system-wide in a lot of other ways, but don't expect
your output to be any better in a practical sense, nor to be correct
in an absolute sense :-(

- Kenn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,028
Messages
2,570,340
Members
46,981
Latest member
JorgeI4324

Latest Threads

Top