?? dac0832.vhd
字號:
--文件名:dac0832.vhd
--功 能:0~5伏可調數字電壓源
--說 明:1.這里是以5伏為基準電壓
-- 2.通過八位撥盤開關對當前的電壓進行數字設定
-- 3.數碼管顯示當前電壓值
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity dac0832 is
Port (clk : in std_logic;
datin : in std_logic_vector(7 downto 0);
shift : out std_logic_vector(3 downto 0);
dout_0832 : out std_logic_vector(7 downto 0);
dout_led : out std_logic_vector(7 downto 0);
cs_led : out std_logic_vector(1 downto 0);
wr,cs:out std_logic);
end dac0832;
architecture Behavioral of dac0832 is
type states is (st0,st1);
signal state:states:=st0;
type state1 is (st0,st1,st2);
signal current_state1 : state1;
type state2 is (st0,st1,st2,st3,st4);
signal current_state2 : state2;
signal reg_dout : std_logic_vector(15 downto 0);
signal dout : std_logic_vector(4 downto 0);
signal clk100k,clk1k,clk100 : std_logic;
begin
dout_0832<=datin;
--系統分頻
process(clk)
variable cnt : integer range 0 to 500; --產生100KHz的頻率;
begin
if rising_edge(clk) then cnt:=cnt+1;
if cnt<250 then clk100k<='0';
elsif cnt<500 then clk100k<='1';
else cnt:=0;clk100k<='0';
end if;
end if;
end process;
process(clk100k)
variable cnt: integer range 0 to 100; --產生1KHz的頻率;
begin
if rising_edge(clk100k) then cnt:=cnt+1;
if cnt<50 then clk1k<='0';
elsif cnt<100 then clk1k<='1';
else cnt:=0;clk1k<='0';
end if;
end if;
end process;
process(clk1k)
variable cnt : integer range 0 to 10;--產生100Hz的頻率;
begin
if rising_edge(clk1k) then cnt:=cnt+1;
if cnt<5 then clk100<='0';
elsif cnt<10 then clk100<='1';
else cnt:=0;clk100<='0';
end if;
end if;
end process;
--dac0832的控制程序
process(clk100k)
begin
if rising_edge(clk100k) then
case state is --狀態之間的轉換
when st0=>wr<='1'; cs<='1'; state<=st1;
when st1=>wr<='0'; cs<='0'; state<=st0;
when others=>null;
end case;
end if;
end process;
--十進制-BCD碼轉換;
process(clk100)
variable reg : integer range 0 to 8000;
variable d1,d2,d3,d4 : std_logic_vector(3 downto 0);
begin
if clk100'event and clk100='1' then
case current_state1 is
when st0=>
reg:=conv_integer(datin)*20; --將輸入的二進制信號轉換成十進制再乘以電壓系數=當前電壓值
d1:="0000";d2:="0000";d3:="0000";d4:="0000";
current_state1<=st1;
when st1=>
if reg>999 then reg:=reg-1000;d1:=d1+1;
elsif reg>99 then reg:=reg-100;d2:=d2+1;
elsif reg>9 then reg:=reg-10;d3:=d3+1;
elsif reg>0 then reg:=reg-1;d4:=d4+1;
else current_state1<=st2;
end if;
when st2=>
reg_dout<=d1&d2&d3&d4;
current_state1<=st0;
when others=>
current_state1<=st0;
end case;
end if;
end process;
--動態掃描控制;
process(clk1k)
begin
if clk1k'event and clk1k='1' then
case current_state2 is
when st0=> --熄滅與本實驗無關的發光二極管
cs_led<="11";
shift<="1111";
dout<="11111";
current_state2<=st1;
when st1=> --在數碼管的最高位顯示數據
cs_led<="10";
shift<="0111";
dout<='0'®_dout(15 downto 12);
current_state2<=st2;
when st2=> --在數碼管的次高位顯示數據
cs_led<="10";
shift<="1011";
dout<='1'®_dout(11 downto 8);
current_state2<=st3;
when st3=> --在數碼管的次低位顯示數據
cs_led<="10";
shift<="1101";
dout<='1'®_dout(7 downto 4);
current_state2<=st4;
when st4=> --在數碼管的最低位顯示數據
cs_led<="10";
shift<="1110";
dout<='1'®_dout(3 downto 0);
current_state2<=st1;
when others=>
current_state2<=st0;
end case;
end if;
end process;
--將BCD碼進行8段譯碼(包括小數點)
code1: process (dout)
begin
case dout(3 downto 0) is
when "0000"=>dout_led<=dout(4)&"0000001";--dout(4)代表小數點,低電平點亮
when "0001"=>dout_led<=dout(4)&"1001111";
when "0010"=>dout_led<=dout(4)&"0010010";
when "0011"=>dout_led<=dout(4)&"0000110";
when "0100"=>dout_led<=dout(4)&"1001100";
when "0101"=>dout_led<=dout(4)&"0100100";
when "0110"=>dout_led<=dout(4)&"0100000";
when "0111"=>dout_led<=dout(4)&"0001111";
when "1000"=>dout_led<=dout(4)&"0000000";
when "1001"=>dout_led<=dout(4)&"0000100";
when others=>dout_led<="11111111";
end case;
end process;
end Behavioral;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -