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_misc.all;
- use IEEE.NUMERIC_STD.ALL;
- entity LedCounter is
- Port ( clk : in STD_LOGIC;
- rst : in STD_LOGIC;
- button : in STD_LOGIC;
- leds : out STD_LOGIC_VECTOR (7 downto 0));
- end LedCounter;
- architecture Behavioral of LedCounter is
- -- Clocks since the button was last pressed
- signal csp : unsigned(31 downto 0) := (others => '0');
- -- Holds the value that will be assigned to the LEDs
- signal leds_counter : unsigned(6 downto 0) := (others => '0');
- -- Last sampled value of the button
- signal last_button_value : std_logic := '0';
- signal countdown : unsigned(31 downto 0) := (others => '0');
- signal ffs : std_logic_vector(1 downto 0);
- signal result : std_logic;
- begin
- process(result)
- begin
- if(rising_edge(result)) then
- leds_counter <= leds_counter + 1;
- end if;
- end process;
- -- Always assign LEDs the value of leds_counter
- leds(6 downto 0) <= std_logic_vector(leds_counter);
- leds(7) <= '0';
- -- process(clk, rst)
- -- variable sr : std_logic_vector(3 downto 0) := (others => '0');
- -- constant sr_chk : std_logic_vector(sr'high-1 downto 0) := (others => '1');
- -- begin
- -- if(rst = '1') then
- -- leds_counter <= (others => '0');
- -- sr := (others => '0');
- -- elsif(rising_edge(clk)) then
- -- sr(sr'high downto 1) := sr (sr'high-1 downto 0);
- -- sr(0) := button;
- -- --if(sr(sr'high downto 1) = sr_chk and sr(0) = '0') then
- -- --if(sr(19 downto 0) = sr_chk and sr(20) = '0') then
- -- if(sr(sr'high-1 downto 0) = sr_chk and sr(sr'high) = '0') then
- -- leds_counter <= leds_counter + 1;
- -- end if;
- -- end if;
- -- end process;
- -- process(clk)
- -- variable ffs : std_logic_vector(9 downto 0) := (others => '0');
- -- variable co : unsigned(10 downto 0) := (others => '0');
- -- variable a : std_logic;
- -- begin
- -- if(rising_edge(clk)) then
- -- ffs(ffs'high downto 1) := ffs(ffs'high - 1 downto 0);
- -- ffs(0) := button;
- -- a := xor_reduce(ffs);
- -- if(a = '1') then
- -- co := (others => '0');
- -- elsif(co(co'high) = '0') then
- -- co := co + 1;
- -- else
- -- if(ffs(1) = '1') then
- -- leds_counter <= leds_counter + 1;
- -- end if;
- -- --result <= ffs(1);
- -- end if;
- -- end if;
- -- end process;
- -- process(clk, rst)
- -- variable counter : unsigned(31 downto 0) := (others => '0');
- -- variable flipflops : std_logic_vector(3 downto 0) := (others => '0');
- -- constant max : unsigned(counter'range) := to_unsigned(100_000_000 / 100, counter'length);
- -- begin
- -- if(rst = '1') then
- -- counter := (others => '0');
- -- flipflops := (others => '0');
- -- elsif(rising_edge(clk)) then
- -- flipflops(flipflops'high downto 1) := flipflops(flipflops'high-1 downto 0);
- -- flipflops(0) := button;
- --
- -- if(and_reduce(flipflops) = '1') then
- -- counter := to_unsigned(100_000_000 / 100, counter'length);
- -- elsif(counter > 0) then
- -- counter := counter - 1;
- -- else
- -- if(button = '1') then
- -- leds_counter <= leds_counter + 1;
- -- end if;
- -- end if;
- -- end if;
- -- end process;
- -- process(clk, rst)
- -- constant max : unsigned(31 downto 0) := to_unsigned(100_000_000/10, 32);
- -- variable counter : unsigned(31 downto 0) := max;
- -- variable last_button_state : std_logic := '0';
- -- begin
- -- if(rst = '1') then
- -- counter := max;
- -- last_button_state := '0';
- -- elsif(rising_edge(clk)) then
- -- if(counter > 0) then
- -- counter := counter - 1;
- -- elsif(button = '1' and last_button_state = '0') then
- -- counter := max;
- -- leds_counter <= leds_counter + 1;
- -- end if;
- --
- -- last_button_state := button;
- -- end if;
- -- end process;
- process(clk, rst)
- constant start : unsigned(31 downto 0) := to_unsigned(100_000_000 / 1000, 32);
- variable counter : unsigned(start'range) := (others => '0');
- begin
- if(rst = '1') then
- counter := (others => '0');
- ffs <= (others => '0');
- elsif(rising_edge(clk)) then
- ffs(1) <= ffs(0);
- ffs(0) <= button;
- if(ffs = "10" or ffs = "10") then
- counter := start;
- elsif(counter > 0) then
- counter := counter - 1;
- else
- result <= button;
- end if;
- end if;
- end process;
- -- PROCESS(clk)
- -- variable flipflops : std_logic_vector(1 downto 0) := (others => '0');
- -- variable counter_out : unsigned(27 downto 0) := (others => '0');
- -- --variable result : std_logic;
- -- variable counter_set : std_logic;
- -- BEGIN
- --
- -- IF(clk'EVENT and clk = '1') THEN
- -- flipflops(0) := button;
- -- flipflops(1) := flipflops(0);
- -- counter_set := flipflops(0) xor flipflops(1);
- -- If(counter_set = '1') THEN --reset counter because input is changing
- -- counter_out := (OTHERS => '0');
- -- ELSIF(counter_out(counter_out'high) = '0') THEN --stable input time is not yet met
- -- counter_out := counter_out + 1;
- -- ELSE --stable input time is met
- -- result <= flipflops(1);
- -- END IF;
- -- END IF;
- -- END PROCESS;
- -- process(clk, rst)
- -- variable ffs : std_logic_vector(1 downto 0) := (others => '0');
- -- variable counter_out : unsigned(19 downto 0) := (others => '0');
- -- begin
- -- if(rst = '1') then
- -- counter_out := (others => '0');
- -- ffs := (others => '0');
- -- elsif(rising_edge(clk)) then
- -- ffs(1) := ffs(0);
- -- ffs(0) := button;
- -- if(ffs(0) xor ffs(1) = '1') then
- -- counter_out := (others => '0');
- -- elsif(counter_out(counter_out'high) = '0') then
- -- counter_out := counter_out + 1;
- -- else
- -- leds_counter <= leds_counter + 1;
- -- end if;
- -- end if;
- -- end process;
- -- process(clk, rst)
- -- --variable countdown : unsigned(31 downto 0) := (others => '0');
- -- begin
- -- if(rst = '1') then
- -- countdown <= (others => '0');
- -- leds_counter <= (others => '0');
- -- elsif(rising_edge(clk)) then
- -- if(countdown /= 0) then
- -- countdown <= countdown - 1;
- -- leds(7) <= '0';
- -- else
- -- leds(7) <= '1';
- -- if(button = '1' and last_button_value = '0') then
- -- countdown <= to_unsigned(100_000_000 / 100, countdown'length);
- -- leds_counter <= leds_counter + 1;
- -- elsif(button = '0' and last_button_value = '1') then
- -- countdown <= to_unsigned(100_000_000 / 100, countdown'length);
- -- end if;
- --
- --
- -- end if;
- --
- --
- -- end if;
- -- end process;
- -- process(clk, rst)
- -- begin
- -- -- clear things
- -- if(rst = '1') then
- -- leds_counter <= (others => '0');
- -- csp <= (others => '0');
- -- -- Assume the button has not been pressed
- -- last_button_value <= '0';
- -- elsif(rising_edge(clk)) then
- -- -- Check to make sure some time has passed since the button was pressed
- -- if(csp > 100_000_000 / 5) then
- -- -- Check to see if the button is on the rising edge
- -- if(button = '1' and last_button_value = '0') then
- -- -- Increment the counter and reset the counts since last pressed
- -- leds_counter <= leds_counter + 1;
- -- csp <= (others => '0');
- -- end if;
- -- else
- -- -- The max count has not been reached. Increment.
- -- csp <= csp + 1;
- -- end if;
- --
- -- -- Always update the last value signal with the current value
- -- last_button_value <= button;
- -- end if;
- -- end process;
- -- process(clk, rst)
- -- variable c : unsigned(31 downto 0) := (others => '0');
- -- begin
- -- if(rst = '1') then
- -- c := (others => '0');
- -- elsif(rising_edge(clk)) then
- -- if(c > 100_000_000 / 10 and button = '1' and last_button_value = '0') then
- -- leds_counter <= leds_counter + 1;
- -- c := (others => '0');
- -- else
- -- if(c <= 100_000_000 / 4) then
- -- c := c + 1;
- -- end if;
- -- end if;
- -- end if;
- --
- -- last_button_value <= '0';
- -- end process;
- end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement