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

📄 condition.vhd

📁 具有多种功能的电子钟:闹钟
💻 VHD
字号:
library ieee;  --状态机
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity condition is
  port(clk:in std_logic;
	   keya,keyb,keye,keyf,keyg:in std_logic;  --a确认;b取消;c报时开关;
													  -- d闹钟开关;e设置时间;f设置闹钟;
                                                      -- g数值增加
       buffertime: buffer std_logic_vector(23 downto 0); --缓存时间
       iscount:out std_logic;                           --计数
       alarmload:out std_logic;                         --闹钟
	   timeload: out std_logic);                       --设置时间
                                 
end condition;
architecture con of condition is
--状态设置
 type state is (count,m1,m2,m3,m4,m5,m6,m7,loadtime,
	  			      s1,s2,s3,s4,s5,s6,s7,loadalarm);
 signal pre_state :state:=count;
 signal next_state :state:=count;
 signal temp:std_logic_vector(23 downto 0);  --时间
 signal q:integer:=0;
begin
  process(clk)
begin
	if(clk'event and clk='1')then
		pre_state<=next_state;
  end if;
end process;

process(keyg)
  begin
	if(keyg'event and keyg='1')then
      if(q=10)then
      q<=0;
     else  
      q<=q+1;
    end if;
 end if;
end process;



process(clk,pre_state,q)
begin
  if(clk'event and clk='0')then
    case pre_state is
   when count=>                        --计数态
				if(keye='1')then
                   next_state<=m1;
                elsif(keyf='1')then
                   next_state<=s1;
                else
				   next_state<=pre_state;
                end if;
                temp<=(others=>'0');
   
   when m1=>                            --小时高位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=2 and keya='1')then
                    next_state<=m2;
                    temp(23 downto 20)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;
  
    when m2=>                         --小时低位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<4 and keya='1'and temp(23 downto 20)="0010")then
                    next_state<=m3;
                    temp(19 downto 16)<=conv_std_logic_vector(q,4);
                 elsif(q<=9 and keya='1'and temp(23 downto 20)<"0010")then
                    next_state<=m3;
                    temp(19 downto 16)<=conv_std_logic_vector(q,4);
                  else  
                    next_state<=pre_state;
                    temp<=temp;
				end if;
   when m3=>                            --分钟高位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=5 and keya='1')then
                    next_state<=m4;
                    temp(15 downto 12)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;
  
   when m4=>                           --分钟低位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=9 and keya='1')then
                    next_state<=m5;
                    temp(11 downto 8)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;
                                              
   when m5=>                                   --秒高位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=5 and keya='1')then
                    next_state<=m6;
                    temp(7 downto 4)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;

   when m6=>                              --秒低位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=2 and keya='1')then
                    next_state<=loadtime;
                    temp(3 downto 0)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;
  
   
   when m7=>                             --确认输出
				if(keyb='1')then
                    next_state<=count;
                 elsif(keya='1')then
                    next_state<=loadtime;
                 else
					next_state<=pre_state;
                  end if;
                  temp<=temp;
   when loadtime=>                     --开始计数
				next_state<=count;
                temp<=temp;
				
     when s1=>                        --闹钟设置小时高位   
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=2 and keya='1')then
                    next_state<=s2;
                    temp(23 downto 20)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;
  
    when s2=>                         --小时低位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<4 and keya='1'and temp(23 downto 20)="0010")then
                    next_state<=s3;
                    temp(19 downto 16)<=conv_std_logic_vector(q,4);
                 elsif(q<=9 and keya='1'and temp(23 downto 20)<"0010")then
                    next_state<=s3;
                    temp(19 downto 16)<=conv_std_logic_vector(q,4);
                  else  
                    next_state<=pre_state;
                    temp<=temp;
				end if;
   when s3=>                                    --分钟高位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=5 and keya='1')then
                    next_state<=s4;
                    temp(15 downto 12)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;
  
   when s4=>                                  --分钟低位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=9 and keya='1')then
                    next_state<=s5;
                    temp(11 downto 8)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;

   when s5=>                             --秒高位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=5 and keya='1')then
                    next_state<=s6;
                    temp(7 downto 4)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;

   when s6=>                                   --秒低位
				if(keyb='1')then
                    next_state<=count;
                 elsif(q<=2 and keya='1')then
                    next_state<=s7;
                    temp(3 downto 0)<=conv_std_logic_vector(q,4);
                 else
					next_state<=pre_state;
                    temp<=temp;
				end if;
  
   
   when  s7=>                            --确认闹钟
				if(keyb='1')then
                    next_state<=count;
                 elsif(keya='1')then
                    next_state<=loadalarm;
                 else
					next_state<=pre_state;
                  end if;
                  temp<=temp;
   
   when loadalarm=>                      --开始计数
				next_state<=count;
                temp<=temp;
   
   --多态
  when others=>next_state<=count;
                temp<=temp;
  end case;
  end if;
end process;
--输出时间
buffertime<=temp;
--输出标志位
process(pre_state)
  begin
	if(pre_state=count)then
      iscount<='1';
    else
	  iscount<='0';
  end if;
	if(pre_state=loadtime)then
	  timeload<='1';
    else
	  timeload<='0';
  end if;	
	if(pre_state=loadalarm)then
	  alarmload<='1';
    else
	  alarmload<='0';
   end if;
end process;
end con;

  

⌨️ 快捷键说明

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