標題: 分頻器設計--VHDL源碼 [打印本頁]

作者: liuda    時間: 2015-1-23 21:37
標題: 分頻器設計--VHDL源碼
        呵呵,好爽啊,前面幾個星期看電源看得我頭都大啦,也落了不少課,前幾天我突然發(fā)現(xiàn)我的EDA落了很多東西啦,唉,心想,把電源放放,看看EDA再說,突然有了興趣,學起來也挺快的,前面兩天我寫了一個能夠實現(xiàn)加減的計數(shù)器,并且可以選擇加1,10,100,1000。呵呵,這個程序寫下來,對VHDL里面的狀態(tài)機有了初步的了解,遇到的問題挺多的,比如說一個進程里面是不能同時有上升沿和下降沿判斷的?墒俏疫沒有通過硬件測試,我只是簡單的用軟件仿真看了下,感覺是對的。明天做實驗時檢測正確后,我再把源碼貼上來。今天從下午6點多到現(xiàn)在才把這個分頻器個整出來。具體的功能是:對一個頻率進行任意分頻,分頻數(shù)可以通過按鍵輸入,且分頻數(shù)可以加減,不過加減數(shù)為1。還有一點不足的是分頻數(shù)沒有通過數(shù)碼管顯示出來。等明天把我前兩天寫得計數(shù)器調試好后有時間再添上去吧,因為它只是一個例化語句的使用,應該不會難倒我。
              下面是分頻器的源代碼:
【注】:仿真圖我就不貼上來啦,提醒一下,仿真時要把pclk設置幾個脈沖后,后面就讓它為低電平,因為電腦有點難受的。還有rst開始一段很短時間要設置為高電平,clk要在pclk為低電平后在設置它的時鐘。至于怎么設,相信大家應該可以搞定。這樣設置有利于看仿真結果。同時電腦的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:待分頻時鐘  pclk:按鍵
                     clkmd:in std_logic;--按鍵加減模式
                     rst:in std_logic;--復位,
                     fout:out std_logic--頻率輸出
              );
end;
architecture lammy of foudiv is
       signal up:std_logic;--上升沿計數(shù)結束標志位
       signal do:std_logic;--下降沿計數(shù)結束標志位
       signal full:std_logic;--分頻頻率輸出狀態(tài)位
       signal fullup:std_logic;--分頻頻率翻轉標志位
       signal fulldo:std_logic;--分頻頻率翻轉標志位
       signal db:std_logic_vector(7 downto 0);--分頻數(shù)
       signal du:std_logic_vector(7 downto 0);--上升沿計數(shù)器
       signal dd:std_logic_vector(7 downto 0);--下降沿計數(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;





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1