📄 date_counter.vhd
字号:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity date_counter is
--iday_cntl表示日期的各位,iday_cnth表示日期的十位,其他依此类推
Port ( reset: in std_logic;
--start: in std_logic;
clk_day : in std_logic;
iday_cntl : in std_logic_vector(3 downto 0);
iday_cnth : in std_logic_vector(3 downto 0);
imonth_cntl : in std_logic_vector(3 downto 0);
imonth_cnth : in std_logic_vector(3 downto 0);
iyear_cntl : in std_logic_vector(3 downto 0);
iyear_cntml : in std_logic_vector(3 downto 0);
iyear_cntmh : in std_logic_vector(3 downto 0);
iyear_cnth : in std_logic_vector(3 downto 0);
oday_cntl : out std_logic_vector(3 downto 0);
oday_cnth : out std_logic_vector(3 downto 0);
omonth_cntl : out std_logic_vector(3 downto 0);
omonth_cnth : out std_logic_vector(3 downto 0);
oyear_cntl : out std_logic_vector(3 downto 0);
oyear_cntml : out std_logic_vector(3 downto 0);
oyear_cntmh : out std_logic_vector(3 downto 0);
oyear_cnth : out std_logic_vector(3 downto 0));
-----------------------------------------------------------------------------
function leap_identify(year:integer)return boolean is
begin --判断是否是闰年的函数
if (year rem 4)=0 then --虽然,1900和2100年不是闰年(1900和2100可以被4整除),
--但是实际运用一般不大可能超出这个范围(1901-2099),
return true; --即使超出这两年,也可以通过再预置解决;
else
return false;
end if;
end leap_identify;
end date_counter;
-------------------------------------------------------------------------------
architecture Behavioral of date_counter is
begin
process(clk_day,reset,iday_cntl,iday_cnth,imonth_cntl,imonth_cnth,iyear_cntl,iyear_cntml,iyear_cntmh,iyear_cnth)
type mon is array(11 downto 0)of std_logic_vector(7 downto 0);
constant leapyear : mon := ("00110001","00110000","00110001","00110000","00110001","00110001","00110000","00110001","00110000","00110001","00101000","00110001");
constant n_leapyear : mon := ("00110001","00110000","00110001","00110000","00110001","00110001","00110000","00110001","00110000","00110001","00101001","00110001");
variable temp:mon;
variable day_cntl:std_logic_vector(3 downto 0);
variable day_cnth:std_logic_vector(3 downto 0);
variable month_cntl:std_logic_vector(3 downto 0);
variable month_cnth:std_logic_vector(3 downto 0);
variable year_cntl:std_logic_vector(3 downto 0);
variable year_cntml:std_logic_vector(3 downto 0);
variable year_cntmh:std_logic_vector(3 downto 0);
variable year_cnth:std_logic_vector(3 downto 0);
variable temp1:std_logic_vector(7 downto 0);
begin
if reset='0'then
--enable='0'表示将预置日期代入
day_cntl:=iday_cntl ;
day_cnth:=iday_cnth;
month_cnth:=imonth_cnth;
month_cntl:=imonth_cntl ;
year_cntl:=iyear_cntl ;
year_cntml:=iyear_cntml;
year_cntmh:=iyear_cntmh;
year_cnth:=iyear_cnth;
--初始化
oday_cntl<=day_cntl;oday_cnth<=day_cnth;
omonth_cntl<=month_cntl;omonth_cnth<=month_cnth;
oyear_cntl<=year_cntl;oyear_cntml<=year_cntml;oyear_cntmh<=year_cntmh;oyear_cnth<=year_cnth;
elsif rising_edge(clk_day)then
--if start='0'then
--------------------------------------------
--判断是否是闰年(只须判断年的十位个位即可)
if leap_identify(conv_integer(year_cntl)+conv_integer(year_cnth)*10) then
temp:=leapyear ;--是闰年
else
temp:=n_leapyear;--不是闰年
end if;
--------------------------------------------
temp1:=day_cnth&day_cntl;
if temp1=temp(conv_integer(month_cntl)+conv_integer(month_cnth)*10-1) then
--判断这一年的这个月是否结束
----------------------------------------------------------------
day_cntl:="0000";day_cnth:="0000";
--从1号开始计日
if month_cnth="0001"and month_cntl="0010" then
--到了12月;
month_cntl:="0000";month_cnth:="0000";
--从一月开始计
----------------------------------------------------------------
year_cntl:=year_cntl+1;
--年加一
if year_cntl="1010"then
year_cntl:="0000";
year_cntml:=year_cntml+1;
end if;
if year_cntml="1010"then
year_cntml:="0000";
year_cntmh:=year_cntmh+1;
end if;
if year_cntmh="1010"then
year_cntmh:="0000";
year_cnth:=year_cnth+1;
end if;
end if;
----------------------------------------------------------------
month_cntl:=month_cntl+1;
--月加一
if month_cntl="1010"then
month_cntl:="0000";
month_cnth:=month_cnth+1;
end if;
----------------------------------------------------------------
end if;
day_cntl:=day_cntl+1;
--日期加一
if day_cntl="1010"then
day_cntl:="0000";
day_cnth:=day_cnth+1;
end if;
--------------------------------------------
oday_cntl<=day_cntl;
oday_cnth<=day_cnth;
omonth_cnth<=month_cnth;
omonth_cntl<=month_cntl ;
oyear_cntl<=year_cntl ;
oyear_cntml<=year_cntml;
oyear_cntmh<=year_cntmh;
oyear_cnth<=year_cnth;
--end if;
end if;
end process;
end Behavioral;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -