|
各位大大,想請問一下,我現在要做,當我鍵盤連續輸入,結果只取最後一個鍵盤輸入的值# y: A7 a ^/ P
$ a( i& B O" |9 i* r這是一個自動控制鍵盤的控制器,我可以先輸入數字,然後再輸入指令,我的機器就會動那個指令n次
0 o& j0 u' S- o q2 ]! {+ Z8 G& l$ S" e! f; ]7 f
例如,先按5再按前進,自東車就會前進5次,如果只按前進,則自動車會自動前進1次(預設值)' y0 F0 f5 b7 ^( ?/ G$ F
7 s5 w9 W! l2 N# s2 t當我遇到連續輸入字數時,我不管要輸入多少數字,永遠取最後一個的數字當動作次數
! _! C; X: w/ R+ H4 N0 i9 c- Y% Z! C5 H( z
現在我卡在不知道如何判別這種機制,希望有高手可以給我一點提示幫我解決,謝謝!
: k! N5 ~" R! q" `" s' L* N
0 f! Q) d) r6 B4 X; zlibrary IEEE;
$ T- _ o8 |2 quse IEEE.STD_LOGIC_1164.ALL;1 f; i8 ^$ T7 b4 u3 \" a# F
use IEEE.STD_LOGIC_ARITH.ALL;( n* i2 c0 s' |4 X( k
use IEEE.STD_LOGIC_UNSIGNED.ALL;9 k5 s) y' A. z# V9 e/ r, p
ENTITY key_controller IS
2 I$ O4 l* j5 T! @+ U& u PORT( % w5 `; C1 `* P
clk : IN std_logic; s$ \, n9 ]! O) U% R. H
col : OUT std_logic_vector ( 3 DOWNTO 0 );4 q( t! M3 X3 c
key_out : OUT std_logic;& [* O& U9 D: ^/ q$ t% t
row : IN std_logic_vector ( 3 DOWNTO 0 );! J) d7 I6 K% S# c5 ]- {
digit : OUT std_logic_vector ( 5 DOWNTO 0 )- U F, p3 g) C, ^8 P
);
5 w9 |# h( d. V+ O+ ~! f) OEND key_controller ;) b- x! L# s, `# n! N! R) X2 H
architecture behavioral of key_controller is2 A% T8 O. Q* ^, \
signal clkm : std_logic;- k& p7 q& z: K6 G* i
signal key_pressed : std_logic;
! Y# T/ d) a$ m5 s% Nsignal key_valid : std_logic := '0';
- C3 Z5 Y! f& m# ksignal dbnq : std_logic_vector( 6 downto 0 ) := ( others => '0' ); --debounce
0 ]; t' }8 W/ p- C- e$ J1 u9 o+ Csignal scan_cnt : std_logic_vector( 3 downto 0 ) := ( others => '0' );6 k0 X, @* z3 L
signal cmd_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
! N8 {3 V( t' X, Isignal num_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
& B3 { f5 A" D( Lbegin, ]! X$ l& x$ a7 P; p; p4 b
count : process( clk )
# |0 H$ x" l6 ~7 k9 X( L begin
7 _/ h4 \2 E W+ ~$ L if rising_edge( clk ) then- r q9 W/ v# ?
count_t <= count_t + 1;
5 J9 ~4 b( n- _ end if;) A1 Q/ M4 X1 I% |
clkm <= count_t( 15 ); --scaning clock generaterd) J Y3 U& _9 u1 m4 s
end process; ( b; w( f' a9 }" p E% P1 ]
-----------------------------------------------
u' x) {4 M$ G( j: ^$ A( h2 E O9 jkeyboard: process( clkm, scan_cnt, key_pressed )1 }, g7 a. q9 g: G, G
begin
1 {* P( z7 L' b7 P* S+ X; p--scan_cnt is a 4 bit up scan counter X: ?9 H4 ^& y' {# h0 n' d
case scan_cnt( 3 downto 2 ) is --keypad column scaning, decoder functional $ K3 T& R* F4 ^* z5 I9 K
when "00" => col <= "0111";
: H( p( b9 h% F3 I when "01" => col <= "1011";
3 Y5 h7 W2 K( M u3 Z when "10" => col <= "1101";, r, ]% q! J7 B% a( s7 l* l6 ~
when "11" => col <= "1110";. k H* f% O$ |* `+ X# C
when others => col <= "1111";
+ J, b9 W6 }/ Z/ ` end case;2 B% J$ [* e% i' l1 N
---------------------------------------------
: s1 Z# D+ t% f case scan_cnt( 1 downto 0 ) is --keypad row scaning, mux functional5 f9 x3 R {" s- w' H- d
when "00" => key_pressed <= row(0);* g1 x; I ?* o6 Z, T- q
when "01" => key_pressed <= row(1);1 [9 A6 P5 O* i% D: ?+ j+ T, a
when "10" => key_pressed <= row(2);& \% k$ m$ V' T( J( a$ i; t
when "11" => key_pressed <= row(3);
' P+ F# G3 b9 t2 D/ Y- n when others => null;8 G, x t* A4 o" x4 }, X" X
end case;
, p0 K8 i. L2 u1 @9 J----------------------------------------------------------------
/ u) P7 {" S* l. _ if rising_edge( clkm ) then
' S/ R/ z+ k3 z; V if key_pressed = '1' then --no key_pressed in, continue to scan2 d3 n6 A( K3 H5 W4 j% U% R$ O
scan_cnt <= scan_cnt + 1;
, ^; D t* `) C8 o$ ?, X dbnq <= "1111111";
' y- X- m5 }3 G5 D+ Z, U elsif key_pressed = '0' then --key_pressed in0 X8 D! l$ T* K$ K C
dbnq <= dbnq - 1;, B5 `! j9 J+ e2 w$ W1 W
end if;# _8 z7 x4 e' X$ d4 y
----- debounce ) R2 `$ w0 _# N/ c9 I7 |! ]" E
if dbnq = "1001111" then --key_pressed debounce
# ^4 {% _9 m8 m+ z' c9 K key_valid <= '1'; % I; r8 P5 S: g7 K* a
else key_valid <= '0';
( w R0 |( D) x7 @5 Q6 ~0 e, f end if; " K: Y0 Z# i+ k7 f
end if;# k2 [0 j6 M2 k. N3 h7 s
-- scan_out <= scan_cnt;
6 u, i- ?& f( y# s5 { end process;
: O4 A4 _/ i: k: N/ Z: c9 Q---------------------------------------------------------------------------
B) k' O" I: J& @( gkeycode : process( clk, scan_cnt, key_valid )6 ?& m% j8 K8 ~2 F" s9 ~4 F/ m! a0 V
begin
4 R# \, I H$ g; v------------- asynchronous expression
/ @3 X. \# Q! L if key_valid'event and key_valid = '1' then7 S+ x, ?0 N& w- ~
if scan_cnt( 3 ) = '1' then1 | ]* C" @2 T6 T# ^7 b
cmd_flag <= '1';
% f6 A/ J* }2 ^( ^ cmd_buf <= scan( 2 downto 0 ); --command store into ram7 m2 n b, z# n9 X2 q$ V6 T9 ?
elsif scan_cnt( 3 ) = '0' then$ [8 P$ [$ \+ F4 O! D
num_flag <= '1'; 8 u- V2 ^; y$ ]2 s( o
num_buf <= scan( 2 downto 0 ); --numeric store into ram
# H- U( e; K; a+ S3 F) D( y9 r else num_buf <= "000", --numeric store into ram ; p) @4 I5 B6 E& {3 w
cmd_buf <= "000"; --numeric store into ram+ O# E9 q. ?( w4 f; l- ?
num_fleg <= '0';9 j) R" n; j5 T+ J$ M* U; I+ N
cmd_fleg <= '0';) u, T4 l/ q! d8 F7 I1 Y. i
end if;# F( h% g" B, m* g% G) u0 Z
digit <= cmd_buf & num_buf; -- out of key controller* a- K, {) c5 ^- C; ^4 ~. X- k% d
end if;$ K3 t$ s# F) G0 ~9 |% f* v$ w
end process;
; f; r* w( M' m& f) h5 w3 X! r+ t-------------------
4 e' s( v* E$ e3 L7 O8 C key_out <= key_valid;9 G! M. j X$ K4 h7 R( ?. K$ k
digit <= seg4bit;7 D% K# R6 A6 Z% J' A! X( x
end behavioral; |
|