|
各位大大,想請問一下,我現在要做,當我鍵盤連續輸入,結果只取最後一個鍵盤輸入的值% T6 h! k6 a% u! Z
9 T* R8 y1 ?- w% P/ R這是一個自動控制鍵盤的控制器,我可以先輸入數字,然後再輸入指令,我的機器就會動那個指令n次
* p. X! x: k% N% t& z. p. u! M
" F3 T$ h- ?1 l例如,先按5再按前進,自東車就會前進5次,如果只按前進,則自動車會自動前進1次(預設值)
2 s$ \" ~! W: q1 j% j! c6 {7 i* v( u( w( w% X% g1 M# z
當我遇到連續輸入字數時,我不管要輸入多少數字,永遠取最後一個的數字當動作次數
w/ J% V; O& q1 |8 _3 l3 {0 V) ]0 J4 d3 a. a1 s& i# s
現在我卡在不知道如何判別這種機制,希望有高手可以給我一點提示幫我解決,謝謝!. y6 y- y% m& |% r: N9 r5 e
5 ^& f3 z% \4 _. t; [0 rlibrary IEEE;+ T( _' o9 x7 ^) g5 T1 w
use IEEE.STD_LOGIC_1164.ALL;' E X0 b# X$ T8 K; W) o0 V
use IEEE.STD_LOGIC_ARITH.ALL;
# e& \6 O3 t: |use IEEE.STD_LOGIC_UNSIGNED.ALL;1 Q- f. e. ]' L! [- z" @& c" H
ENTITY key_controller IS! m* d2 f8 ]) i1 z
PORT( * l# @- H& X/ T% T1 q5 k
clk : IN std_logic;
; r5 v- j% [; J# R O$ X col : OUT std_logic_vector ( 3 DOWNTO 0 );6 e. R1 F& C0 n" Q
key_out : OUT std_logic;- h) k' o) X* J7 k8 }. h$ T
row : IN std_logic_vector ( 3 DOWNTO 0 );
. {7 t% v+ E4 b* o2 E digit : OUT std_logic_vector ( 5 DOWNTO 0 )
) l9 A! W' ]# y0 g) w: a% n );
5 [& B- S: _8 z- |END key_controller ;
& H! Y9 h% I' ?+ ]3 carchitecture behavioral of key_controller is/ U; G4 H+ b3 P! p; n" }. G9 U
signal clkm : std_logic;4 G/ }$ T; l; S) y, d+ X
signal key_pressed : std_logic;0 d& D a* g9 G/ {* I7 Q; {+ G
signal key_valid : std_logic := '0';
) g' H$ d4 R& o; ?/ wsignal dbnq : std_logic_vector( 6 downto 0 ) := ( others => '0' ); --debounce' P! m7 u( `& d" Q3 u) s' Z3 q
signal scan_cnt : std_logic_vector( 3 downto 0 ) := ( others => '0' );
) W _8 S/ U' y8 Y" Ksignal cmd_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );3 c+ k( i9 n1 U$ }, k
signal num_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
+ n3 h" f. Y2 W6 @" K9 pbegin
) O! R5 z- W5 h1 H1 E7 n! Wcount : process( clk )$ A- I- \* {0 z3 u: u3 X
begin/ ^2 ?0 N6 [" C: `* d- X: c! i
if rising_edge( clk ) then0 X0 {+ O' }- T8 t* y$ G
count_t <= count_t + 1;8 H+ r- c, v* [
end if;
3 i) ~: l, L f8 Q \7 u% B+ S% O' F+ R clkm <= count_t( 15 ); --scaning clock generaterd
# t: ~) j& b0 ^0 T; x- Z; @ end process; : x3 J, D( ?9 v* h# f
-----------------------------------------------0 L; g( t' n8 H, Y8 O4 k5 P
keyboard: process( clkm, scan_cnt, key_pressed )
' \# L* T( u( c! cbegin
$ r1 R d' _, t% q5 D4 Q5 Y" R--scan_cnt is a 4 bit up scan counter
0 c8 l' q% y. _: V/ k1 N case scan_cnt( 3 downto 2 ) is --keypad column scaning, decoder functional
5 D& a3 W0 G' g/ }+ A' Z when "00" => col <= "0111";! x5 l2 h' ~9 m) h8 ^: b
when "01" => col <= "1011";
' }& B3 I: z& O/ R9 b; t when "10" => col <= "1101";
4 V2 j) {" ~) L+ U when "11" => col <= "1110";
- U+ }; z: y- O. b when others => col <= "1111";
; |& V/ K/ o' Q' f: u$ i, i end case;" H e X# P o/ G& |
---------------------------------------------& w4 z* @2 v L( t8 i
case scan_cnt( 1 downto 0 ) is --keypad row scaning, mux functional
& y4 L5 Z" ~9 b7 ?% | ~ when "00" => key_pressed <= row(0);
# l; b% M' Z2 A9 b; b9 i% } when "01" => key_pressed <= row(1);5 Z" t3 H6 `1 E, D
when "10" => key_pressed <= row(2);# |- L& F' m9 a* \% y' x
when "11" => key_pressed <= row(3);2 A1 A" Y x. `$ G7 w7 ~8 k
when others => null;
1 Q; f0 a% A0 z& ? end case;
4 f! j/ j) p# S0 R9 b0 y4 y----------------------------------------------------------------" A2 {& {$ F$ g4 M6 H
if rising_edge( clkm ) then
$ g9 ]$ B: _3 r& m! A5 W' M+ A if key_pressed = '1' then --no key_pressed in, continue to scan; P5 e3 ~) }4 p! i5 E
scan_cnt <= scan_cnt + 1;1 z* k- N3 ^" }/ H
dbnq <= "1111111";
" r% ^1 m% s0 b0 J. N elsif key_pressed = '0' then --key_pressed in; u" M# d" n; {0 R0 e7 C; Q
dbnq <= dbnq - 1;$ A8 C, [% X% q4 r% H( M7 z
end if;
$ n8 X3 B0 S5 U( ]6 ?9 x9 ?----- debounce
2 r% j9 w. K8 F: ^. t if dbnq = "1001111" then --key_pressed debounce- }0 r! k- u! l* q% a8 b1 v
key_valid <= '1'; 4 T/ I4 y) y. C5 H
else key_valid <= '0';
( I4 O6 A* p; n( N end if; ; l- ^9 t7 q- r! f* d2 G! X
end if;
: T* F9 y5 l5 [. K+ \-- scan_out <= scan_cnt;: D! w5 n) U9 d0 u6 H2 K
end process;1 [1 O; F. @( J6 a
---------------------------------------------------------------------------
3 J3 {2 j z( ?" f/ T) ^keycode : process( clk, scan_cnt, key_valid )
* T! V7 W% n E4 C" a% a" i begin9 r/ e& B3 z. i2 U! ~, \
------------- asynchronous expression
1 l; K. V; d" |) O$ ]# f if key_valid'event and key_valid = '1' then
' l% M- B6 d7 r* d* p if scan_cnt( 3 ) = '1' then2 T# X0 ]* P9 E: [5 }0 e, C. x4 @
cmd_flag <= '1';7 l+ U, e3 G* L
cmd_buf <= scan( 2 downto 0 ); --command store into ram+ J6 y, m! W2 R6 U6 g
elsif scan_cnt( 3 ) = '0' then
4 l% @1 {/ X/ \ num_flag <= '1'; & Q8 v- \# | j% ]# |! B4 w/ W6 j5 z
num_buf <= scan( 2 downto 0 ); --numeric store into ram
) Q) D: c( T. m7 F else num_buf <= "000", --numeric store into ram u. _- I- U }! s
cmd_buf <= "000"; --numeric store into ram
h5 D! N: l3 g' L1 ?; V" j num_fleg <= '0';
" f6 R5 `& o1 Z% ?2 W; N; h cmd_fleg <= '0';9 K3 c3 E" f! z
end if;
2 {4 r* t& U Z4 e digit <= cmd_buf & num_buf; -- out of key controller2 K& t* q( H& k" Y2 V$ r- U
end if;# `& C$ I# n2 C) b4 F
end process;
5 O4 m0 C _7 |5 ~1 ^: w-------------------
) Q) J! Y0 F: G8 }" p, X3 C+ H key_out <= key_valid;& x9 ^! T3 a# A1 `( r3 \
digit <= seg4bit;1 e4 V, N2 e% X6 A% }; _# Y; ]
end behavioral; |
|