Calendar
Calendar
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity Calendar is
Port (
clk : in STD_LOGIC; -- 1Hz clock
reset : in STD_LOGIC;
set_sec : in STD_LOGIC;
set_minute : in STD_LOGIC;
set_hour : in STD_LOGIC;
set_day : in STD_LOGIC;
set_month : in STD_LOGIC;
set_year : in STD_LOGIC;
seg : out STD_LOGIC_VECTOR(27 downto 0) -- 6 x 7-segment outputs
(HHMMSS)
);
end Calendar;
begin
process(clk, reset)
begin
if reset = '1' then
sec <= (others => '0');
min <= (others => '0');
hr <= (others => '0');
day <= (others => '1');
month <= (others => '1');
year <= (others => '1');
last_set_sec <= '0';
last_set_minute <= '0';
last_set_hour <= '0';
last_set_day <= '1';
last_set_month <= '1';
last_set_year <= '1';
-- Set second
if set_sec = '1' and last_set_sec = '0' then
if sec = "111011" then
sec <= (others => '0');
else
sec <= std_logic_vector(unsigned(sec) + 1);
end if;
-- Set minute
elsif set_minute = '1' and last_set_minute = '0' then
if min = "111011" then
min <= (others => '0');
else
min <= std_logic_vector(unsigned(min) + 1);
end if;
-- Set hour
elsif set_hour = '1' and last_set_hour = '0' then
if hr = "010111" then
hr <= (others => '0');
else
hr <= std_logic_vector(unsigned(hr) + 1);
end if;
-- BCD Conversion
sec_ones <= std_logic_vector(unsigned(sec) mod 10);
sec_tens <= std_logic_vector(unsigned(sec) /10 mod 10);
min_ones <= std_logic_vector(unsigned(min) mod 10);
min_tens <= std_logic_vector(unsigned(min) / 10);
hr_ones <= std_logic_vector(unsigned(hr) mod 10);
hr_tens <= std_logic_vector(unsigned(hr) / 10);
day_ones <= std_logic_vector(unsigned(day) mod 10);
day_tens <= std_logic_vector(unsigned(day) / 10);
month_ones <= std_logic_vector(unsigned(month) mod 10);
month_tens <= std_logic_vector(unsigned(month) / 10);
-- 7-segment encoding
seg <= bcd_to_7seg(day_ones);
seg2 <= bcd_to_7seg(day_tens);
seg1 <= bcd_to_7seg(month_ones);
seg0 <= bcd_to_7seg(month_tens);
end Behavioral;