Altera DE1 Board Read KEY

vhdl

I need to press and hold key 1 and than press key 2 to increase value.
How to Read keys only if both keys are pressed. This is what is have which is not working correct. (programming languages vhdl).

if key(0) and key(1) are pessed do somthing

PORT (
KEY : IN STD_LOGIC_VECTOR(3 DOWNTO 0);
);

SIGNAL A, B, C, D : STD_LOGIC_VECTOR(1 DOWNTO 0);

BEGIN
A <= KEY(0) & KEY(1);
B <= KEY(0) & KEY(2);
C <= KEY(0) & KEY(3);

Readkey: process(KEY)
begin
if (A = 1) then
D <= "00";
if (B = 1) then
D <= "01";
if (C = 1) then
D <= "10";
end if;
end if;
end if;
end process;

Best Answer

I'm not surprised it's not working. Now, I don't know the slightest thing about VHDL, but I do know about programming in general.

Firstly: INDENT YOUR CODE With no indents the code is all but unreadable. Here is your code indented properly:

PORT (
    KEY : IN STD_LOGIC_VECTOR(3 DOWNTO 0);
);

SIGNAL A, B, C, D : STD_LOGIC_VECTOR(1 DOWNTO 0);

BEGIN
    A <= KEY(0) & KEY(1);
    B <= KEY(0) & KEY(2);
    C <= KEY(0) & KEY(3);

Readkey: process(KEY) 
begin
    if (A = 1) then
        D <= "00";
        if (B = 1) then
            D <= "01";
            if (C = 1) then
                D <= "10";
            end if;
        end if;
    end if;
end process;

Now you can see one major flaw in your code at a glance.

C being 1 will only be tested if B is 1, which itself will only be tested if A is 1. The if statements shouldn't be nested like that.

Secondly, (again, I don't know VHDL) is how A B and C are being assigned. In a classic programming language you would need to assign the incoming values to A B and C every time you want to test them, otherwise you won't ever see any changes. This may not be the case in VHDL, but it's something to look more closely at.

Related Topic