--24 Hour Clock Code with seconds and Time setting --Project "DPF" --References the "The Smart Alarm Clock" --File name: Time_display.vhd library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity time_display is generic ( num_width : positive :=4; clk_per_sec : natural := 2 ); port ( slow_clock : in std_logic; clock : in std_logic; reset : in std_logic; am_pm_in : in std_logic; am_pm_out : out std_logic; set_clock : in std_logic; button1, button2: in std_logic; num1_bcd_out, num2_bcd_out, num3_bcd_out, num4_bcd_out : buffer std_logic_vector ( num_width - 1 downto 0); num1_secs, num2_secs : buffer std_logic_vector ( num_width - 1 downto 0) ); end time_display; architecture order of time_display is component button_input_synchronizer1 is port( button_in, -- negative logic input button syn_clock, -- system clock to synchronize reset : in std_logic; -- reset syn_button_out : buffer std_logic ); end component button_input_synchronizer1; --buttons to set the time signal synbutton1, synbutton2, slow_clock1: std_logic; begin --instantiates button synchronizer for time setting button1_synchronizer: button_input_synchronizer1 port map( button_in => button1, syn_clock => clock, reset => reset, syn_button_out => synbutton1 ); button2_synchronizer: button_input_synchronizer1 port map( button_in => button2, syn_clock => clock, reset => reset, syn_button_out => synbutton2 ); --this process is where the normal clock operation takes place --We chose to use compounded if statements so that I would not have a large number of --states, althouth this is not the method I would normally use I found it to be most appropriate --because otherwise if state maps were used there would be 1440 states to deal with and this would be to combersome show : process(slow_clock,reset) variable count : natural := 0; variable secs : natural := 0; begin if reset = '1' then --resets to a known state 12:00 num1_bcd_out <= "0000"; num2_bcd_out <= "0000"; num3_bcd_out <= "0010"; num4_bcd_out <= "0001"; num1_secs <= "0000"; num2_secs <= "0000"; am_pm_out <= '0'; count := 0; secs := 0; elsif rising_edge(slow_clock) then if set_clock = '1' then --if setclock is high if synbutton1 = '1' then --increment minutes num1_bcd_out <= num1_bcd_out + 1; if num1_bcd_out = 9 then num1_bcd_out <= "0000"; if num2_bcd_out = 5 then num2_bcd_out <= "0000"; else num2_bcd_out <= num2_bcd_out + 1; end if; end if; elsif synbutton2 = '1' then --increments hours num3_bcd_out <= num3_bcd_out + '1'; if num3_bcd_out = 9 then --sets time to 10:00 when its 9:59 num3_bcd_out <= "0000"; num4_bcd_out <= num4_bcd_out + '1'; elsif num3_bcd_out = 2 and num4_bcd_out =1 then --resets back to 1 o'clock when it's 12:59 num3_bcd_out <= "0001"; num4_bcd_out <= "0000"; else num3_bcd_out <= num3_bcd_out + '1'; end if; if num4_bcd_out = 1 and num3_bcd_out =2 and num2_bcd_out = 0 and num1_bcd_out = 0 then if am_pm_in = '0' then am_pm_out <= '1'; elsif am_pm_in = '1' then am_pm_out <= '0'; end if; end if; end if; else secs := secs + 1; if secs = clk_per_sec then num1_secs <= num1_secs + 1; if num1_secs = 9 then num1_secs <= "0000"; if num2_secs = 5 then num2_secs <= "0000"; else num2_secs <= num2_secs + 1; end if; end if; secs := 0; count:= count + 1; end if; if count = 6 then if num1_bcd_out = 9 then num1_bcd_out <= "0000"; --resets minutes back to 0 when minutes are 9 if num2_bcd_out = 5 then num2_bcd_out <= "0000"; --resets minutes back to 0 when minutes are 59 if num3_bcd_out = 9 then --sets time to 10:00 when its 9:59 num3_bcd_out <= "0000"; num4_bcd_out <= num4_bcd_out + '1'; elsif num3_bcd_out = 2 and num4_bcd_out =1 then --resets back to 1 o'clock when it's 12:59 num3_bcd_out <= "0001"; num4_bcd_out <= "0000"; else num3_bcd_out <= num3_bcd_out + '1'; end if; else num2_bcd_out <= num2_bcd_out + '1'; end if; else num1_bcd_out <= num1_bcd_out + '1'; end if; if num4_bcd_out = 1 and num3_bcd_out =2 and num2_bcd_out = 0 and num1_bcd_out = 0 then if am_pm_in = '0' then am_pm_out <= '1'; elsif am_pm_in = '1' then am_pm_out <= '0'; end if; end if; count := 0; end if; end if; end if; end process show; end order;