输入1Hz的时钟作为秒信号,秒计数满60后向分计数进1,分计数满60后向时计数进1。当计数到24:60:60自动回到00:00:00;
library ieee;
use ieee.std_logic_1164.all;
entity clock is
port(clk:in std_logic;--输入1Hz的时钟作为秒信号
clr:in std_logic;--异步清零信号
s:out integer range 0 to 60;--秒
min:out integer range 0 to 60;--分
h:out integer range 0 to 24--时
);
end clock;
architecture clock of clock is
begin
process(clk,clr)
variable count1 :integer range 0 to 60;--秒计数
variable count2 :integer range 0 to 60;--分计数
variable count3 :integer range 0 to 24;--时计数
begin
s<=count1;
min<=count2;
h<=count3;
if(clr='1')then
count1:=0;
count2:=0;
count3:=0;
elsif(clk'event and clk='1')then
count1:=count1+1;
if (count1=60)then
count1:=0;
count2:=count2+1;
if(count2=60)then
count2:=0;
count3:=count3+1;
if(count3=24)then
count3:=0;
end if;
end if;
end if;
end if;
end process;
end clock;