0
\$\begingroup\$

The counter only last for one clock cycle but I want the test_counter remain unchanged until next button_case.

test_counter should transition like this "0000" -> "0010" -> "0001" ->"0100" -> "1000" -> "0000"

I have tried this below but it's not working.

when others      => null; -- do nothing

when others => knapp_in_1_n <= knapp_in_1_n; gives me the following wave.

enter image description here

enter image description here

enter image description here

library ieee;
use ieee.std_logic_1164.all;
use IEEE.numeric_std.all;
entity testbench_keypad_v2 is
end entity;

-- testbench for keypad scan

architecture rtl OF testbench_keypad_v2 is

-- constants 
constant sys_clk_period : TIME := 100 ns;

-- signals 
signal sys_clk           : std_logic;
signal led               : std_logic_vector(3 downto 0);
signal knapp_in_1_n      : std_logic_vector(3 downto 0) := b"0000";


signal reset_n           : std_logic;

constant default_value   : unsigned(25 downto 0) := 26d"200000";   -- 20ms

signal  test_counter     : unsigned(25 downto 0) := 26d"0";
signal prev_test_counter : unsigned(25 downto 0) := (others => '0');

component keypad_v1
port (
    clk, reset_n         : in  std_logic;
    key_in               : in  std_logic_vector(3 downto 0);
    key_out              : out std_logic_vector(3 downto 0);
    led_display          : out std_logic_vector(3 downto 0)
);
end component;

begin

    i1 : keypad_v1
    PORT MAP (
        -- list connections between master ports and signals
        clk                => sys_clk,
        led_display        => led,
        key_in             => knapp_in_1_n,
        reset_n            => reset_n
    );
 
    clock: process
    begin
        -- Clock period 
        sys_clk <= '0'; 
        wait for sys_clk_period/2;
        sys_clk <= '1';
        wait for sys_clk_period/2;
         -- Här upprepas processen, ”GO TO Start”
    end process;

    reset: process
    begin
         -- Reset at start up
        reset_n <= '0';
        wait for 5*sys_clk_period;
        -- Target running
        reset_n <= '1';
        wait; -- test case 5
        -- ett tomt “WAIT;” stannar processen, eftersom det finns inget villkor
    end process;

    counter_cases : process(sys_clk)
    begin
        if rising_edge(sys_clk) then
            if test_counter < default_value then
                test_counter <= test_counter + 1;
            else
                test_counter <= (others => '0') after 150 ns; -- 150 ns is 1.5 sys_clk
            end if; 
        end if;
    end process;

    button_case: process(test_counter)
    begin
        case test_counter is
            when 26d"100001" => knapp_in_1_n <= "0001";
            when 26d"266668" => knapp_in_1_n <= "0010";
            when 26d"800014" => knapp_in_1_n <= "1000";
            when 26d"600015" => knapp_in_1_n <= "0100";
            when others      => knapp_in_1_n <= "0000"; -- do nothing
      end case;
    end process;      

end rtl;
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

If you don't want your test_counter to reset, then you shouldn't do it

...
else
                test_counter <= (others => '0') after 150 ns; -- 150 ns is 1.5 sys_clk
...

But do be honest, I'm not really sure what you want to achieve. It seems you want button_case to depend on test_counter and vice-versa.

Update:

If you want knapp_in_1_n to keep its value after a case assignment, change the others statement to

when others => knapp_in_1_n <= knapp_in_1_n;

But you then need an additional condition/case to set knapp_in_1_n back to "0000" but only you know when this is.

\$\endgroup\$
7
  • \$\begingroup\$ I want it to keep its value until next transition. test_counter should transition like this "0000" -> "0010" -> "0001" ->"0100" -> "1000" -> "0000" \$\endgroup\$
    – kile
    Commented Feb 27, 2023 at 8:19
  • \$\begingroup\$ you mean knapp_in_1_n should transition like this? I updated the answer \$\endgroup\$
    – po.pe
    Commented Feb 27, 2023 at 8:53
  • \$\begingroup\$ It doesn't work even with when others => knapp_in_1_n <= knapp_in_1_n; See my updated question \$\endgroup\$
    – kile
    Commented Feb 27, 2023 at 13:59
  • \$\begingroup\$ What exactly is the problem with the new sequence? knapp_in_1_n now seems to be stable after change... \$\endgroup\$
    – po.pe
    Commented Feb 27, 2023 at 14:17
  • \$\begingroup\$ knapp_in_1_n should change to knapp_in_1_n <= "0100" when test_counter reaches 26d"600015" However, It doesn't change in the wave form \$\endgroup\$
    – kile
    Commented Feb 27, 2023 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.