process(clk)--clk輸入時鐘;
begin
if(rst = '0') then --rst復位訊號;
clkout <= '0';
elsif(clk;event and clk = '1')then
clkout <= not clk;
end if;
end process;
但是如果實現乙個三分頻呢?? 是不是
3分頻器應該是每
1.5的
clock就
0變1、
1變0,但問題來了,哪來的
1.5個
clock?計數器並不能產生
1.5!!正源觸發與負源觸發的間隔時間剛好是
0.5個
clock?所以我們產生兩個
clock,乙個是
posedge clk,乙個是
negedge clk,最後將兩個
clock做
or,這樣就可以產生出
0.5個
clock了。下面給出**:::
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity clk_div_n is
port(clk : in std_logic;
rst : in std_logic;
clkout :out std_logic
);end clk_div_n;
architecture rtl of clk_div_n is
constant n : integer range 0 to 10 := 6; --這裡的n可以是任意值,當然要大於1.
signal clk_p : std_logic;
signal clk_n : std_logic;
signal cnt_p : integer range 0 to n;
signal cnt_n : integer range 0 to n;
begin
process(clk_p, clk_n)
begin
if((n mod 2) = 0)then
clkout <= clk_p;
else
clkout <= clk_p or clk_n;
end if;
end process;
process(clk, rst)
begin
if(rst = '0') then
cnt_p <= 0;
elsif(clk'event and clk = '1') then
if(cnt_p = n-1) then
cnt_p <= 0;
else
cnt_p <= cnt_p + 1;
end if;
end if;
end process;
process(clk, rst)
begin
if(rst = '0') then
clk_p <= '0';
elsif(clk'event and clk = '1')then
if (cnt_p < (n/2)) then
clk_p <= '1';
else
clk_p <= '0';
end if ;
end if;
end process;
process(clk, rst)
begin
if(rst = '0') then
cnt_n <= 0;
elsif(clk'event and clk = '0')then
if(cnt_n = n-1) then
cnt_n <= 0;
else
cnt_n <= cnt_n + 1;
end if;
end if;
end process;
process(clk, rst)
begin
if(rst = '0') then
clk_n <= '0';
elsif(clk'event and clk = '0')then
if (cnt_n < (n/2)) then
clk_n <= '1';
else
clk_n <= '0';
end if ;
end if;
end process;
end rtl;
接下來我給出對應的testbench::有興趣可以用make a simulation in modelsim
library ieee ;
use ieee.std_logic_1164.all ;
use ieee.std_logic_arith.all ;
use ieee.std_logic_unsigned.all ;
entity clk_div_n_tb is
end clk_div_n_tb;
architecture clk_div_tb_arch of clk_div_n_tb is
signal clkout : std_logic ;
signal rst : std_logic := '0' ;
signal clk : std_logic := '1' ;
component clk_div_n
port (
clk : in std_logic ;
rst : in std_logic ;
clkout : out std_logic
); end component ;
begin
process
begin
wait for 50ns;
clk <= not clk;
end process;
rst <= '1' after 200ns;
test:clk_div_n
port map (
clk => clk,
rst => rst,
clkout => clkout) ;
end clk_div_tb_arch;
來自為知筆記(wiz)
VHDL語言實現的任意整數分頻器。
fpga中,一般外接的晶振是50mhz,如果電路中乙個模組需要25mhz時鐘,那麼進行乙個2分頻,這個是相當容易的,下面是一種方法,還有可以用乙個二進位制計數器實現。這裡就不寫 了。easy.同樣的原理 四分頻也很容易。process clk clk輸入時鐘 begin if rst 0 then ...
VHDL語言實現的任意整數分頻器
process clk clk輸入時鐘 begin if rst 0 then rst復位訊號 clkout 0 elsif clk event and clk 1 then clkout not clk end if end process 但是如果實現乙個三分頻呢?是不是3分頻器應該是每1.5的...
Verilog任意整數分頻電路
define n 5module div n input clk,基準時鐘output clk div n,n分頻後得到的時鐘input rst wire 31 0 n 5 n為分頻係數,n 2即可,n的值為clk除以clk div n後取整 四捨五入 產生備用時鐘1 reg 31 0 cnt1 r...