2013-03-25 101 views
2

好吧,所以我试图实现键盘控制器与altera DE2 FPGA板一起使用,并遇到一些问题。我已经在quartus模拟器中运行了这些代码,并且一切似乎都在做我认为应该做的事情。但是,当我尝试将其编程到FPGA上时,没有任何工作。我将其目标锁定在我模拟ps/2时钟的方式,而系统时钟看起来并不像它们实际运行的方式。ps/2键盘接口VHDL

我模拟了系统时钟在50 mhz,20ns周期和ps2clock周期为90ns。当将ps2data设置为整个仿真中的随机值时,正确的位被加载到8位扫描代码中。问题在于,当编程到板上时,状态机永远不会离开空闲状态。当数据位为零时,状态机应在ps2时钟的下降沿保持空闲状态,这似乎永远不会发生。我有ps2data和ps2clock引脚连接到正确的输入,但似乎无法找出问题。

我没有添加测试此功能的顶级实体,但它只是输出keyCode并将其发送到7seg显示中的一个。我觉得这个答案与ps2clock有关,我只是不确定究竟是什么。

library IEEE; 
use IEEE.STD_LOGIC_1164.ALL; 
use IEEE.STD_LOGIC_ARITH.ALL; 
use IEEE.STD_LOGIC_UNSIGNED.ALL; 

entity keyboard is 
    Port (Clk   : in std_logic; --system clock 
      ps2Clk  : in std_logic; --keyboard clock 
      ps2Data  : in std_logic; --keyboard data 
      reset  : in std_logic; --system reset 
      keyReady  : out std_logic; 
      DoRead  : in std_logic; -- when to read 
      keyCode  : out std_logic_vector(7 downto 0); 
      pFalling  : out std_logic; --debugging 
      pFixedClk : out std_logic_vector(1 downto 0); --debugging 
      divClock_out : out std_logic; --debugging 
      clockCount_out : out std_logic_vector(9 downto 0); --debugging 
      testDiv_out  : out std_logic; 
      bitCount_out : out std_logic_vector(3 downto 0); 
      shiftIn_out : out std_logic_vector(8 downto 0)); --debugging 
end keyboard; 

architecture Behavioral of keyboard is 

component div_counter is 
    Port(clk, reset : in std_logic; 
       Q : out std_logic_vector(9 downto 0)); 
end component div_counter; 

signal shiftIn    : std_logic_vector(8 downto 0); -- shifted in data 
signal ps2fixedClock  : std_logic_vector(1 downto 0); -- 2 bit shift register 
signal divClock    : std_logic ; -- main clock/512 
signal clockCount   : std_logic_vector(9 downto 0); -- debugging 
signal ps2falling   : std_logic ; 
signal bitCount    : std_logic_vector(3 downto 0); 
signal keyReady_sig   : std_logic; 

type state_type is (idle, shift, ready); 
signal state : state_type; 
begin 

keyReady <= keyReady_sig; 

------------------------------- 
--- counter to divide the main clock by 512 
------------------------------- 
counter : div_counter 
    Port map(clk => Clk, 
      reset => reset, 
       Q => clockCount); 

clockCount_out <= clockCount;    
divided_clock : process (clockCount) 
begin 
    if clockCount = "1000000001" then 
     divClock <= '1'; 
    else 
     divClock <= '0'; 
    end if; 
end process divided_clock;     

testDiv_out <= divClock; 
------------------------------------ 
------ 2 bit shift register to sync clocks 
------------------------------------ 
ps2fixed_Clock : process (reset, divClock) 
begin 
    if reset = '1' then 
     ps2fixedClock <= "00"; 
    elsif (divClock'event and divClock = '1') then 
     ps2fixedClock(0) <= ps2fixedClock(1); 
     ps2fixedClock(1) <= ps2Clk; 
    end if; 
end process ps2fixed_Clock; 

pFixedClk <= ps2fixedClock; 
----------------------------------- 
-------- edge detector 
----------------------------------- 
process (ps2fixedClock) 
begin 
    if ps2fixedClock = "01" then 
     ps2falling <= '1'; 
     else 
     ps2falling <= '0'; 
    end if; 
end process; 

pFalling <= ps2falling; 
bitCount_out <= bitCount; 

-------------------------------- 
------- state machine 
-------------------------------- 
state_machine : process (divClock, reset) 
begin 
    if (reset = '1') then 
     state <= idle; 
     bitCount <= "0000"; 
     shiftIn <= (others => '0'); 
     keyCode <= (others => '0'); 
     keyReady_sig <= '0'; 
    elsif (divClock'event AND divClock = '1') then 

     if DoRead='1' then 
      keyReady_sig <= '0'; 
     end if; 

     case state is 
     when idle => 
      bitCount <= "0100"; 
      if ps2falling = '1' and ps2Data = '0' then 
       state <= shift; 
      end if;   
     when shift => 
       if bitCount >= 9 then 
        if ps2falling = '1' then -- stop bit 
         keyReady_sig <= '1'; 
         keyCode <= shiftIn(7 downto 0); 
         state <= idle; 
        end if; 
       elsif ps2falling='1' then 
        bitCount <= bitCount + 1; 
        shiftIn(7 downto 0) <= shiftIn(8 downto 1); 
        shiftIn(8) <= ps2Data; 
       end if; 
     when others => 
      state <= idle; 
    end case; 
    end if; 
end process;  

shiftIn_out <= shiftIn; 
end Behavioral; 

回答

0

您尝试将ps2clock同步到您的divClock。然而,divClock是一个使能信号而不是时钟。它并不经常活跃。

我建议你在ps2fixed_Clock过程中使用CLK

ps2fixed_Clock : process (reset, clk) 
begin 
    if reset = '1' then 
     ps2fixedClock <= "00"; 
    elsif (rising_edge(clk)) then 
     ps2fixedClock(0) <= ps2fixedClock(1); 
     ps2fixedClock(1) <= ps2Clk; 
    end if; 
end process ps2fixed_Clock; 

另外,如果你想用一个时钟分频器(divided_clock过程),你应该在你的state_machine过程

state_machine : process (clk, reset) 
begin 
    if (reset = '1') then 
     ... 
    elsif (rising_edge(clk)) then 
     ... 

使用CLK,你可以生成使能信号(如您所做的那样),并在之后使用它您同步了时钟,例如在状态机中!

1

说回回答这个有点晚....

事实证明为什么这是不工作是因为我使用的是USB的原因 - > PS2适配器,而不是原来的PS2接口键盘。