library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity clock is
port(clk:in std_logic;-----时钟输入 20mhz
clr:in std_logic;-----清零信号
en:in std_logic;------暂停信号
mode:in std_logic;----控制信号,用于选择模式
inc:in std_logic;-----置数信号
seg7:out std_logic_vector(6 downto 0);-----数码管显示信号
scan:out std_logic_vector(5 downto 0));---数码管扫描信号
end;
architecture one of clock is
signal state:std_logic_vector(1 downto 0);------定义四种状态
signal qhh,qhl,qmh,qml,qsh,qsl:std_logic_vector(3 downto 0);---小时、分、秒的高位和低位
signal data:std_logic_vector(3 downto 0);
signal cnt:integer range 0 to 5;-------------扫描数码管的计数器
signal clk1khz,clk1hz,clk2hz:std_logic;------1khz、1hz、2hz的分频信号
signal blink:std_logic_vector(2 downto 0);---闪烁信号
signal inc_reg:std_logic;
signal sec,min:integer range 0 to 59;
signal hour:integer range 0 to 23;
begin
----------------------------------1khz分频,用于扫描数码管地址
process(clk)
variable count:integer range 0 to 0;
begin
if clk'event and clk='1' then
if count=0 then clk1khz else count:=count+1;
end if;
end if;
end process;
------------------------------------1Hz分频,用于计时
process(clk1khz)
variable count:integer range 0 to 1;
begin
if clk1khz'event and clk1khz='1' then
if count=1 then clk1hz else count:=count+1;
end if;
end if;
end process;
--------------------------------------2Hz分频,用于数码管闪烁
process(clk1khz)
variable count:integer range 0 to 0;
begin
if clk1khz'event and clk1khz='1' then
if count=0 then clk2hz else count:=count+1;
end if;
end if;
end process;
-----------------------------模式转换
process(mode,clr)
begin
if clr='1' then
state elsif mode'event and mode='1' then
state end if;
end process;
-------------------------状态控制
process(clk1hz,state,en,clr,hour,sec,min)
begin
if en='1' then
hour min sec elsif clr='1' then
hour min sec elsif clk1hz'event and clk1hz='1' then
case state is
when "00"=>if sec=59 then sec if min=59 then min if hour=23 then hour else hour else min else sec end if;
when "01"=> if inc='1' then ---------------------------模式1,设定小时时间
if inc_reg='0' then inc_reg if hour=23 then
hour else hour end if;
end if;
else inc_reg end if;
when "10"=>if inc='1' then ---------------------------模式2,设定分钟时间
if inc_reg='0' then inc_reg if min=59 then
min else min end if;
end if;
else inc_reg end if;
when "11"=> if inc='1' then ---------------------------模式3,设定秒钟时间
if inc_reg='0' then inc_reg if sec=59 then
sec else sec end if;
end if;
else inc_reg end if;
end case;
end if;
end process;
-----------------------------当进行时间设定时,令数码管闪烁---------
process(state,clk2hz)
begin
case state is
when"00"=>blink when"01"=>blinkclk2hz,others=>'0');
when"10"=>blinkclk2hz,others=>'0');
when"11"=>blinkclk2hz,others=>'0');
end case;
end process;
---------------------------秒计数的十进制转BCD码--------
process(sec)
begin
case sec is
when 0|10|20|30|40|50 =>qsl when 1|11|21|31|41|51 =>qsl when 2|12|22|32|42|52 =>qsl when 3|13|23|33|43|53 =>qsl when 4|14|24|34|44|54 =>qsl when 5|15|25|35|45|55 =>qsl when 6|16|26|36|46|56 =>qsl when 7|17|27|37|47|57 =>qsl when 8|18|28|38|48|58 =>qsl when 9|19|29|39|49|59 =>qsl when others=>null;
end case;
case sec is
when 0|1|2|3|4|5|6|7|8|9 =>qsh when 10|11|12|13|14|15|16|17|18|19 =>qsh when 20|21|22|23|24|25|26|27|28|29 =>qsh when 30|31|32|33|34|35|36|37|38|39 =>qsh when 40|41|42|43|44|45|46|47|48|49 =>qsh when 50|51|52|53|54|55|56|57|58|59 =>qsh when others=>null;
end case;
end process;
---------------------------分计数的十进制转BCD码---------
process(min)
begin
case min is
when 0|10|20|30|40|50 =>qml when 1|11|21|31|41|51 =>qml when 2|12|22|32|42|52 =>qml when 3|13|23|33|43|53 =>qml when 4|14|24|34|44|54 =>qml when 5|15|25|35|45|55 =>qml when 6|16|26|36|46|56 =>qml when 7|17|27|37|47|57 =>qml when 8|18|28|38|48|58 =>qml when 9|19|29|39|49|59 =>qml when others=>null;
end case;
case min is
when 0|1|2|3|4|5|6|7|8|9 =>qmh when 10|11|12|13|14|15|16|17|18|19 =>qmh when 20|21|22|23|24|25|26|27|28|29 =>qmh when 30|31|32|33|34|35|36|37|38|39 =>qmh when 40|41|42|43|44|45|46|47|48|49 =>qmh when 50|51|52|53|54|55|56|57|58|59 =>qmh when others=>null;
end case;
end process;
---------------------------小时计数的十进制转BCD码---------
process(hour)
begin
case hour is
when 0|10|20 =>qhl when 1|11|21 =>qhl when 2|12|22 =>qhl when 3|13|23 =>qhl when 4|14 =>qhl when 5|15 =>qhl when 6|16 =>qhl when 7|17 =>qhl when 8|18 =>qhl when 9|19 =>qhl when others=>null;
end case;
case hour is
when 0|1|2|3|4|5|6|7|8|9 =>qhh when 10|11|12|13|14|15|16|17|18|19 =>qhh when 20|21|22|23 =>qhh when others=>null;
end case;
end process;
----------------------------------------数码管动态扫描计数----------------------
process(clk1khz)
begin
if clk1khz'event and clk1khz='1' then
if cnt=5 then cnt else cnt end if;
end if;
end process;
-------------------------------------数码管动态扫描-----------
process(cnt,qhh,qhl,qmh,qml,qsh,qsl,blink)
begin
case cnt is
when 0=> data when 1=> data when 2=> data when 3=> data when 4=> data when 5=> data when others=>null;
end case;
end process;
-----------------------------------------七段译码--------------------
process(data)
begin
case data is
when"0000"=>seg7 when"0001"=>seg7 when"0010"=>seg7 when"0011"=>seg7 when"0100"=>seg7 when"0101"=>seg7 when"0110"=>seg7 when"0111"=>seg7 when"1000"=>seg7 when"1001"=>seg7 when others=>seg7 end case;
end process;
end;