📄 statemachine.vhd
字号:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity statemachine is
Port ( reset : in std_logic;
buttonr : in std_logic;
buttonl : in std_logic;
preset_enable : in std_logic;
clk : in std_logic;
clk_1Hz:in std_logic;
--flag: out std_logic;
flash1: out std_logic_vector(3 downto 0);
day_cntl : out std_logic_vector(3 downto 0);
day_cnth : out std_logic_vector(3 downto 0);
month_cntl : out std_logic_vector(3 downto 0);
month_cnth : out std_logic_vector(3 downto 0);
year_cntl : out std_logic_vector(3 downto 0);
year_cntml : out std_logic_vector(3 downto 0);
year_cntmh : out std_logic_vector(3 downto 0);
year_cnth : out std_logic_vector(3 downto 0);
hour_cntl : out std_logic_vector(3 downto 0);
hour_cnth : out std_logic_vector(3 downto 0);
minute_cntl : out std_logic_vector(3 downto 0);
minute_cnth : out std_logic_vector(3 downto 0);
second_cntl : out std_logic_vector(3 downto 0);
second_cnth : out std_logic_vector(3 downto 0);
count_dayh : out std_logic_vector(3 downto 0);
count_dayl : out std_logic_vector(3 downto 0);
count_hourh : out std_logic_vector(3 downto 0);
count_hourl: out std_logic_vector(3 downto 0);
count_minuteh: out std_logic_vector(3 downto 0);
count_minutel : out std_logic_vector(3 downto 0);
how_many_hourh : out std_logic_vector(3 downto 0);
how_many_hourl : out std_logic_vector(3 downto 0));
end statemachine;
architecture Behavioral of statemachine is
signal current_state:std_logic_vector(4 downto 0);
signal next_state:std_logic_vector(4 downto 0);
signal cnt:std_logic_vector(3 downto 0);
signal flash: integer range 0 to 15:=15;
type state is (st1,st2,st3);
signal current :state;
begin
process(current_state,reset,next_state,clk)
begin
if reset='0'then
current_state<="00001";
elsif clk'event and clk='1'then
current_state<=next_state;
end if;
end process;
process(current_state,buttonr,buttonl,preset_enable)
begin
case current_state is
when "00001"=> --年的千位
--flag<='0';
flash<=0;
if preset_enable='0'then
year_cnth<=cnt;
end if;
if buttonr='0' then
next_state<="00010";
end if;
if buttonl='0' then
next_state<="11000";
end if;
when "00010"=> --年的百位
--flag<='1';
flash<=1;
if preset_enable='0'then
year_cntmh<=cnt;
end if;
if buttonr='0' then
next_state<="00011";
end if;
if buttonl='0' then
next_state<="00001";
end if;
when "00011"=> --年的十位
--flag<='0';
flash<=2;
if preset_enable='0'then
year_cntml<=cnt;
end if;
if buttonr='0' then
next_state<="00100";
end if;
if buttonl='0' then
next_state<="00010";
end if;
when "00100"=> --年的个位
--flag<='1';
flash<=3;
if preset_enable='0'then
year_cntl<=cnt;
end if;
if buttonr='0' then
next_state<="00101";
end if;
if buttonl='0' then
next_state<="00011";
end if;
when "00101"=> --月的十位
--flag<='0';
flash<=4;
if preset_enable='0'then
month_cnth<=cnt;
end if;
if buttonr='0' then
next_state<="00110";
end if;
if buttonl='0' then
next_state<="00100";
end if;
when "00110"=> --月的个位
--flag<='1';
flash<=5;
if preset_enable='0'then
month_cntl<=cnt;
end if;
if buttonr='0' then
next_state<="00111";
end if;
if buttonl='0' then
next_state<="00101";
end if;
when "00111"=> --日的十位
--flag<='0';
flash<=6;
if preset_enable='0'then
day_cnth<=cnt;
end if;
if buttonr='0' then
next_state<="01000";
end if;
if buttonl='0' then
next_state<="00110";
end if;
when "01000"=> --日的个位
--flag<='1';
flash<=7;
if preset_enable='0'then
day_cntl<=cnt;
end if;
if buttonr='0' then
next_state<="01001";
end if;
if buttonl='0' then
next_state<="00111";
end if;
when "01001"=>
--flag<='0';
flash<=8;
if preset_enable='0'then
hour_cnth<=cnt; --hour h
end if;
if buttonr='0' then
next_state<="01010";
end if;
if buttonl='0' then
next_state<="01000";
end if;
when "01010"=>
--flag<='1';
flash<=9;
if preset_enable='0'then
hour_cntl<=cnt; --hour l
end if;
if buttonr='0' then
next_state<="01011";
end if;
if buttonl='0' then
next_state<="01001";
end if;
when "01011"=>
-- flag<='0';
flash<=10;
if preset_enable='0'then
minute_cnth<=cnt; --minute h
end if;
if buttonr='0'then
next_state<="01100";
end if;
if buttonl='0' then
next_state<="01010";
end if;
when "01100"=>
--flag<='1';
flash<=11;
if preset_enable='0'then
minute_cntl<=cnt; --minute l
end if;
if buttonr='0'then
next_state<="01101";
end if;
if buttonl='0' then
next_state<="01011";
end if;
when "01101"=>
--flag<='0';
flash<=12;
if preset_enable='0'then
second_cnth<=cnt; --second h
end if;
if buttonr='0'then
next_state<="01110";
end if;
if buttonl='0' then
next_state<="01100";
end if;
when "01110"=>
--flag<='1';
flash<=13;
if preset_enable='0'then
second_cntl<=cnt; --second l
end if;
if buttonr='0'then
next_state<="01111";
end if;
if buttonl='0' then
next_state<="01101";
end if;
when "01111"=>
-- flag<='0';
-- flash<=16;
if preset_enable='0'then
count_dayh<=cnt; --count_dayh
end if;
if buttonr='0'then
next_state<="10000";
end if;
if buttonl='0' then
next_state<="01110";
end if;
when "10000"=>
--flag<='1';
--flash<=17;
if preset_enable='0'then
count_dayl<=cnt; --count_dayl
end if;
if buttonr='0'then
next_state<="10011";
end if;
if buttonl='0' then
next_state<="01111";
end if;
when "10011"=>
--flag<='0';
--flash<=18;
if preset_enable='0'then
count_hourh<=cnt; --count_hourh
end if;
if buttonr='0'then
next_state<="10100";
end if;
if buttonl='0' then
next_state<="10000";
end if;
when "10100"=>
-- flag<='1';
--flash<=19;
if preset_enable='0'then
count_hourl<=cnt; --count_hourl
end if;
if buttonr='0'then
next_state<="10101";
end if;
if buttonl='0' then
next_state<="10011";
end if;
when "10101"=>
-- flag<='0';
-- flash<=20;
if preset_enable='0'then
count_minuteh<=cnt; --count_minuteh
end if;
if buttonr='0'then
next_state<="10110";
end if;
if buttonl='0' then
next_state<="10100";
end if;
when "10110"=>
-- flag<='1';
--flash<=21;
if preset_enable='0'then
count_minutel<=cnt; --count_minutel
end if;
if buttonr='0'then
next_state<="10111";
end if;
if buttonl='0' then
next_state<="10101";
end if;
when "10111"=>
-- flag<='0';
--flash<=22;
if preset_enable='0'then
how_many_hourh<=cnt;
end if;
if buttonr='0'then
next_state<="11000";
end if;
if buttonl='0' then
next_state<="10110";
end if;
when "11000"=>
-- flag<='1';
--flash<=23;
if preset_enable='0'then
how_many_hourl<=cnt;
end if;
if buttonr='0'then
next_state<="11001";
end if;
if buttonl='0' then
next_state<="10111";
end if;
when "11001"=>
-- flag<='1';
--flash<=24;
if buttonr='0'then
next_state<="00001";
end if;
if buttonl='0' then
next_state<="11000";
end if;
when others=>
next_state<="00001";
end case;
flash1<=conv_std_logic_vector(flash,4);
end process;
process(clk,reset,buttonr)
begin
if reset='0'then
cnt<="0000";
elsif rising_edge(clk)then
if buttonr='0'or buttonl='0'then
cnt<="0000";
end if;
if preset_enable='0'then
cnt<=cnt+1;
end if;
if cnt="1001"then
cnt<="0000";
end if;
end if;
end process;
end Behavioral;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -