|
各位大大,想請問一下,我現在要做,當我鍵盤連續輸入,結果只取最後一個鍵盤輸入的值
) N. C* v/ ~; L- N( }$ O0 S
3 X8 D- r+ R* a4 _$ m3 @這是一個自動控制鍵盤的控制器,我可以先輸入數字,然後再輸入指令,我的機器就會動那個指令n次
& t3 d2 f( `; B/ ~+ L; I6 T
+ ^- M/ `! h8 h- J7 Z4 L例如,先按5再按前進,自東車就會前進5次,如果只按前進,則自動車會自動前進1次(預設值)9 h# d% r$ q1 @* c4 \. G
1 E4 M2 s9 Q8 ^$ d3 h0 D當我遇到連續輸入字數時,我不管要輸入多少數字,永遠取最後一個的數字當動作次數& m6 l2 q+ U. Z% ]) m, }& L7 _" c
i" |/ r& B2 v' W n: }7 [3 |現在我卡在不知道如何判別這種機制,希望有高手可以給我一點提示幫我解決,謝謝!$ q2 r$ u2 R7 E' z7 j" b
0 g, x8 x) e8 r( }library IEEE; ~ G, v J) _* J) Q* {/ j; O
use IEEE.STD_LOGIC_1164.ALL;
+ u% e5 \+ z5 T# wuse IEEE.STD_LOGIC_ARITH.ALL;
7 A t4 g* F3 `; R: Q( i: h3 C) @use IEEE.STD_LOGIC_UNSIGNED.ALL;
! d9 E i0 o( w/ r1 E/ e+ sENTITY key_controller IS8 G- D- }- ^! R7 G: N- a
PORT(
# G Q( U5 Z' }3 n clk : IN std_logic;! j3 E3 Q8 y+ R8 ~( S0 a
col : OUT std_logic_vector ( 3 DOWNTO 0 );
. J" ^* e# y6 R key_out : OUT std_logic;. l; y) x& m/ }: k- m T9 V; a
row : IN std_logic_vector ( 3 DOWNTO 0 );
% K- ?5 A7 B4 w. U5 L3 W digit : OUT std_logic_vector ( 5 DOWNTO 0 )
& t8 {+ {# {5 g) ?+ v V );# A: D* g3 d+ [( `
END key_controller ;9 z1 b& x2 W; [; _
architecture behavioral of key_controller is
/ L4 O# }1 n% l4 Rsignal clkm : std_logic;
) C# A6 ?4 _4 s8 w( h! z" ?signal key_pressed : std_logic;
& S+ y: B2 L( tsignal key_valid : std_logic := '0';. E) g4 K+ [/ c
signal dbnq : std_logic_vector( 6 downto 0 ) := ( others => '0' ); --debounce
! v+ Q' Q- `# {6 Z9 ~5 }signal scan_cnt : std_logic_vector( 3 downto 0 ) := ( others => '0' );& \3 r/ _* [# Y* ^% P) P7 G5 v
signal cmd_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
( b3 S. B; q! Q8 k/ ~! Nsignal num_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );+ p# h9 A' I' S' [, b
begin
: \$ u. L* V0 b: B( @count : process( clk )
3 g8 c a7 n6 I8 l0 ~ begin9 | m; u* ]; }; ]. W) A5 l) n; E
if rising_edge( clk ) then6 S; z! c! I+ p$ S/ l* p3 Z
count_t <= count_t + 1;, m B# D" e$ v$ H/ r0 s
end if;4 s" [6 s+ B& Q2 R
clkm <= count_t( 15 ); --scaning clock generaterd7 V2 u4 t6 B5 y% `, W) T
end process;
5 m" t7 i! l2 ^4 o; q) O-----------------------------------------------
E! ?$ H- n$ ]$ B3 Y5 c) Okeyboard: process( clkm, scan_cnt, key_pressed )
) k- l: ?/ c, h( n/ Gbegin
4 h" j! S C3 U, K. W% I--scan_cnt is a 4 bit up scan counter
4 c2 x/ I# U! J# i) p case scan_cnt( 3 downto 2 ) is --keypad column scaning, decoder functional + f. X, w: a: T; y4 M, c- y# F
when "00" => col <= "0111";
5 Z4 b1 C+ z2 @+ U6 w8 Z: ~ when "01" => col <= "1011";5 x2 P: J6 `" V: Q, i8 [' ]
when "10" => col <= "1101"; {5 n I- [2 A8 h4 f" m* I
when "11" => col <= "1110";
% J! h- O, i- r when others => col <= "1111"; ( X2 d8 T H, ~4 V
end case;8 @$ C; Z8 ]+ o$ M, w3 A
---------------------------------------------
1 v) _2 ?& _; [' v& _# J9 [ case scan_cnt( 1 downto 0 ) is --keypad row scaning, mux functional
1 M" W$ V4 [8 r, K) m/ Z when "00" => key_pressed <= row(0);
; _) Y! y3 y+ f5 E( k) \, j when "01" => key_pressed <= row(1);7 i# U7 J, J Q* s1 e
when "10" => key_pressed <= row(2);
7 v) j, F( ^* |3 e6 k when "11" => key_pressed <= row(3);( R/ X/ S) t- `0 Z
when others => null;
( }5 v0 N% h- w7 b" d end case;; E" }, o3 \& D# d# ]
----------------------------------------------------------------' M6 A* o. a/ r, \$ ]) T P# y( Z
if rising_edge( clkm ) then
, _' F9 k0 y. [+ F3 V* Z" ^0 F if key_pressed = '1' then --no key_pressed in, continue to scan6 a, `0 M) `. w8 f, F$ B( Z. E- a
scan_cnt <= scan_cnt + 1;) O6 }" F! ^6 C: n
dbnq <= "1111111";! b$ j. s8 ^ a% V$ E
elsif key_pressed = '0' then --key_pressed in$ M$ @, [, Q- l1 x
dbnq <= dbnq - 1;
! {9 |1 q, t" P, P- z: u end if;8 G& N3 W- ?1 X0 W
----- debounce
9 H# Z: L) p) E if dbnq = "1001111" then --key_pressed debounce
, g$ a4 G* [8 Q, i& W$ j# C key_valid <= '1';
3 x: d4 ]$ t5 C1 o else key_valid <= '0';+ {! z* k9 k. k! E
end if; ( ]$ h8 y; N5 s; M- b& C- ?
end if;
8 b% b% C, Q+ F& a5 Z4 _! ^/ }4 j-- scan_out <= scan_cnt;8 _& k0 _+ H: I1 \) v
end process;* u; c& x) s! L! z5 I/ ~- @5 c
---------------------------------------------------------------------------1 w! l1 D* v6 b0 }2 Z; [ X8 ?
keycode : process( clk, scan_cnt, key_valid )
6 j p+ e( e" G* k* t0 j4 b8 a begin [5 N. Y" b& O8 I# S7 w$ K
------------- asynchronous expression
; |7 {" ~3 `, D/ g: b2 @( v if key_valid'event and key_valid = '1' then
. \/ V# c7 G3 q; j/ Z5 W if scan_cnt( 3 ) = '1' then8 j1 e: L+ C" X6 r7 }
cmd_flag <= '1';& E& x& V' h* Z# Y$ i
cmd_buf <= scan( 2 downto 0 ); --command store into ram
/ t" |, Q9 q$ c4 f' ^( \ elsif scan_cnt( 3 ) = '0' then0 E" p* ]& b6 `
num_flag <= '1';
: P! ?8 q. Z. _2 ^9 V S num_buf <= scan( 2 downto 0 ); --numeric store into ram
9 {* [1 y$ u" j else num_buf <= "000", --numeric store into ram
) n7 C) H* K( W& e/ J! Q cmd_buf <= "000"; --numeric store into ram4 O' K, g9 n5 n7 h6 a8 S
num_fleg <= '0';
! B5 Q4 ~! e5 L: W cmd_fleg <= '0';1 @, k& @* Y _, A- }. v: j
end if;
8 e$ k. K8 |9 ]- A2 ?- F3 \3 o2 R2 T& T digit <= cmd_buf & num_buf; -- out of key controller- L* N! v: m. t1 v d. w9 }
end if;/ R& `8 {7 {# \3 t' p! U: e% l
end process;
* D, P" `5 k8 o3 {% u" R-------------------
8 E9 T: @* T9 i$ J: l key_out <= key_valid;
: w5 m6 v# C8 ^$ R9 e- d digit <= seg4bit;
+ g, o+ [' K9 V# t* Nend behavioral; |
|