--a**都为缓冲器;
--rih,ril日历天的高低位;
--yueh,yuel日历中月的高低位;
--nianh,nianl日历中年的高低位;
--前面加a为相应缓冲器;
library IEEE;
use IEEE.std_logic_1164.all;
entity timer is
port( clk : in std_logic; -- System clock input port(250KHz).
naozhong: in std_logic;--闹钟停止键
L : out std_logic_vector(2 downto 0); -- 8 seven-segment displays selection ports.
dled: out std_logic_vector(7 downto 0); -- Digits display ports.
day,time: in std_logic;--time控制闹钟,day控制日历
sound: out std_logic;
clk_1: in std_logic;--设置键
clk_2: in std_logic;--加法键?
clk_3: in std_logic);--减法键
end timer;
architecture display of timer is
signal rih,arih:integer range 0 to 3;
signal ayueh,yueh: integer range 0 to 1;
signal cnt: integer range 0 to 7; -- 8 seven-segment displays seletion counter.
signal ct:integer range 0 to 250000 ; -- 1 second generator counter.
signal hourH,ahourH,alarm_hourH: integer range 0 to 2;
signal minuteH,secondH,aminuteH,asecondH,alarm_minuteH,alarm_secondH: integer range 0 to 7;
signal anianl,anianh,ril,aril,ayuel,yueh,yuel,nianl,nianh, hourL,minuteL,secondL,ahourL,aminuteL,asecondL,alarm_hourL,alarm_minuteL,alarm_secondL: integer range 0 to 10;
signal set : integer range 0 to 7;
signal d,k,p: std_logic;
signal aweek,week: integer range 0 to 6;
begin
process (clk)
begin
if clk'event and clk = '1' then
sound<='1';
cnt<=cnt+1;
ct<=ct+1;
if ct=250000 then -- 1 second
ct<=0;
secondL<=secondL+1;
end if;
if secondL=10 then -- 10 seconds
secondL<=0;
secondH<=secondH+1;
end if;
if secondH=6 then -- 60 seconds(1 minute)
secondH<=0;
minuteL<=minuteL+1;
end if;
if minuteL=10 then -- 10 minutes
minuteL<=0;
minuteH<=minuteH+1;
end if;
if minuteH=6 then -- 60 minutes(1 hour)
minuteH<=0;
hourL<=hourL+1;
end if;
if hourL=10 then -- 10 hours
hourL<=0;
hourH<=hourH+1;
end if;
if hourH=2 and hourL =4 then -- 24 hours
hourH<=0;
hourL<=0;
end if;
if hourh=0 and hourl=0 then
week<=week +1;
ril<=ril+1;
end if;
if ril=10 then
ril<=0;
rilh<=rih+1;
end if;
if rih=3 then
rih<=0;
yue<=yuel+1;
end if;
if yuel=10 then
yuel<=0;
yueh<=yueh+1;
end if ;
if yueh=1 and yuel=2 then
yueh<=0;
yuel<=0;
nianl<=nianl+1;
end if;
if nianl=10 then
nianl<=0;
nianh<=nianh+1;
end if;
if nianh=10 then
nianh<=0;
nianl<=0;
end if;
if (time='0' or day='0') then
ahourH<=hourH;
ahourL<=hourL;
aminuteH<=minuteH;
aminuteL<=minuteL;
asecondH<=secondH;
asecondL<=secondL;
aweek<=week;
elsif(time='1')then
ahourH<=alarm_hourH;
ahourL<= alarm_hourL;
aminuteH<= alarm_minuteH;
aminuteL<= alarm_minuteL;
asecondH<= alarm_secondH;
asecondL<= alarm_secondL;
aweek<=week;
elsif (day='1') then
anianh<=nianh;
anianl<=nianl;
ayueh<=yueh;
ayuel<=yuel;
arih<=rih;
aril<=ril;
end if;
case cnt is -- To display the time.
when 0 => L<="000"; -- Enable the leftmost display.
if day='1' then
dled<=x"DA";
else
case ahourH is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when others => null;
end case;
end if;
when 1 => L<="001"; -- Enable the second display.
if day='1' then
dled<=X"FC";
else
case ahourL is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when 6 => dled<=X"BE";--6
when 7 => dled<=X"E0";--7
when 8 => dled<=X"FE";--8
when 9 => dled<=X"F6";--9
when others => null;
end case;
end if;
when 2 => L<="010"; -- Enable the third display.
if day='1' then
case anianh is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when 6 => dled<=X"BE";--6
when 7 => dled<=X"E0";--7
when 8 => dled<=X"FE";--8
when 9 => dled<=X"F6";--9
when others => null;
end case;
else
case aminuteH is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when others => null;
end case;
end if;
when 3 => L<="011"; -- Enable the fifth display.
if day='1' then
case anianl is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when 6 => dled<=X"BE";--6
when 7 => dled<=X"E0";--7
when 8 => dled<=X"FE";--8
when 9 => dled<=X"F6";--9
when others => null;
end case;
else
case aminuteL is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when 6 => dled<=X"BE";--6
when 7 => dled<=X"E0";--7
when 8 => dled<=X"FE";--8
when 9 => dled<=X"F6";--9
when others => null;
end case;
when 4 => L<="100"; -- Enable the seventh display.
if day<='1'then
case ayueh is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when others => null;
end case;
else
case asecondH is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when others => null;
end case;
end if;
when 5 => L<="101"; -- Enable the rightmost display
if day<='1' then
case ayuel is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when 6 => dled<=X"BE";--6
when 7 => dled<=X"E0";--7
when 8 => dled<=X"FE";--8
when 9 => dled<=X"F6";--9
when others => null;
end case;
else
case asecondL is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when 6 => dled<=X"BE";--6
when 7 => dled<=X"E0";--7
when 8 => dled<=X"FE";--8
when 9 => dled<=X"F6";--9
when others => null;
end case;
end if;
when 6 => L <="110"
if day='1' then
case arih is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when others => null;
end case;
else
dled<=X"01";
end if;
when 7 => L <="111";
if day='1' then
case aril is
when 0 => dled<=X"FC";--0
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when 6 => dled<=X"BE";--6
when 7 => dled<=X"E0";--7
when 8 => dled<=X"FE";--8
when 9 => dled<=X"F6";--9
when others => null;
end case;
else
case aweek is
when 0 => dled<=X"E0"-7
when 1 => dled<=X"60";--1
when 2 => dled<=X"DA";--2
when 3 => dled<=X"F2";--3
when 4 => dled<=X"66";--4
when 5 => dled<=X"B6";--5
when 6 => dled<=X"BE";--6
when others => null;
end case;
end if;
end case ;
d<=clk_1;
k<=clk_2;
p<=clk_3;
if time='1' then
if ( d='0' and clk_1='1')then
set<=set+1;
end if;
case set is
when 1 =>
if(k='0' and clk_2='1')then
alarm_hourH<= alarm_hourH+1;
elsif( p='0' and clk_3='1')then
alarm_hourH<= alarm_hourH-1;
end if;
when 2 => if( k='0' and clk_2='1')then
alarm_hourL<= alarm_hourL+1;
elsif( p='0' and clk_3='1')then
alarm_hourL<= alarm_hourL-1;
end if;
when 3 => if(k='0' and clk_2='1')then
alarm_minuteH<= alarm_minuteH +1;
elsif( p='0' and clk_3='1')then
alarm_minuteH<= alarm_minuteH -1;
end if;
when 4 => if( k='0' and clk_2='1')then
alarm_minuteL<= alarm_minuteL+1;
elsif( p='0' and clk_3='1')then
alarm_minuteL<= alarm_minuteL-1;
end if;
when 5=> if(k='0' and clk_2='1')then
alarm_secondH<= alarm_secondH +1;
elsif(p='0' and clk_3='1')then
alarm_secondH<=alarm_secondH-1;
end if;
when 6 => if( k='0' and clk_2='1')then
alarm_secondL<= alarm_secondL +1;
elsif( p='0' and clk_3='1')then
alarm_secondL<= alarm_secondL-1;
end if;
when 7=> if( k='0' and clk_2='1')then
aweek<=aweek+1;
elsif( p='0' and clk_3='1')then
aweek<=aweek-1;
end if;
when others=>null;
end case;
end if;
if time='0' or day='0' then
if ( d='0' and clk_1='1')then
set<=set+1;
end if;
case set is
when 1 => if(k='0' and clk_2='1')then
hourH<=hourH+1;
elsif( p='0' and clk_3='1')then
hourH<= hourH-1;
end if;
when 2 => if( k='0' and clk_2='1')then
hourL<= hourL+1;
elsif( p='0' and clk_3='1')then
hourL<= hourL-1;
end if;
when 3 => if(k='0' and clk_2='1')then
minuteH<= minuteH +1;
elsif( p='0' and clk_3='1')then
minuteH<= minuteH -1;
end if;
when 4 => if( k='0' and clk_2='1')then
minuteL<= minuteL+1;
elsif( p='0' and clk_3='1')then
minuteL<= minuteL-1;
end if;
when 5=>
if(k='0' and clk_2='1')then
secondH<= secondH +1;
elsif(p='0' and clk_3='1')then
secondH<= secondH-1;
end if;
when 6 => if( k='0' and clk_2='1')then
secondL<= secondL +1;
elsif( p='0' and clk_3='1')then
secondL<= secondL-1;
end if;
when 7=>if( k='0' and clk_2='1')then
week <= week +1;
elsif( p='0' and clk_3='1')then
week <= week -1;
end if;
when others=>null;
end case;
end if;
if day ='1' then
if ( d='0' and clk_1='1')then
set<=set+1;
end if;
case set is
when 1=> if(k='0' and clk_2='1')then
nianh<=nianH+1;
elsif( p='0' and clk_3='1')then
nianH<= nianH-1;
end if;
when 2 => if( k='0' and clk_2='1')then
nianL<= nianL+1;
elsif( p='0' and clk_3='1')then
nianL<= nianL-1;
end if;
when 3 => if(k='0' and clk_2='1')then
yueH<= yueH +1;
elsif( p='0' and clk_3='1')then
yueH<= yueH -1;
end if;
when 4 => if( k='0' and clk_2='1')then
yueL<= yueL+1;
elsif( p='0' and clk_3='1')then
yueL<=yueL-1;
end if;
when 5=>
if(k='0' and clk_2='1')then
riH<= riH +1;
elsif(p='0' and clk_3='1')then
riH<= riH-1;
end if;
when 6 => if( k='0' and clk_2='1')then
riL<= riL +1;
elsif( p='0' and clk_3='1')then
riL<=riL-1;
end if;
when others=>null;
end case;
end if;
if ((hourH = alarm_hourH) and (hourL= alarm_hourL) and (minuteH = alarm_minuteH)and(minuteL=alarm_minuteL))or((minuteH=0)and(minuteL=0) )then
sound<='1';
end if;
if (secondH=1)or(naozh)then
sound<='0';
end if ;
end if;
end process;
end architecture display;
1