⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dac0832.vhd

📁 一些很好的FPGA设计实例
💻 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'&reg_dout(15 downto 12);
			 current_state2<=st2;
      when st2=>							 --在数码管的次高位显示数据
	           cs_led<="10";
			 shift<="1011";
			 dout<='1'&reg_dout(11 downto 8);
			 current_state2<=st3;
	 when st3=>							 --在数码管的次低位显示数据
	           cs_led<="10";
			 shift<="1101";
			 dout<='1'&reg_dout(7 downto 4);
			 current_state2<=st4;
      when st4=>							 --在数码管的最低位显示数据
	           cs_led<="10";
			 shift<="1110";
			 dout<='1'&reg_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 + -