|
各位大大,想請問一下,我現在要做,當我鍵盤連續輸入,結果只取最後一個鍵盤輸入的值
* y3 m# F! w7 Y8 ~6 \" I2 d# X8 J1 |; ^( `
這是一個自動控制鍵盤的控制器,我可以先輸入數字,然後再輸入指令,我的機器就會動那個指令n次
0 V7 I% k& m" ~( p7 N" b3 t
9 M1 A' e) g' n1 l+ ~例如,先按5再按前進,自東車就會前進5次,如果只按前進,則自動車會自動前進1次(預設值); g( q* @6 b6 {6 {
}' N* i9 H- y$ Q! f, v$ w( y當我遇到連續輸入字數時,我不管要輸入多少數字,永遠取最後一個的數字當動作次數3 r; B) L$ A7 s V1 w/ o9 s
6 z- {9 a) T0 g- @% l
現在我卡在不知道如何判別這種機制,希望有高手可以給我一點提示幫我解決,謝謝!6 ?, B' S. K8 H! j
3 X8 j/ M7 {9 q8 F* qlibrary IEEE;
# b# o- g' Q8 huse IEEE.STD_LOGIC_1164.ALL;
* h' g9 V: m- X4 a/ i1 W6 q9 V- \' q+ Fuse IEEE.STD_LOGIC_ARITH.ALL;4 L0 S0 z5 E+ X: r& m. v
use IEEE.STD_LOGIC_UNSIGNED.ALL;
) J$ Q8 N1 z, _% D( `' W8 ?ENTITY key_controller IS8 T0 R8 S8 V9 h# K: f0 s' k( r( B
PORT( 7 \. ]+ N" V/ b( A) d4 G! M
clk : IN std_logic;
/ j) o; e6 W; E- c& V: V. C( e col : OUT std_logic_vector ( 3 DOWNTO 0 );* X, T" _+ n2 @0 |8 c# E: \
key_out : OUT std_logic;) p& U8 M$ ~4 W2 b! g
row : IN std_logic_vector ( 3 DOWNTO 0 );
8 l# w! G2 |# K( Y) M# _4 ] digit : OUT std_logic_vector ( 5 DOWNTO 0 )7 z F/ a( f/ @: l
);
1 b/ @" k5 A: Z0 N- P4 q; l/ B) I& C; H5 cEND key_controller ; ~8 h, O" N; W. J- {
architecture behavioral of key_controller is
; F5 G+ w# @! V, }3 _/ N tsignal clkm : std_logic;, q3 i {/ S p% }* H5 w! R
signal key_pressed : std_logic;1 ?+ h& t1 e4 F
signal key_valid : std_logic := '0';( S: W8 J9 |6 B3 i. y
signal dbnq : std_logic_vector( 6 downto 0 ) := ( others => '0' ); --debounce% W% B- F' `7 G% l$ M1 Y# [2 J% }7 t) }
signal scan_cnt : std_logic_vector( 3 downto 0 ) := ( others => '0' );' L& U( x2 T+ Z
signal cmd_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
% c' v( _0 `* l& z d. ~5 m) wsignal num_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );6 {0 H! |* {9 `' k
begin
# \5 e& w3 z2 t% Tcount : process( clk )8 r: A! ~$ \2 M/ H: z
begin
, g( `8 X. H6 l6 [: V1 O7 ] if rising_edge( clk ) then
# V9 h. O& J3 S1 v8 c3 N count_t <= count_t + 1;$ ?2 e x ?! G1 O# v/ c. k
end if;$ E# P1 ?5 v' t% X
clkm <= count_t( 15 ); --scaning clock generaterd$ Y% C+ U! }. k Y5 S0 P( b' y5 p
end process; 3 z; h: j+ t. }: j; @
-----------------------------------------------
, y2 C0 B% w1 I" @# p) }keyboard: process( clkm, scan_cnt, key_pressed ): e q! X2 \6 m9 Z+ c+ ^
begin
) D$ N3 |3 X! o- C2 e--scan_cnt is a 4 bit up scan counter
0 {+ A9 X+ }" M, L case scan_cnt( 3 downto 2 ) is --keypad column scaning, decoder functional
5 o' a8 D% f! g when "00" => col <= "0111";9 |* `# ]+ Y8 H/ Y4 q( n# }
when "01" => col <= "1011";
7 R4 E! t8 |0 E% P% k r$ w; A when "10" => col <= "1101";/ N% a1 [% i. U2 _% n
when "11" => col <= "1110";9 \) ~; O& Y* L- m y: ], B
when others => col <= "1111";
& `& h! w+ p' p9 ^ end case;+ L6 f- h" J) W+ }6 D- p
---------------------------------------------9 K O3 @9 R4 h- {) A9 g& t# G
case scan_cnt( 1 downto 0 ) is --keypad row scaning, mux functional4 b' \( l$ s# z1 E
when "00" => key_pressed <= row(0);
/ _6 {" `$ g- d4 ]7 T7 q$ n5 }2 E) @; V when "01" => key_pressed <= row(1);
* n% n% Y: j6 l* y6 h when "10" => key_pressed <= row(2);' J1 A1 O1 z# h: b
when "11" => key_pressed <= row(3);5 j" e+ c7 j- T, |2 C; J
when others => null;9 W* o% ?; l* w1 x: ~1 I
end case;9 k5 s% s9 ?9 M& I6 [1 G& M, x
----------------------------------------------------------------$ @; @4 i% M2 _
if rising_edge( clkm ) then 0 V7 p) K" i! N" l5 ^
if key_pressed = '1' then --no key_pressed in, continue to scan* o Z& E# {/ ^0 e
scan_cnt <= scan_cnt + 1;
' \2 Z# E/ p/ i; t# u$ L dbnq <= "1111111";/ j. Z5 C9 H' d
elsif key_pressed = '0' then --key_pressed in3 X+ N. v, a l
dbnq <= dbnq - 1;
9 k; x1 @) m ?, }5 E/ {+ h end if;
% B% ]# [/ \' @ _----- debounce - ~( u. K; |! c9 [
if dbnq = "1001111" then --key_pressed debounce" I4 W- Y: M+ A# X: Y
key_valid <= '1'; / ~2 c# j3 C4 r5 ]' K6 c
else key_valid <= '0';
0 V4 l( W/ b& F1 m6 ^" ?7 J) ?, B' |. X end if; / R3 e- }9 X1 W4 }
end if;
+ C5 ^% ^+ A: q-- scan_out <= scan_cnt;# n% S) n E# u/ M. U
end process;
! [( K1 [$ Z q2 r---------------------------------------------------------------------------) w) x9 f* Q2 h5 v
keycode : process( clk, scan_cnt, key_valid )
: c* {1 s5 Z, Z6 a) l8 k9 d, p: Q* g begin6 t2 L0 i' R- ?2 q) B
------------- asynchronous expression
% Q3 K. _5 J4 {& [( \" U if key_valid'event and key_valid = '1' then& e" X5 m& ~/ ]
if scan_cnt( 3 ) = '1' then7 Q% r, x# X$ ]
cmd_flag <= '1';1 M5 Q9 R6 O: a; z1 Y; {- L( p
cmd_buf <= scan( 2 downto 0 ); --command store into ram
, r; Z& |3 K' i4 n elsif scan_cnt( 3 ) = '0' then
5 Q1 F7 p A6 Z/ N) ~8 q- R r num_flag <= '1';
- M- i5 \; U1 p C5 I num_buf <= scan( 2 downto 0 ); --numeric store into ram; w1 L, Y; O9 y7 _% K4 W
else num_buf <= "000", --numeric store into ram
( H6 P3 \$ `: V cmd_buf <= "000"; --numeric store into ram
9 v( E' a2 V; Z0 b' Y0 b' l0 _8 ?, _) a num_fleg <= '0';3 B; `7 ^1 Q, A
cmd_fleg <= '0';& ?, _- z, L3 s9 T% v2 x
end if;
0 m' V1 h4 L: f2 B. K digit <= cmd_buf & num_buf; -- out of key controller8 U# l5 v0 t4 Q$ J" }% K, ^
end if;
$ |# Q A" m; V4 A end process; y8 M3 l. N. f
-------------------
# M* l& T* u# l3 W, j, t8 c key_out <= key_valid;
3 E( G* b3 ^* x; {; ?* P digit <= seg4bit;0 G. \' a/ u6 o1 ]
end behavioral; |
|