使用斯巴达3(vhdl)从键盘添加2个数字

时间:2014-03-15 03:10:27

标签: vhdl

我的任务是从键盘输入2个输入,这些输入是从0到9的数字并添加它们。问题在于 存储这些数字。我想先保存(输入编号)进入" a"然后按(输入编号)进入 " B"但是通过使用以下代码,首先在a和b中按商店。第二次按下是没用的。这里 scan_code =按下按钮的扫描码(键盘接口代码的输出) a =二进制数(例如,如果我第一次按" 1"则代码检查扫描码并将" 1"的二进制值分配给a)。 谁能帮忙?

process (clk, scan_code, cin)
  variable scancode1 : std_logic_vector (7 downto 0) := "00000000";
  variable cin2      : std_logic_vector(2 downto 0);
begin
  if(clk'event and clk = '1') then

    scancode1 := scan_code;
    a         <= "0000";
    b         <="0000";
    if (scancode1 = "00010110") then
      a <= "0001";
    elsif (scancode1 = "00011110") then
      a <="0010";
    elsif (scancode1 = "00100110") then
      a <="0011";
    elsif (scancode1 = "00100101") then
      a <="0100";
    elsif (scancode1 = "00101110") then
      a <="0101";
    elsif(scancode1 = "00110110") then
      a <="0110";
    elsif (scancode1 = "00111101") then
      a <="0111";
    elsif (scancode1 = "00111110") then
      a <="1000";
    elsif (scancode1 = "01000110") then
      a <="1001";
    elsif (scancode1 = "01000101") then
      a <="0000";
    end if;

    if (scancode1 = "01010101") then    --scancode for + sign
      a <=a;
    end if;

    if (scancode1 = "00010110") then
      b <="0001";
    elsif (scancode1 = "00011110") then
      b <="0010";
    elsif (scancode1 = "00100110") then
      b <="0011";
    elsif (scancode1 = "00100101") then
      b <="0100";
    elsif (scancode1 = "00101110") then
      b <="0101";
    elsif(scancode1 = "00110110") then
      b <="0110";
    elsif (scancode1 = "00111101") then
      b <="0111";
    elsif (scancode1 = "00111110") then
      b <="1000";
    elsif (scancode1 = "01000110") then
      b <="1001";
    elsif (scancode1 = "01000101") then
      b <="0000";
    end if;

    sum(0)  <= a(0) xor b(0) xor cin;
    cin2(0) := (a(0) and b(0)) or (cin and (a(0) xor b(0)));

    sum(1)  <= a(1) xor b(1) xor cin2(0);
    cin2(1) := (a(1) and b(1)) or (cin2(0) and (a(1) xor b(1)));

    sum(2)  <= a(2) xor b(2) xor cin2(1);
    cin2(2) := (a(2) and b(2)) or (cin2(1) and (a(2) xor b(2)));

    sum(3) <= a(3) xor b(3) xor cin2(2);
    cout   <= (a(3) and b(3)) or (cin2(2) and (a(3) xor b(3)));

  end if;
end process;

1 个答案:

答案 0 :(得分:0)

&#34;&#34;和&#34; b&#34;正在你这样写的同时进行更新(同一&#34;扫描码&#34;检查&#34; a&#34;&#34; b&#34;在同一个时钟上)。考虑使用事件触发器,如下例所示:

...
if reset='1' then
   a<=(others => '0');
   b<=(others => '0');
   event_last<='0';
   event_nr<=0;

elsif rising_edge(clk) then 
   event_last<=event;

   -- trigger on "rising edge" of event
   if event='1' and event_last='0' then

        case event_nr is
        -- first event is "a"
        when 0 =>
            event_nr<=1;

            if (scancode1 = "00010110") then
                 a <= "0001";
            ...

        -- second event is "b"
        when others => 
            event_nr<=0;

            if (scancode1 = "00010110") then
                b <= "0001";
            ...
        end case;

   ...
   ...