|
各位大大,想請問一下,我現在要做,當我鍵盤連續輸入,結果只取最後一個鍵盤輸入的值+ V2 X3 e3 ?3 D+ @7 `$ r7 S
1 l7 P- A1 R# H' a |這是一個自動控制鍵盤的控制器,我可以先輸入數字,然後再輸入指令,我的機器就會動那個指令n次. P- `3 \( Q' |+ d
* Q) w6 z) r; [# E0 T4 O4 w例如,先按5再按前進,自東車就會前進5次,如果只按前進,則自動車會自動前進1次(預設值)3 H: F1 q, e% o3 e/ h
7 _7 \6 W) f( O" [' A E' m( H當我遇到連續輸入字數時,我不管要輸入多少數字,永遠取最後一個的數字當動作次數9 w; s2 V6 T2 U$ e! e0 m" A4 [
. l4 {; Z$ `# {8 A/ j現在我卡在不知道如何判別這種機制,希望有高手可以給我一點提示幫我解決,謝謝!0 C! Q% ]) N( i6 r4 D3 p% @7 i) N& [
4 Z" V! X9 g! v- C1 E5 l! d$ j
library IEEE;. S, g! O/ U- K/ Z x" `
use IEEE.STD_LOGIC_1164.ALL;) _; c$ S2 a# x
use IEEE.STD_LOGIC_ARITH.ALL;+ q: W0 O" O0 _
use IEEE.STD_LOGIC_UNSIGNED.ALL;8 @" I, Z& L4 {' s% b7 I
ENTITY key_controller IS$ @! g2 ?- I7 V0 K2 {
PORT( . r1 q4 C( r+ x' `1 }
clk : IN std_logic;
7 Y q1 ^) F& \1 l* Z( Y& u1 A6 Z col : OUT std_logic_vector ( 3 DOWNTO 0 );! E# a L/ v# p' b
key_out : OUT std_logic;1 Z3 J1 {) w7 o# \/ T: l. A7 R3 m
row : IN std_logic_vector ( 3 DOWNTO 0 );
* D4 b T% X2 d digit : OUT std_logic_vector ( 5 DOWNTO 0 )8 \+ b! S1 _. q* F% j4 R6 L' f
);
, z6 Y6 G1 d+ {1 L2 h: O1 \END key_controller ;! f* i- _% ?# V8 w% a b1 ]6 d6 Q
architecture behavioral of key_controller is
# H3 g; i2 r9 w" X- bsignal clkm : std_logic;
1 o9 ?+ N$ I8 a3 x! Z1 Xsignal key_pressed : std_logic;* J N2 Y) w: D S/ z+ U9 p, f% t
signal key_valid : std_logic := '0';" W- f8 M, l' V5 Q( B
signal dbnq : std_logic_vector( 6 downto 0 ) := ( others => '0' ); --debounce
0 y& x, G. D+ m( W+ [- Tsignal scan_cnt : std_logic_vector( 3 downto 0 ) := ( others => '0' );) [3 ~. C4 L, C `- o: S0 `
signal cmd_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );( }. O S7 K. p; I: r9 c7 e
signal num_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
/ d! a6 W2 q$ F3 k4 Nbegin) g( X5 \: ]- O3 D! W1 M* y! I
count : process( clk ). [+ D4 e3 ]+ U/ y
begin
, v8 c+ h K: V3 d" Y if rising_edge( clk ) then2 }& W* {! h2 y
count_t <= count_t + 1;
+ }5 |+ a5 p# }9 w( n* V end if;( P- j" L/ A( J
clkm <= count_t( 15 ); --scaning clock generaterd0 n/ ]# D# y) A% c4 y( N
end process;
; D: ?$ f7 _3 \2 j I-----------------------------------------------
6 e' A3 R' ~: a( t5 ~1 P. Kkeyboard: process( clkm, scan_cnt, key_pressed )8 l8 g8 y1 Y1 _2 U$ ^" a
begin8 Y/ B5 i$ P8 A: ?9 p
--scan_cnt is a 4 bit up scan counter
- V ?( v1 J: B/ c1 I: Z case scan_cnt( 3 downto 2 ) is --keypad column scaning, decoder functional
1 w+ x& `. a+ N# M when "00" => col <= "0111";
8 O& Y' [; k9 |& P T/ Z @ when "01" => col <= "1011";
& s3 ~3 X) s: Q% g; r when "10" => col <= "1101";& M& c9 T- v1 n# Y9 j
when "11" => col <= "1110"; j8 i- a* E) @2 w1 d( M% S
when others => col <= "1111";
x* X. M4 m; g5 n* |5 m0 K end case;
# D( y4 w8 Q+ ?, u( ? ---------------------------------------------0 X+ d& \. ~+ B& [
case scan_cnt( 1 downto 0 ) is --keypad row scaning, mux functional
. k" h1 Z2 j9 ]. K; Z when "00" => key_pressed <= row(0);
" z4 f3 U/ p8 z2 G& _ when "01" => key_pressed <= row(1);
( `* D {% Z+ H+ U8 k: g when "10" => key_pressed <= row(2);
5 l3 V3 g1 l r8 t when "11" => key_pressed <= row(3);. f4 Y8 n" B" n0 v& k& B
when others => null;5 z3 k, L. U$ O0 [' X! m
end case;
7 u& j7 M( L8 |3 V: v9 k----------------------------------------------------------------
9 R X- u' t9 `: g5 O! H' t3 ` if rising_edge( clkm ) then
/ p$ l" e0 O. t+ \0 L if key_pressed = '1' then --no key_pressed in, continue to scan6 L4 b: B( e# L) v% x
scan_cnt <= scan_cnt + 1;( z" K& L* f* K% \2 C. J
dbnq <= "1111111";
3 G) }; I: B' J+ Y elsif key_pressed = '0' then --key_pressed in1 _3 \( b7 s. P& _2 @5 k
dbnq <= dbnq - 1;4 D! A$ }0 C/ C. ^3 J( m
end if;
1 P* b2 x( f2 b S----- debounce
/ m/ A: H: B: D9 i( m0 p( L if dbnq = "1001111" then --key_pressed debounce' G. v8 Z/ C% O& O# z
key_valid <= '1';
Y: K# ?& C. J# x7 R1 f( Q$ r else key_valid <= '0';
% ]5 m4 d3 W( P1 S$ K end if;
) [! E' F0 q0 B' B* C& g# D/ d, x end if;
T2 C* n) R6 Y% Q5 k2 a5 X) z-- scan_out <= scan_cnt;1 M V1 u; I" P/ n
end process;
( x% z( Z2 p/ |9 G, A1 [- V& g4 }---------------------------------------------------------------------------$ b) d* m6 i% ~8 _- a/ F
keycode : process( clk, scan_cnt, key_valid )
6 S% V7 n) W' J; @4 u3 {4 R begin
, d& R6 C) v. t" c" ?' t( S------------- asynchronous expression# A( [. C+ b- _+ m \4 J* i
if key_valid'event and key_valid = '1' then
$ k. C( O, b2 ?% V4 }! F if scan_cnt( 3 ) = '1' then
; W! o( M3 O3 Y7 E7 s+ f( G3 w! n1 D cmd_flag <= '1';
0 A1 f7 b; [* m cmd_buf <= scan( 2 downto 0 ); --command store into ram
; O$ N! q; |' Z- A elsif scan_cnt( 3 ) = '0' then; c- X( n8 L+ W/ U2 p5 E$ h
num_flag <= '1'; # e3 |3 m8 J7 c
num_buf <= scan( 2 downto 0 ); --numeric store into ram5 ^0 x+ c5 G" ?6 @1 r* s7 F
else num_buf <= "000", --numeric store into ram 1 F7 f8 @$ G; s+ g* G1 l L. P# e
cmd_buf <= "000"; --numeric store into ram. L T2 k1 n# x' a, b# j* D/ F
num_fleg <= '0';3 w# `) T+ u' i z
cmd_fleg <= '0';
' m: J! }6 q8 L5 d7 ^ end if;
; p7 O6 \* }5 R& A4 d; p digit <= cmd_buf & num_buf; -- out of key controller( z! | j0 w/ F* Z5 h
end if;
( T# e2 K8 `; d' ` end process; ) {# W1 N$ P0 q* ~$ W
-------------------
/ G e/ W1 |6 f" Y2 V key_out <= key_valid;
" N2 _9 C/ D. w% p. L' \ digit <= seg4bit;( I/ P2 J1 V$ T) O4 N* L
end behavioral; |
|