|
呵呵,好爽啊,前面幾個(gè)星期看電源看得我頭都大啦,也落了不少課,前幾天我突然發(fā)現(xiàn)我的EDA落了很多東西啦,唉,心想,把電源放放,看看EDA再說(shuō),突然有了興趣,學(xué)起來(lái)也挺快的,前面兩天我寫(xiě)了一個(gè)能夠?qū)崿F(xiàn)加減的計(jì)數(shù)器,并且可以選擇加1,10,100,1000。呵呵,這個(gè)程序?qū)懴聛?lái),對(duì)VHDL里面的狀態(tài)機(jī)有了初步的了解,遇到的問(wèn)題挺多的,比如說(shuō)一個(gè)進(jìn)程里面是不能同時(shí)有上升沿和下降沿判斷的。可是我還沒(méi)有通過(guò)硬件測(cè)試,我只是簡(jiǎn)單的用軟件仿真看了下,感覺(jué)是對(duì)的。明天做實(shí)驗(yàn)時(shí)檢測(cè)正確后,我再把源碼貼上來(lái)。今天從下午6點(diǎn)多到現(xiàn)在才把這個(gè)分頻器個(gè)整出來(lái)。具體的功能是:對(duì)一個(gè)頻率進(jìn)行任意分頻,分頻數(shù)可以通過(guò)按鍵輸入,且分頻數(shù)可以加減,不過(guò)加減數(shù)為1。還有一點(diǎn)不足的是分頻數(shù)沒(méi)有通過(guò)數(shù)碼管顯示出來(lái)。等明天把我前兩天寫(xiě)得計(jì)數(shù)器調(diào)試好后有時(shí)間再添上去吧,因?yàn)樗皇且粋(gè)例化語(yǔ)句的使用,應(yīng)該不會(huì)難倒我。
下面是分頻器的源代碼:
【注】:仿真圖我就不貼上來(lái)啦,提醒一下,仿真時(shí)要把pclk設(shè)置幾個(gè)脈沖后,后面就讓它為低電平,因?yàn)殡娔X有點(diǎn)難受的。還有rst開(kāi)始一段很短時(shí)間要設(shè)置為高電平,clk要在pclk為低電平后在設(shè)置它的時(shí)鐘。至于怎么設(shè),相信大家應(yīng)該可以搞定。這樣設(shè)置有利于看仿真結(jié)果。同時(shí)電腦的CPU不至于崩潰。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity foudiv is
port(
clk,pclk :in std_logic; --clk:待分頻時(shí)鐘 pclk:按鍵
clkmd:in std_logic;--按鍵加減模式
rst:in std_logic;--復(fù)位,
fout:out std_logic--頻率輸出
);
end;
architecture lammy of foudiv is
signal up:std_logic;--上升沿計(jì)數(shù)結(jié)束標(biāo)志位
signal do:std_logic;--下降沿計(jì)數(shù)結(jié)束標(biāo)志位
signal full:std_logic;--分頻頻率輸出狀態(tài)位
signal fullup:std_logic;--分頻頻率翻轉(zhuǎn)標(biāo)志位
signal fulldo:std_logic;--分頻頻率翻轉(zhuǎn)標(biāo)志位
signal db:std_logic_vector(7 downto 0);--分頻數(shù)
signal du:std_logic_vector(7 downto 0);--上升沿計(jì)數(shù)器
signal dd:std_logic_vector(7 downto 0);--下降沿計(jì)數(shù)器
begin
lammy01:process(pclk,rst,clkmd)
begin
if rst='1' then db<=(others=>'0');
elsif pclk'event and pclk='1' then
if clkmd='1' then db<=db+1;
elsif clkmd='0' then db<=db-1;
end if;
end if;
end process;
lammy02:process(clk)
variable updata:std_logic_vector(7 downto 0);
begin
if rst='1' then fullup<='0';
elsif clk'event and clk='1' then
-- if updata=db-1 then updata:=(others=>'0');
if do='1' then updata:=(others=>'0');
end if;
updata:=updata+1;
if dd+updata=db then updata:=(others=>'0');fullup<='1';up<='1';
else fullup<='0';up<='0';
end if;
end if;
du<=updata;
end process;
lammy03:process(clk)
variable dodata:std_logic_vector(7 downto 0);
begin
if rst='1' then fulldo<='0';
elsif clk'event and clk='0' then
-- if dodata=db-1 then dodata:=(others=>'0');
if up='1' then dodata:=(others=>'0');
end if;
dodata:=dodata+1;
if du+dodata=db then dodata:=(others=>'0');fulldo<='1';do<='1';
else fulldo<='0';do<='0';
end if;
end if;
dd<=dodata;
end process;
lammy04:process(fulldo,fullup)
begin
full<=fulldo or fullup;
end process;
lammy05:process(full)
variable cnt2:std_logic;
begin
if full'event and full='1' then cnt2:=not cnt2;
if cnt2='1' then fout<='1';
else fout<='0';
end if;
end if;
end process;
end;
|
|