計數器簡介:
計數器是數字系統中用得較多的基本邏輯器件。它不僅能記錄輸入時鐘脈沖的個數,還可以實現分頻、定時、產生節拍脈沖和脈沖序列等。例如,計算機中的時序發生器、分頻器、指令計數器等都要使用計數器。 計數器的種類很多。按時鐘脈沖輸入方式的不同,可分為同步計數器和異步計數器;按進位體制的不同,可分為二進制計數器和非二進制計數器;按計數過程中數字增減趨勢的不同,可分為加計數器、減計數器和可逆計數器。
以下是VHDL代碼和仿真:
- library ieee;
- use ieee.std_logic_1164.all;
- use ieee.std_logic_arith.all;
- use ieee.std_logic_unsigned.all;
- --------------------------------------------------------------------
- entity exp3 is
- port( clk,ret,en : in std_logic; --定義時鐘、異步復位、同步使能信號
- cq : out std_logic_vector(3 downto 0); --計數結果
- cout : out std_logic --進位信號
- );
- end exp3;
- --------------------------------------------------------------------
- architecture behave of exp3 is
- begin
- process(clk,ret,en)
- variable cqi : std_logic_vector(3 downto 0);
- begin
- if ret='0' then cqi:=(others =>'0');-- 計數器異步復位
- elsif clk'event and clk='1' then--檢測時鐘上升沿
- if en='1' then--檢測是否允許計數(同步使能)
- if cqi<15 then cqi:=cqi+1;
- else cqi:=(others =>'0');
- end if;
- end if;
- end if;
- if cqi>9 then cout<='1';--輸出進位信號
- else cout<='0';
- end if;
- cq<=cqi;--計數值向端口輸出
- end process;
- end behave;
復制代碼
代碼下載:
16位計數器.7z
(188.25 KB, 下載次數: 10)
2024-6-27 15:26 上傳
點擊文件名下載附件
|