library IEEE;
use IEEE.std_logic_1164.ALL;
use IEEE.std_logic_unsigned.ALL;
use IEEE.std_logic_arith.ALL;
----EN是各个功能块的使能信号,EN(0)=‘1’时是显示时间,EN(1)=‘1’时是设置时间,EN(2)='1'时是闹钟1,EN(3)='1'时是闹钟2,EN(4)='1'时是闹钟3
---key0是用来调整用的
---key_1是设置时间后按下确认键时的电平
---key1是用来调整时间或闹钟的,和key0配合使用。
---key2是用来切换调整的是时还是分,在显示时间的时候,还可以作为切换显示时,分或者秒
---choise输出信号值,可以为秒,分,时,
---output输出数码管的位选信号,和choise配合使用来实现动态显示
---bell输到蜂鸣器中,当bell=‘0’时蜂鸣器响,这里是用作闹钟响或者整点报时的
---dot是数码管上的e口,即显示的是那一点,主要是用来显示时,分时显示如下12.01
---setm0---seth1这四个是用作输入设置的时间
---set_en设置时间block(块)的使能信号
---e懒人模式块的使能信号
entity clock is
port(clk_1s,clk_5us,clk_100ms:in std_logic;
EN:in std_logic_vector(4 downto 0);
key0:in std_logic;
key_1:in std_logic;
key1:in std_logic;
key2:in std_logic;
choise:out std_logic_vector(3 downto 0);
output:out std_logic_vector(3 downto 0);
bell:out std_logic;
dot:out std_logic;
setm0:in std_logic_vector(3 downto 0);
setm1:in std_logic_vector(3 downto 0);
seth0:in std_logic_vector(3 downto 0);
seth1:in std_logic_vector(3 downto 0);
set_en:out std_logic;
e:out std_logic);
end clock;
architecture behav of clock is
---s0秒值的个位,s1秒值的十位,m0分值的个位,m1分值的十位,h0时的个位,h1时的十位,其他的类推
signal s0:std_logic_vector(3 downto 0):="0000";
signal s1:std_logic_vector(3 downto 0):="0000";
signal m0:std_logic_vector(3 downto 0):="0000";
signal m1:std_logic_vector(3 downto 0):="0000";
signal h0:std_logic_vector(3 downto 0):="0000";
signal h1:std_logic_vector(3 downto 0):="0000";
signal setm00:std_logic_vector(3 downto 0):="0011";
signal setm01:std_logic_vector(3 downto 0):="0000";
signal seth00:std_logic_vector(3 downto 0):="0000";
signal seth01:std_logic_vector(3 downto 0):="0000";
signal setm10:std_logic_vector(3 downto 0):="0011";
signal setm11:std_logic_vector(3 downto 0):="0000";
signal seth10:std_logic_vector(3 downto 0):="0000";
signal seth11:std_logic_vector(3 downto 0):="0000";
signal setm20:std_logic_vector(3 downto 0):="0011";
signal setm21:std_logic_vector(3 downto 0):="0000";
signal seth20:std_logic_vector(3 downto 0):="0000";
signal seth21:std_logic_vector(3 downto 0):="0000";
signal put:integer:=1;
signal if_alarm:std_logic:='1';
begin
JISHI:process(key1,clk_1s) -----------set_clock and set_alarm
begin
if (EN(1)='1' AND key_1='0') then ----按下确认键(key_1=0)时把设置好的时间输入,
m0<=setm0;
m1<=setm1;
h0<=seth0;
h1<=seth1;
elsif rising_edge(clk_1s) then ----1s钟计时
if s0="1001" then
s0<="0000";
if s1="0101" then
s1<="0000";
if m0="1001" then
m0<="0000";
if m1="0101" then
m1<="0000";
if h1="0010" then
if h0="0011" then
h0<="0000";
h1<="0000";
else h0<=h0+1;
end if;
elsif h0="1001" then
h0<="0000";
h1<=h1+1;
else h0<=h0+1;
end if;
else m1<=m1+1;
end if;
else m0<=m0+1;
end if;
else s1<=s1+1;
end if;
else s0<=s0+1;
end if;
END IF;
END PROCESS JISHI;
setalarm:PROCESS(key1,key0) ---设置闹钟
begin
if rising_edge(key0) then
if EN(2)='1' then ---set_bell_1
if key1='0' then
if key2='0' then ---min-1
IF setm00="0000" THEN
setm00<="1001";
IF setm01="0000" THEN
setm01<="0101";
ELSE setm01<=setm01-1;
END IF;
ELSE setm00<=setm00-1;
END IF;
elsif key2='1' then ---hour-1
if seth01="0000" then
IF seth00="0000" THEN
seth00<="0011";
seth01<="0010";
ELSE seth00<=seth00-1;
END IF;
elsif seth00="0000" then
seth00<="1001";
seth01<=seth01-1;
else seth00<=seth00-1;
end if;
end if;
elsif key1='1' then
if key2='0' then ---min+1
IF setm00="1001" THEN
setm00<="0000";
IF setm01="0101" THEN
setm01<="0000";
ELSE setm01<=setm01+1;
END IF;
ELSE setm00<=setm00+1;
END IF;
elsif key2='1' then ---hour+1
if seth01="0010" then
if seth00="0011" then
seth00<="0000";
seth01<="0000";
else seth00<=seth00+1;
end if;
elsif seth00="1001" then
seth00<="0000";
seth01<=seth01+1;
else seth00<=seth00+1;
end if;
end if;
end if;
elsif EN(3)='1' then ---set_bell_2
if key1='0' then
if key2='0' then ---min-1
IF setm10="0000" THEN
setm10<="1001";
IF setm11="0000" THEN
setm11<="0101";
ELSE setm11<=setm11-1;
END IF;
ELSE setm10<=setm10-1;
END IF;
elsif key2='1' then ---hour-1
if seth11="0000" then
IF seth10="0000" THEN
seth10<="0011";
seth11<="0010";
ELSE seth10<=seth10-1;
END IF;
elsif seth10="0000" then
seth10<="1001";
seth11<=seth11-1;
else seth10<=seth10-1;
end if;
end if;
elsif key1='1' then
if key2='0' then ---min+1
IF setm10="1001" THEN
setm10<="0000";
IF setm11="0101" THEN
setm11<="0000";
ELSE setm11<=setm11+1;
END IF;
ELSE setm10<=setm10+1;
END IF;
elsif key2='1' then ---hour+1
if seth11="0010" then
if seth10="0011" then
seth10<="0000";
seth11<="0000";
else seth10<=seth10+1;
end if;
elsif seth10="1001" then
seth10<="0000";
seth11<=seth11+1;
else seth10<=seth10+1;
end if;
end if;
end if;
elsif EN(4)='1' then ---set_bell_3
if key1='0' then
if key2='0' then ---min-1
IF setm20="0000" THEN
setm20<="1001";
IF setm21="0000" THEN
setm21<="0101";
ELSE setm21<=setm21-1;
END IF;
ELSE setm20<=setm20-1;
END IF;
elsif key2='1' then ---hour-1
if seth21="0000" then
IF seth20="0000" THEN
seth20<="0011";
seth21<="0010";
ELSE seth20<=seth20-1;
END IF;
elsif seth20="0000" then
seth20<="1001";
seth21<=seth21-1;
else seth20<=seth20-1;
end if;
end if;
elsif key1='1' then
if key2='0' then ---min+1
IF setm20="1001" THEN
setm20<="0000";
IF setm21="0101" THEN
setm21<="0000";
ELSE setm21<=setm21+1;
END IF;
ELSE setm20<=setm20+1;
END IF;
elsif key2='1' then ---hour+1
if seth21="0010" then
if seth20="0011" then
seth20<="0000";
seth21<="0000";
else seth20<=seth20+1;
end if;
elsif seth20="1001" then
seth20<="0000";
seth21<=seth21+1;
else seth20<=seth20+1;
end if;
end if;
end if;
end if;
end if;
end process setalarm;
out_put:process(clk_5us) ---动态显示,利用5us的时钟
begin
if rising_edge(clk_5us) then
if EN(2)='1' THEN ---显示闹钟1
CASE put is
when 1 =>
output<="1110";
choise<=setm00;
dot<='1';
put<=2;
when 2 =>
output<="1101";
choise<=setm01;
dot<='1';
put<=3;
when 3 =>
output<="1011";
choise<=seth00;
dot<='0';
put<=4;
when 4 =>
output<="0111";
choise<=seth01;
dot<='1';
put<=1;
when others => put<=1;
end case;
elsif EN(3)='1' THEN ---显示闹钟2
CASE put is
when 1 =>
output<="1110";
choise<=setm10;
dot<='1';
put<=2;
when 2 =>
output<="1101";
choise<=setm11;
dot<='1';
put<=3;
when 3 =>
output<="1011";
choise<=seth10;
dot<='0';
put<=4;
when 4 =>
output<="0111";
choise<=seth11;
dot<='1';
put<=1;
when others => null;
end case;
elsif EN(4)='1' THEN ---显示闹钟3
CASE put is
when 1 =>
output<="1110";
choise<=setm20;
dot<='1';
put<=2;
when 2 =>
output<="1101";
choise<=setm21;
dot<='1';
put<=3;
when 3 =>
output<="1011";
choise<=seth20;
dot<='0';
put<=4;
when 4 =>
output<="0111";
choise<=seth21;
dot<='1';
put<=1;
when others => null;
end case;
elsif (EN(0)='1' AND key2='0') then ---显示时和分
CASE put is
when 1 =>
output<="1110";
choise<=m0;
dot<='1';
put<=2;
when 2 =>
output<="1101";
choise<=m1;
dot<='1';
put<=3;
when 3 =>
output<="1011";
choise<=h0;
dot<='0';
put<=4;
when 4 =>
output<="0111";
choise<=h1;
dot<='1';
put<=1;
when others => null;
end case;
elsif (key2='1' and EN(0)='1') then ---显示秒
CASE put is
when 1 =>
output<="1110";
choise<=s0;
dot<='1';
put<=2;
when 2 =>
output<="1101";
choise<=s1;
dot<='1';
put<=3;
when 3 =>
output<="1110";
choise<=s0;
dot<='1';
put<=4;
when 4 =>
output<="1101";
choise<=s1;
dot<='1';
put<=1;
when others => put<=1;
end case;
elsif EN(1)='1' then ---显示设置的时间
case put is
when 1 =>
output<="1110";
choise<=setm0;
dot<='1';
put<=2;
when 2 =>
output<="1101";
choise<=setm1;
dot<='1';
put<=3;
when 3 =>
output<="1011";
choise<=seth0;
dot<='0';
put<=4;
when 4 =>
output<="0111";
choise<=seth1;
dot<='1';
put<=1;
when others => put<=1;
end case;
end if;
end if;
end process out_put;
alarm:process(clk_100ms) ---闹铃响的判断进程
begin
if rising_edge(clk_100ms) then ---整点报时
if (s1="0000" and m1="0000" and m0="0000" and h0="0000" and h1="0000") then
if(s0="0000") then if_alarm<='0';
elsif(s0="0011") then if_alarm<='1';
end if;
elsif EN(0)='1' THEN ---闹钟响
if ((setm00=m0 AND setm01=m1 and seth00=h0 and seth01=h1) or (setm10=m0 AND setm11=m1 and seth10=h0 and seth11=h1) or (setm20=m0 AND setm21=m1 and seth20=h0 and seth21=h1)) THEN
if (s0="0000" and s1="0000") then if_alarm<='0';e<='1';
elsif (s1="0001") then if_alarm<='1';
end if;
end if;
elsif(EN(0)='0') then if_alarm<='1';e<='0'; ---其他情况下闹铃不响
end if;
end if;
end process alarm;
bell<=if_alarm;
set_en<=EN(1);
end behav;
这是一个基于VHDL的数字时钟系统设计,包括时间显示、设置时间及多个闹钟功能。通过EN信号控制不同的操作模式,如显示时间、设置时间和闹钟。key输入信号用于调整和确认时间,输出包括数码管位选信号、蜂鸣器控制和状态指示。

571

被折叠的 条评论
为什么被折叠?



