|
各位大大,想請問一下,我現在要做,當我鍵盤連續輸入,結果只取最後一個鍵盤輸入的值
$ j7 _# c. _& `- P& v8 ]8 G7 Y, z# D5 [8 R1 K1 a6 P7 l9 T6 h9 P- p
這是一個自動控制鍵盤的控制器,我可以先輸入數字,然後再輸入指令,我的機器就會動那個指令n次3 \" k0 E( S2 X6 m" d( S8 x2 N
& V# Z; \' g8 n5 R9 o% y8 U r
例如,先按5再按前進,自東車就會前進5次,如果只按前進,則自動車會自動前進1次(預設值)$ }, S1 o4 [ T+ Q3 W
5 y# P5 G: |. c! \0 z3 I* w- k當我遇到連續輸入字數時,我不管要輸入多少數字,永遠取最後一個的數字當動作次數+ k* K' W6 |6 E4 o
0 q9 e$ T" x$ T' p; I
現在我卡在不知道如何判別這種機制,希望有高手可以給我一點提示幫我解決,謝謝!9 b& J* h5 A9 n2 {/ f6 T8 M% ?
# @5 Z; T: Y2 _2 e; Q8 E9 H; \library IEEE;
5 S1 u/ L6 E) u, |2 luse IEEE.STD_LOGIC_1164.ALL;
* z/ K1 Q# k- V' x9 F8 R+ X. uuse IEEE.STD_LOGIC_ARITH.ALL;7 p0 f. R7 Z- j
use IEEE.STD_LOGIC_UNSIGNED.ALL;
0 s+ ~# G/ R6 @ V$ jENTITY key_controller IS% f: c2 J0 l' m0 E8 O
PORT(
5 o% ~. p& w4 k2 o: ~ clk : IN std_logic;
# o* J; ~, A0 ]* J; S7 t col : OUT std_logic_vector ( 3 DOWNTO 0 );9 c. f7 z4 j2 }+ X
key_out : OUT std_logic;
$ H! }. F/ }" {2 F6 V row : IN std_logic_vector ( 3 DOWNTO 0 );! ]- ?9 [; D! ^# a4 a2 U
digit : OUT std_logic_vector ( 5 DOWNTO 0 )
1 C8 N" ~. Y4 a5 N# J );4 r/ v3 [4 }( T; `9 m+ I
END key_controller ;
+ k: `2 l8 G w: Z* Xarchitecture behavioral of key_controller is
: I) a: a/ {6 Z- q- n& psignal clkm : std_logic;3 H1 R' O9 b; H6 @ Y
signal key_pressed : std_logic;
* S: p+ N6 E# t- [5 j- A9 Rsignal key_valid : std_logic := '0';
% }1 ]2 `( ]( u6 F8 X ^1 ?8 lsignal dbnq : std_logic_vector( 6 downto 0 ) := ( others => '0' ); --debounce+ r0 x% u+ v! n& \. B
signal scan_cnt : std_logic_vector( 3 downto 0 ) := ( others => '0' );' l' g0 D/ H9 v: q' z9 B0 p
signal cmd_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
- L R& D {+ a; e* S$ X$ Z$ ?signal num_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
6 P- D$ ]; s: U+ r3 G8 xbegin1 d9 ^9 x) i4 Y; E! X5 i
count : process( clk ). v _8 m* c! F( x
begin- }/ K: l7 j( C7 Y: c$ l
if rising_edge( clk ) then; ^! U; T3 |: j) b3 k
count_t <= count_t + 1;1 ]2 ^& g: g' F# [% \) j, v
end if;0 }& b) b# R; Z% d
clkm <= count_t( 15 ); --scaning clock generaterd- D2 I. }1 Z0 r; P/ S$ @1 }# I
end process;
3 O( D5 v' @# W% ^$ X-----------------------------------------------$ I% C8 t B7 d% K2 m
keyboard: process( clkm, scan_cnt, key_pressed )3 ?0 @/ d o! s3 N% |
begin
4 y% i/ o2 X9 f--scan_cnt is a 4 bit up scan counter 4 W# x: j8 x: p" s6 W1 T
case scan_cnt( 3 downto 2 ) is --keypad column scaning, decoder functional ! B0 o7 y& q3 T
when "00" => col <= "0111";
2 p5 j! a8 g( [% m when "01" => col <= "1011";
# O+ ~/ q+ R6 F+ q- R when "10" => col <= "1101";
3 @9 x* A! Z2 q' \! ~5 E when "11" => col <= "1110";
8 S1 V A# N) E when others => col <= "1111";
6 a. \7 m9 z+ y T/ j end case;% G- F* J% g1 T4 X2 t- }
---------------------------------------------1 _. f& l4 b/ ?$ j1 E
case scan_cnt( 1 downto 0 ) is --keypad row scaning, mux functional) H+ V9 p: G7 z9 t% I; S/ L+ c
when "00" => key_pressed <= row(0);. T# Y# r: _! C- w( e% u
when "01" => key_pressed <= row(1);
+ R4 C0 z* G! A when "10" => key_pressed <= row(2);& B$ f. V' t. L4 b. ~& j, r6 [) v) A
when "11" => key_pressed <= row(3);$ i4 I6 j6 f. |+ S1 v
when others => null;
9 j: D2 W3 s1 q end case;
: w2 ?2 r/ U& Z* `----------------------------------------------------------------+ R8 u! @& ?# n! L0 J% \
if rising_edge( clkm ) then
" a7 d: m1 |; t: B# n; q1 ~ if key_pressed = '1' then --no key_pressed in, continue to scan
' g7 x+ X+ f: a6 j scan_cnt <= scan_cnt + 1;/ o# \' M; j. d! d' P" B. K* a) _$ ^
dbnq <= "1111111";% ^2 [& S. k1 d
elsif key_pressed = '0' then --key_pressed in
8 U. X* i2 Y8 k; B! v$ q: P dbnq <= dbnq - 1;: h8 x5 a1 ^* ]0 J6 D+ N
end if;# E; M' ~1 C# f. M3 F* a6 E
----- debounce & P' D/ o; Q% R+ u. P( X8 K
if dbnq = "1001111" then --key_pressed debounce
/ v& h- A, M, n0 v0 A key_valid <= '1';
& O% ^: C9 ?( _) A- g else key_valid <= '0';
' q7 Y8 F+ G3 f end if; ~* k: j9 w8 c; ]& s
end if;
6 E: z; F2 T: W% ?! h) o) _% y-- scan_out <= scan_cnt;
; O* W- G, z6 ? Z8 h i2 d0 r# N end process;
; |8 X$ h/ p- L5 Z0 K7 ^4 s% T---------------------------------------------------------------------------) u- j2 X7 x- m# K+ s2 ~
keycode : process( clk, scan_cnt, key_valid )
/ _7 R) P' w" g( U1 [ begin
3 g! l* d& p" F9 f6 f, u0 N------------- asynchronous expression; D) D" b+ \. D, P) e1 M, Y+ k
if key_valid'event and key_valid = '1' then" n0 J/ }( f$ s6 ^$ t/ W
if scan_cnt( 3 ) = '1' then7 R$ R" e8 A- {' B. M/ K" u
cmd_flag <= '1';, _0 a1 a* s+ V( g# d- v! f9 a; D/ K9 ?
cmd_buf <= scan( 2 downto 0 ); --command store into ram+ j9 l, _1 a p* P
elsif scan_cnt( 3 ) = '0' then- F$ i0 _% Z2 n7 `. E7 ^9 o
num_flag <= '1'; ! H0 F, G3 V5 }1 ~
num_buf <= scan( 2 downto 0 ); --numeric store into ram% p4 y4 l. ]& I
else num_buf <= "000", --numeric store into ram * q0 Y7 w; M; S$ |
cmd_buf <= "000"; --numeric store into ram
4 T* X! r% y$ _6 E0 }4 k num_fleg <= '0';
3 q6 D) T' M F7 y9 }- L% c cmd_fleg <= '0';; z" F6 M! q [) V7 W
end if;
8 N" r" c! P, M- H `2 I: A digit <= cmd_buf & num_buf; -- out of key controller9 Q: k& e) @8 ]* I1 _9 m
end if;* m6 P7 j2 Q; I- \5 B$ ]" r( q
end process;
& T5 X4 o6 t e* v-------------------
7 ]$ \2 l/ M1 F, Y4 s ?* f4 G' X5 k key_out <= key_valid;
: x) t- e9 O( x ` digit <= seg4bit;
3 A y6 s1 s* K) ]% uend behavioral; |
|