0
我需要在VHDL編碼鍵盤編碼器。我正在考慮與4個州合作。當在columnn1掃描行1,行2,行2,行4。這樣4個狀態應該根據我是不夠好,但我從網上找到了一個例子使得它具有8種狀態:鍵盤編碼器爲什麼有8個狀態?
key_scanner_sm : process (clk)
begin -- process key_scanner
if clk'event and clk = '1' then
if state_inc = '1' then
-- reset scan_complete
scan_complete <= '0';
case key_state is
when pulse_row_1 =>
key_read <= (others => '0');
key_row <= "0001";
key_state <= read_row_1;
when read_row_1 =>
case key_col is
when "0001" => key_read <= X"31"; -- 1
when "0010" => key_read <= X"32"; -- 2
when "0100" => key_read <= X"33"; -- 3
when "1000" => key_read <= X"41"; -- A
when others => null;
end case;
key_state <= pulse_row_2;
when pulse_row_2 =>
key_row <= "0010";
key_state <= read_row_2;
when read_row_2 =>
case key_col is
when "0001" => key_read <= X"34"; -- 4
when "0010" => key_read <= X"35"; -- 5
when "0100" => key_read <= X"36"; -- 6
when "1000" => key_read <= X"42"; -- B
when others => null;
end case;
key_state <= pulse_row_3;
when pulse_row_3 =>
key_row <= "0100";
key_state <= read_row_3;
when read_row_3 =>
case key_col is
when "0001" => key_read <= X"37"; -- 7
when "0010" => key_read <= X"38"; -- 8
when "0100" => key_read <= X"39"; -- 9
when "1000" => key_read <= X"43"; -- C
when others => null;
end case;
key_state <= pulse_row_4;
when pulse_row_4 =>
key_row <= "1000";
key_state <= read_row_4;
when read_row_4 =>
case key_col is
when "0001" => key_read <= X"2A"; -- *
when "0010" => key_read <= X"30"; -- 0
when "0100" => key_read <= X"23"; -- #
when "1000" => key_read <= X"44"; -- D
when others => null;
end case;
key_state <= pulse_row_1;
scan_complete <= '1';
when others => null;
end case;
end if;
end if;
end process key_scanner_sm;
是否有這背後一個很好的理由,沒有任何人知道?
因此,我們可以簡單地在4個狀態寫這篇文章,我只是不明白爲什麼編碼器做到了在8個州?爲什麼使它更復雜?我正在尋找一個很好的理由,是嗎?爲了保護例如像2按鈕按下等情況? – Anarkie
他們沒有意識到裁減一些州的機會。你在網上看到的大多數VHDL代碼在質量上差強人意。它並不總是代表最佳實踐。在這種情況下,浪費週期並不重要,因爲這是一小部分邏輯,對於人類交互來說足夠快。你需要注意的是實現濾波器來消除矩陣輸入,避免從彈跳按鈕記錄多個按鍵。在將外部信號直接饋入去抖濾波器之前,還需要將'key_col'輸入同步到您的時鐘域。 –
要避免的一個例子是用於同步過程的VHDL-87樣式clock'event和clock ='1'樣板。你應該使用VHDL-93的rising_edge函數,因爲它在測試之前有一個to_X01的內部調用來降低時鐘信號的強度。這是更可讀的,並且對於時鐘可以是'H'或'L'的模擬情況下的正確行爲是重要的。許多現代的VHDL仍舊以舊的方式編碼,因爲寫得不好,過時的教學材料。當我看到代碼少於15歲的VHDL-87時,我懷疑它的質量。 –