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

📄 alarm_controller.vhd

📁 电子闹钟:基于fpga的电子闹钟设计
💻 VHD
字号:



--控制器源程序alarm_controller.vhd
library ieee;
use ieee.std_logic_1164.all;
use work.p_alarm.all;
entity alarm_controller is
port( key : in std_logic;
      time_button : in std_logic;
      alarm_button : in std_logic;
      clk : in std_logic;
      reset : in std_logic;
      load_new_A : out std_logic;
      load_new_C : out std_logic;
      show_new_time : out std_logic;
      show_A : out std_logic);
end entity;
architecture behav of alarm_controller is
  type t_state is (s0,s1,s2,s3,s4);
  constant key_timeout : t_short :=50000;
  constant show_alarm_timeout :t_short :=50000;
  signal curr_state : t_state;
  signal next_state : t_state;
  signal counter_k : t_short;
  signal enable_count_k : std_logic;
  signal count_k_end : std_logic;
  signal counter_A : t_short;
  signal enable_count_A : std_logic;
  signal count_A_end : std_logic;
begin
  process(clk,reset) 
  begin
    if reset='1' then 
      curr_state<=s0;
    elsif rising_edge(clk) then
      curr_state<=next_state;
    end if;
  end process;

  process(key,alarm_button,time_button,curr_state,count_A_end,count_k_end) is
  begin
    next_state<=curr_state;
    load_new_A<='0';
    load_new_C<='0';
    show_A<='0';
    show_new_time<='0';
    enable_count_k<='0';
    enable_count_A<='0';
    case curr_state is
      when s0=>               --电路初态即正常时钟计数状态
         if (key='1') then
           next_state<=s1;
           show_new_time<='1';
         elsif (alarm_button='1') then
           next_state<=s4;
           show_A<='1';
         else 
           next_state<=s0;
         end if;     
      when s1=>    --接收键盘输入状态,在s0状态下按下数字键进入此状态,显示用户键入的数字
         if (key='1') then
           next_state<=s1;
           show_new_time<='1'; -- 应该有的输出动作,但是书上没有
         elsif (alarm_button='1') then
           next_state<=s2;
           load_new_A<='1';
         elsif (time_button='1') then
           next_state<=s3;
           load_new_C<='1';
         else
           if (count_k_end='1') then
             next_state<=s0;
           else
             next_state<=s1;
           end if;
           enable_count_k<='1';
         end if;
         show_new_time<='1'; 
      when s2=>   --设置新的闹钟时间。在s1状态下alarm键进入此状态
         if (alarm_button='1') then
           next_state<=s2;
           load_new_A<='1';
         else
           next_state<=s0;
         end if;
      when s3=>   --设置新的计数器时间。在s1状态下用户按下time键后进入此状态
         if (time_button='1') then
           next_state<=s3;
           load_new_C<='1';
         else
           next_state<=s0;
         end if;
      when s4=>    --显示闹钟时间。在s0时按下alarm进入此状态
         if (key='1') then
           next_state<=s1;
         else
           next_state<=s4;
           enable_count_A<='1';
           if (count_A_end='1') then
             next_state<=s0;
           else
             next_state<=s4;
             show_A<='1';
           end if;
           
         end if;
      when others=>
         null;
      end case;
    end process;
  count_key : process(enable_count_k,clk) is
  begin
    if (enable_count_k='0' ) then
      counter_k <=0;
      count_k_end<='0';
    elsif (rising_edge(clk)) then
      if (counter_k>=key_timeout) then
        count_k_end<='1';
      else
        counter_k<=counter_k+1;
      end if;
    end if;
  end process count_key;
  count_alarm : process(enable_count_A,clk) is
  begin
    if (enable_count_A='0') then
      counter_A<=0;
      count_A_end<='0';
    elsif (rising_edge(clk)) then
      if (counter_A=show_alarm_timeout) then
        count_A_end<='1';
      else
        counter_A<=counter_A+1;
      end if;
    end if;
  end process count_alarm;
end behav;
    

⌨️ 快捷键说明

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