Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library IEEE;
- use IEEE.STD_LOGIC_1164.ALL;
- use IEEE.STD_LOGIC_UNSIGNED.ALL;
- entity timer is
- Port(
- clk, mux, time : in std_logic;
- start_in, reset_in : in std_logic;
- time_prec : in std_logic;
- a, b, c, d, e, f, g, dp, dspA, dspB : out std_logic);
- end timer;
- architecture atimer of timer is
- signal en_p, en_t, en_p_plus, en_t_plus, start, reset, start_plus, reset_plus : std_logic := '0';
- signal adress, seconds_1, seconds_10, minutes_1, minutes_10 : std_logic_vector(3 downto 0) := "0000";
- signal mseconds_1, mseconds_10, mseconds_100 : std_logic_vector(3 downto 0) := "0000";
- signal t_prec : std_logic := '0';
- signal count_enable : std_logic := '0';
- signal display : std_logic_vector(1 downto 0) := "00";
- signal data : std_logic_vector(6 downto 0) := "0000000";
- begin
- -- Klocka insignaler
- process(clk) begin
- if rising_edge(clk) then
- en_p <= en_p_plus;
- en_t <= en_t_plus;
- en_p_plus <= mux;
- en_t_plus <= time;
- start <= start_plus;
- reset <= reset_plus;
- start_plus <= start_in;
- reset_plus <= reset_in;
- t_prec <= time_prec;
- end if;
- end process;
- -- Räkna upp
- process(clk) begin
- if rising_edge(clk) then
- if (start='0' and start_plus='1') then
- count_enable <= not count_enable;
- elsif (reset='0' and reset_plus='1') then
- seconds_1 <= "0000";
- seconds_10 <= "0000";
- minutes_1 <= "0000";
- minutes_10 <= "0000";
- count_enable <= '0';
- elsif (en_t='0' and en_t_plus='1' and count_enable='1') then
- if (t_prec = '0') then
- seconds_1 <= seconds_1 + 1;
- if (seconds_1 = 9) then
- seconds_10 <= seconds_10 + 1;
- seconds_1 <= "0000";
- end if;
- if (seconds_10 = 5 and seconds_1 = 9) then
- minutes_1 <= minutes_1 + 1;
- seconds_10 <= "0000";
- end if;
- if (minutes_1 = 9 and seconds_10 = 5 and seconds_1 = 9) then
- minutes_10 <= minutes_10 + 1;
- minutes_1 <= "0000";
- end if;
- if (minutes_10 = 5 and minutes_1 = 9 and seconds_10 = 5 and seconds_1 = 8) then
- count_enable <= '0';
- end if;
- end if;
- elsif (en_p = '0' and en_p_plus = '1' and count_enable = '1') then
- if (t_prec = '1') then
- mseconds_1 <= mseconds_1 + 1;
- if (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 8 and seconds_1 = 9 and seconds_10 = 5 and minutes_1 = 9 and minutes_10 = 5) then
- count_enable <= '0';
- elsif (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 9 and seconds_1 = 9 and seconds_10 = 5 and minutes_1 = 9) then
- minutes_10 <= minutes_10 + 1;
- minutes_1 <= "0000";
- seconds_10 <= "0000";
- seconds_1 <= "0000";
- mseconds_100 <= "0000";
- mseconds_10 <= "0000";
- mseconds_1 <= "0000";
- elsif (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 9 and seconds_1 = 9 and seconds_10 = 5) then
- minutes_1 <= minutes_1 + 1;
- seconds_10 <= "0000";
- seconds_1 <= "0000";
- mseconds_100 <= "0000";
- mseconds_10 <= "0000";
- mseconds_1 <= "0000";
- elsif (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 9 and seconds_1 = 9) then
- seconds_10 <= seconds_10 + 1;
- seconds_1 <= "0000";
- mseconds_100 <= "0000";
- mseconds_10 <= "0000";
- mseconds_1 <= "0000";
- elsif (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 9) then
- seconds_1 <= seconds_1 + 1;
- mseconds_100 <= "0000";
- mseconds_10 <= "0000";
- mseconds_1 <= "0000";
- elsif (mseconds_10 = 9 and mseconds_1 = 9) then
- mseconds_100 <= mseconds_100 + 1;
- mseconds_10 <= "0000";
- mseconds_1 <= "0000";
- elsif(mseconds_1 = 9) then
- mseconds_10 <= mseconds_10 + 1;
- mseconds_1 <= "0000";
- end if;
- end if;
- end if;
- end if;
- end process;
- -- Sätt adress för PROMen samt välja display-segment
- process(clk) begin
- if rising_edge(clk) then
- if (en_p='0' and en_p_plus='1') then
- display <= display + 1;
- dp <= '0';
- if (display = 0) then
- if (t_prec = '0') then
- if (minutes_10 > 0) then
- dspA <= '1';
- dspB <= '1';
- end if;
- adress <= seconds_1;
- elsif (t_prec = '1') then
- if (seconds_10 > 0) then
- dspA <= '1';
- dspB <= '1';
- end if;
- adress <= mseconds_10;
- end if;
- elsif (display = 1) then
- if (t_prec = '0') then
- dspA <= '0';
- dspB <= '0';
- adress <= seconds_10;
- elsif(t_prec = '1') then
- dspA <= '0';
- dspB <= '0';
- adress <= mseconds_100;
- end if;
- elsif (display = 2) then
- if (t_prec = '0') then
- if ((minutes_1 >= 0 and seconds_10 > 0) or (seconds_10 > 0)) then
- dspA <= '1';
- dspB <= '0';
- end if;
- adress <= minutes_1;
- elsif(t_prec = '1') then
- if ((seconds_1 >= 0 and mseconds_100 > 0) or (mseconds_100 > 0)) then
- dspA <= '1';
- dspB <= '0';
- end if;
- adress <= seconds_1;
- end if;
- elsif (display = 3) then
- dp <= '1';
- if (t_prec = '0') then
- if ((minutes_1 >= 0 and minutes_10 > 0) or (minutes_1 > 0)) then
- dspA <= '0';
- dspB <= '1';
- end if;
- adress <= minutes_10;
- elsif(t_prec = '1') then
- if ((seconds_1 >= 0 and seconds_10 > 0) or (seconds_1 > 0)) then
- dspA <= '0';
- dspB <= '1';
- end if;
- adress <= seconds_10;
- end if;
- display <= "00";
- end if;
- end if;
- end if;
- end process;
- -- Översätt tal till display
- with adress select
- data <= "1111110" when "0000",
- "0110000" when "0001",
- "1101101" when "0010",
- "1111001" when "0011",
- "0110011" when "0100",
- "1011011" when "0101",
- "1011111" when "0110",
- "1110000" when "0111",
- "1111111" when "1000",
- "1111011" when "1001",
- "-------" when others;
- -- Sätt outputs
- process(clk) begin
- if (en_p='0' and en_p_plus='1') then
- if rising_edge(clk) then
- a <= data(6);
- b <= data(5);
- c <= data(4);
- d <= data(3);
- e <= data(2);
- f <= data(1);
- g <= data(0);
- end if;
- end if;
- end process;
- -- Lagra föregående klockpuls
- end atimer;
- -- vsim timer
- -- add wave sim:/timer/*
- -- restart -force
- -- force -freeze sim:/timer/clk 0 0, 1 4 -r 8
- -- force -freeze sim:/timer/mux 0 0, 1 50 -r 100
- -- force -freeze sim:/timer/time 0 0, 1 500 -r 1000
- -- force -freeze sim:/timer/start_in 0 0, 1 100
- -- force -freeze sim:/timer/reset_in 0 0
- -- force -freeze sim:/timer/time_prec 0 0
- -- run 500000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement