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

📄 dianzhen.vhd

📁 实现一个10秒倒计时电路
💻 VHD
字号:

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity dianzhen is

	port 
	(
		clk	,countbutton,statebutton: in std_logic;--计数开关和预置开关
		clk1,clk2,clk3,clk4: buffer std_logic;--分频时钟
		row,col:out std_logic_vector(7 downto 0);--行列选通信号
		voice:out std_logic;--蜂鸣器报警信号
	    q1:buffer integer range 0 to 10;--十进制计数
        press:buffer integer range 0 to 10;--置位次数
		q:out std_logic_vector(7 downto 0)--彩灯显示
	);

end entity;

architecture rtl of dianzhen  is

	signal qtemp:integer range 0 to 49999;
	signal qtemp2:integer range 0 to 1000;
	signal qtemp3:integer range 0 to 20000;
	signal qtemp4:integer range 0 to 700000000;
	--signal q1:integer range 0 to 10;--十进制计数
	signal q2:std_logic_vector(2 downto 0);
	type all_state is (s0,s1,s2,s3,s4,s5,s6,s7);
    signal state:all_state;
    signal flag1:integer;--蜂鸣器控制信号
    signal flag2:integer;--彩灯控制信号
    --signal press:integer range 0 to 10;--置位次数
begin
--把50M分频为1K,作为行列控制信号的时钟
p1:process(countbutton,clk)
	begin

	if(clk'event and clk='1') then
		if qtemp=49999 then
		qtemp<=0;
		else
		qtemp<=qtemp+1;
		end if;
	
        if qtemp<49999 then
           clk1<='0';
        else
           clk1<='1';
        end if;
    end if;

end process p1;


--把1K分频为1Hz,作为显示各数字的时钟
p2:process(countbutton,clk1)
	begin
	if(clk1'event and clk1='1') then
		if qtemp2=1000 then
		   qtemp2<=0;
		else
		   qtemp2<=qtemp2+1;
		end if;	
		
        if qtemp2<1000 then
           clk2<='0';
        else
           clk2<='1';
        end if;
    end if;

end process p2;

--蜂鸣器时钟
p3:process(clk)
   begin
   if clk'event and clk='1' then 
        if qtemp3=20000 then
           qtemp3<=0;
        else 
           qtemp3<=qtemp3+1;
        end if;
        
        if qtemp3<20000 then
           clk3<='0';
        else 
           clk3<='1';
        end if;
    end if;
end process p3;
p4:process(clk)
   begin
   if clk'event and clk='1' then 
        if qtemp4=700000000 then
           qtemp4<=0;
        else 
           qtemp4<=qtemp4+1;
        end if;
        
        if qtemp4<700000000 then
           clk4<='0';
        else 
           clk4<='1';
        end if;
    end if;
end process p4;

--预置开关的设置
yuzhi:process(statebutton, clk1)
begin
 if clk4'event and clk4='1' then
 if  statebutton'event and statebutton='1' then
    if press=0 then 
       press<=10;
    else 
       press<=press-1;
    end if;
 end if;
end if;
 end process yuzhi;
    
--用模为N(N取10到0)的计数器产生各个倒计时计数状态
jishu:process(clk2,press)
--variable s:integer range 0 to 10;
begin
    if clk2'event and clk2='1' then 
    case  press is
                   
     when 10=>--预置数为10
              q1<=10;
              if countbutton='1' then
   		          if q1=0 then
		             q1<=10;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
            
        when 9=>--预置数为9
            
               q1<=9;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=9;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
            
        when 8=>--预置数为8
            
               q1<=8;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=8;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
            
        when 7=>--预置数为7
            
               q1<=7;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=7;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
            
        when 6=>--预置数为6
            
               q1<=6;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=6;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
            
        when 5=>--预置数为5

               q1<=5;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=5;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
        
        when 4=>--预置数为4

               q1<=4;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=4;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	   
     
        when 3=>--预置数为3

               q1<=3;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=3;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
            
        when 2=>--预置数为2
            
               q1<=2;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=2;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
            
        when 1=>--预置数为1
            
               q1<=1;
	           if countbutton='1' then
   		          if q1=0 then
		             q1<=1;
   		          else
		             q1<=q1-1;
    	          end if;
    	       end if;	
    	       
        when others=>
               q1<=0;
               
   end case;
end if;
end process jishu;

--行列控制信号 
control:process(clk1)
begin
if clk1'event and clk1='1' then
    if q2="111"then
	  q2<="000";
    else
	  q2<=q2+1;
    end if;
end if;
end process control;

--用计数器1控制显示哪个数字
choose:process(clk1,q1,q2)
begin
if (clk1'event and clk1='1') then
    case q1 is
         
        when 10=> --显示数字10
            flag1<=0;--彩灯关                 
                 case q2 is
            when"000"=>row<="01111111";col<="00101110";
            when"001"=>row<="10111111";col<="00101010";
            when"010"=>row<="11011111";col<="00101010";
            when"011"=>row<="11101111";col<="00101010";
            when"100"=>row<="11110111";col<="00101010";
            when"101"=>row<="11111011";col<="00101010";
            when"110"=>row<="11111101";col<="00101010";
            when"111"=>row<="11111110";col<="00101110";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
  
        when 9=>--显示数字9
            flag1<=0;    
                 case q2 is
            when"000"=>row<="01111111";col<="00111100";
            when"001"=>row<="10111111";col<="00100100";
            when"010"=>row<="11011111";col<="00100100";
            when"011"=>row<="11101111";col<="00111100";
            when"100"=>row<="11110111";col<="00000100";
            when"101"=>row<="11111011";col<="00000100";
            when"110"=>row<="11111101";col<="00000100";
            when"111"=>row<="11111110";col<="00111100";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
       
         when 8=>--8
            flag1<=0;
                 case q2 is
            when"000"=>row<="01111111";col<="00111100";
            when"001"=>row<="10111111";col<="00100100";
            when"010"=>row<="11011111";col<="00100100";
            when"011"=>row<="11101111";col<="00111100";
            when"100"=>row<="11110111";col<="00100100";
            when"101"=>row<="11111011";col<="00100100";
            when"110"=>row<="11111101";col<="00100100";
            when"111"=>row<="11111110";col<="00111100";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
                 
         when 7=>--7  
            flag1<=0;
                 case q2 is
            when"000"=>row<="01111111";col<="00111100";
            when"001"=>row<="10111111";col<="00000100";
            when"010"=>row<="11011111";col<="00000100";
            when"011"=>row<="11101111";col<="00000100";
            when"100"=>row<="11110111";col<="00000100";
            when"101"=>row<="11111011";col<="00000100";
            when"110"=>row<="11111101";col<="00000100";
            when"111"=>row<="11111110";col<="00000100";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
                 
         when 6=>--6
            flag1<=0;
	              case q2 is
            when"000"=>row<="01111111";col<="00111100";
            when"001"=>row<="10111111";col<="00100000";
            when"010"=>row<="11011111";col<="00100000";
            when"011"=>row<="11101111";col<="00111100";
            when"100"=>row<="11110111";col<="00100100";
            when"101"=>row<="11111011";col<="00100100";
            when"110"=>row<="11111101";col<="00100100";
            when"111"=>row<="11111110";col<="00111100";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
                 
         when 5=>--5
            flag1<=0;
                 case q2 is
            when"000"=>row<="01111111";col<="00111100";
            when"001"=>row<="10111111";col<="00100000";
            when"010"=>row<="11011111";col<="00100000";
            when"011"=>row<="11101111";col<="00111100";
            when"100"=>row<="11110111";col<="00000100";
            when"101"=>row<="11111011";col<="00000100";
            when"110"=>row<="11111101";col<="00000100";
            when"111"=>row<="11111110";col<="00111100";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
                 
         when 4=>--4
            flag1<=0;
                  case q2 is
            when"000"=>row<="01111111";col<="00100100";
            when"001"=>row<="10111111";col<="00100100";
            when"010"=>row<="11011111";col<="00100100";
            when"011"=>row<="11101111";col<="00111100";
            when"100"=>row<="11110111";col<="00000100";
            when"101"=>row<="11111011";col<="00000100";
            when"110"=>row<="11111101";col<="00000100";
            when"111"=>row<="11111110";col<="00000100";
            when others=>row<="00000000";col<="00000000" ;
                  end case;
                  
         when 3=>--3
            flag1<=0;
                  case q2 is
            when"000"=>row<="01111111";col<="00111100";
            when"001"=>row<="10111111";col<="00000100";
            when"010"=>row<="11011111";col<="00000100";
            when"011"=>row<="11101111";col<="00111100";
            when"100"=>row<="11110111";col<="00000100";
            when"101"=>row<="11111011";col<="00000100";
            when"110"=>row<="11111101";col<="00000100";
            when"111"=>row<="11111110";col<="00111100";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
                 
         when 2=>--2
            flag1<=0;
                 case q2 is
            when"000"=>row<="01111111";col<="00111100";
            when"001"=>row<="10111111";col<="00000100";
            when"010"=>row<="11011111";col<="00000100";
            when"011"=>row<="11101111";col<="00111100";
            when"100"=>row<="11110111";col<="00100000";
            when"101"=>row<="11111011";col<="00100000";
            when"110"=>row<="11111101";col<="00100000";
            when"111"=>row<="11111110";col<="00111100";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
                 
        when 1=>--1
            flag1<=0;
                  case q2 is
            when"000"=>row<="01111111";col<="00000100";
            when"001"=>row<="10111111";col<="00000100";
            when"010"=>row<="11011111";col<="00000100";
            when"011"=>row<="11101111";col<="00000100";
            when"100"=>row<="11110111";col<="00000100";
            when"101"=>row<="11111011";col<="00000100";
            when"110"=>row<="11111101";col<="00000100";
            when"111"=>row<="11111110";col<="00000100";
            when others=>row<="00000000";col<="00000000" ;
                 end case;
                 
        when others=>--计数完毕时
            flag1<=0;
                  case q2 is
            when"000"=>row<="01111111";col<="00111100";
            when"001"=>row<="10111111";col<="00100100";
            when"010"=>row<="11011111";col<="00100100";
            when"011"=>row<="11101111";col<="00100100";
            when"100"=>row<="11110111";col<="00100100";
            when"101"=>row<="11111011";col<="00100100";
            when"110"=>row<="11111101";col<="00100100";
            when"111"=>row<="11111110";col<="00111100";
            when others=>row<="00000000";col<="00000000" ;
                  end case;
            voice<=clk3;--蜂鸣器响
            flag1<=1;
    end case;

end if;
end process choose;
 
caideng:process(clk2,flag1,state)--彩灯显示
begin
    if clk2'event and clk2='1' then    --彩灯亮          
        if flag1=1 then
          case state is
	when s0=> state<=s1;q<="00011000";
	when s1=> state<=s2;q<="00111100";
	when s2=> state<=s3;q<="01111110";
	when s3=> state<=s4;q<="11111111";
	when s4=> state<=s5;q<="01111110";
	when s5=> state<=s6;q<="00111100";
	when s6=> state<=s7;q<="00011000";
	when s7=> state<=s0;q<="00000000";
	when others=> state<=s0;q<="00000000";
           end case;
        end if;
    end if;
     
end process caideng;    
end rtl;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -