|
各位大大,想請問一下,我現在要做,當我鍵盤連續輸入,結果只取最後一個鍵盤輸入的值( ~" O7 I0 D [+ ]- f5 ^& x: K
) X$ v/ R) E6 d" h, B1 Z+ x這是一個自動控制鍵盤的控制器,我可以先輸入數字,然後再輸入指令,我的機器就會動那個指令n次
! m7 O/ Z1 z" C5 f/ F' L* Z1 R6 e7 G+ b" P
例如,先按5再按前進,自東車就會前進5次,如果只按前進,則自動車會自動前進1次(預設值)
% }; z4 S# i/ C" O9 s/ Y5 `! C ^0 M o
當我遇到連續輸入字數時,我不管要輸入多少數字,永遠取最後一個的數字當動作次數
/ o2 d( d d" {. B& y/ G7 d" Z& u# \2 s4 K* U9 `
現在我卡在不知道如何判別這種機制,希望有高手可以給我一點提示幫我解決,謝謝!7 L% H9 c* _0 `" Y$ u* p! O0 ~6 F( s
4 V6 H; |; I: `" g' t, r
library IEEE;$ [% f3 o# @: d2 ^: M% |( h1 ^7 r
use IEEE.STD_LOGIC_1164.ALL;5 Y# g! U6 t; O3 \( J. B0 z1 v$ c$ G
use IEEE.STD_LOGIC_ARITH.ALL;7 x* x" t1 q/ U. h. r) Q" d* Z9 t: ~
use IEEE.STD_LOGIC_UNSIGNED.ALL;
6 i2 ^0 B/ ?, @! u5 HENTITY key_controller IS
, \) c) g; r7 C8 L, `9 c2 \) T5 V; m PORT( 4 T" d- l2 z h( P9 A1 f
clk : IN std_logic;" b6 h4 D& A9 [. c8 E* D' w! e7 _
col : OUT std_logic_vector ( 3 DOWNTO 0 );$ I4 C8 s! x& g, R& ^
key_out : OUT std_logic;
% V' x Y' |% L row : IN std_logic_vector ( 3 DOWNTO 0 );
+ x+ j9 X4 M; Y digit : OUT std_logic_vector ( 5 DOWNTO 0 )
6 T) y5 K# T S1 m, x+ ]0 `. { );
0 P6 Q, N0 @4 j1 d+ X! vEND key_controller ;% ^" @' O" n) ], j; N& N- s
architecture behavioral of key_controller is
6 _& k; `+ G* z8 t9 Tsignal clkm : std_logic;
`- C* `1 q# xsignal key_pressed : std_logic;
. h- i8 t7 T' _9 ?signal key_valid : std_logic := '0';4 D/ N; i0 ?) O0 t
signal dbnq : std_logic_vector( 6 downto 0 ) := ( others => '0' ); --debounce
+ r# k# u3 d# n( C4 A8 t5 ssignal scan_cnt : std_logic_vector( 3 downto 0 ) := ( others => '0' );/ [- ?1 B ~* r
signal cmd_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
0 ^3 ^7 t: t1 E! x& Esignal num_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );, z1 d$ r, C- f% Y
begin: I# ?+ n5 C7 N) E0 ~9 A, T& @
count : process( clk )
) w( d, D/ c* z7 W, c, h! [# x begin1 c0 a+ w. z* U
if rising_edge( clk ) then
) Y8 H! I. l( X6 W ^ count_t <= count_t + 1;
$ K! S3 N) C6 y end if;
( c2 l0 D! V7 s clkm <= count_t( 15 ); --scaning clock generaterd. s- [% _, I3 ~0 ?& p5 K( O
end process; / y: h6 n5 n6 x. R% `
-----------------------------------------------8 p$ Z0 U- c! C9 j' k4 L4 z6 z: s3 L# y
keyboard: process( clkm, scan_cnt, key_pressed )5 a8 s2 W& H2 Q4 e0 G
begin9 z( w: B5 h: ]% {' s
--scan_cnt is a 4 bit up scan counter
$ K0 |% m% e) Y+ p0 L. t1 X/ H case scan_cnt( 3 downto 2 ) is --keypad column scaning, decoder functional ; D! P/ ~4 `; U9 p& Q+ t
when "00" => col <= "0111";
5 Y# l. f) w, U& x5 Z. x; T when "01" => col <= "1011";
3 v/ K3 T7 w# R* {* ] when "10" => col <= "1101";
! H) x: D8 d7 P+ f0 s3 B+ p% p when "11" => col <= "1110";; E2 U/ ?$ z$ E' d O/ s* r, [
when others => col <= "1111";
4 r8 L( E: @# z- i end case;+ i8 H- V7 l7 u U6 V( n
---------------------------------------------
2 z2 H% w& G2 R; C2 O* H case scan_cnt( 1 downto 0 ) is --keypad row scaning, mux functional6 P+ ~) i( q I, I
when "00" => key_pressed <= row(0);& j( q$ y9 R0 F7 M. p( O. ]9 K
when "01" => key_pressed <= row(1);
9 |7 a) [4 A; w$ Y% e( \ when "10" => key_pressed <= row(2);* B R) L( x3 u; i& U- ]- F
when "11" => key_pressed <= row(3);% q8 A3 t4 B6 K6 i# v" v
when others => null;
- b& Z v/ F& t, Y5 L3 [; K1 L end case;
; q- t; n# ?# [3 v----------------------------------------------------------------- l. }4 D. C* G3 ~/ }& C6 U
if rising_edge( clkm ) then
( u2 B8 T* h. R* P if key_pressed = '1' then --no key_pressed in, continue to scan
( s. R, F ]5 f9 F scan_cnt <= scan_cnt + 1;- {. W& @; m: }# p) M5 m
dbnq <= "1111111";
m4 c) _+ p: U/ t$ ]* c! i( F elsif key_pressed = '0' then --key_pressed in
C+ x2 S* P/ [, r8 {1 x* v# e j dbnq <= dbnq - 1;0 M4 n6 [. `" a5 k. f
end if;
" u% z6 q1 B2 T2 n----- debounce : v2 F$ X3 y, `! F% r$ C( ^5 L* m
if dbnq = "1001111" then --key_pressed debounce+ U. @2 `1 c3 F3 w+ O: |" G
key_valid <= '1'; 1 V* k1 l' F( P
else key_valid <= '0';. K$ r' h U7 U' N, b
end if;
7 I R5 c. O3 H h& P, x5 W end if;/ ?/ W& I: B+ J9 n8 k
-- scan_out <= scan_cnt;5 V C" L9 ~/ J5 }! ?% q" D5 d' n
end process;% O$ T# [; n7 a7 G+ F* N& D
--------------------------------------------------------------------------- _) Y' V$ [. B! F$ K
keycode : process( clk, scan_cnt, key_valid )
: M8 n! Y4 \/ c3 h7 @ begin2 Q/ b) [4 n; _7 \- j% Y9 Y
------------- asynchronous expression
: f/ I% Z7 q5 K1 e1 G if key_valid'event and key_valid = '1' then# b; _0 d; U! x0 U* i
if scan_cnt( 3 ) = '1' then
4 ?3 }/ [0 m( P% z cmd_flag <= '1';3 U' p5 D3 J) {
cmd_buf <= scan( 2 downto 0 ); --command store into ram
, H0 o+ w+ p- C/ t* W" h, C elsif scan_cnt( 3 ) = '0' then5 \. [5 N" M7 K& S* x4 x
num_flag <= '1'; 4 H' z$ P% O$ n% x
num_buf <= scan( 2 downto 0 ); --numeric store into ram
) b* ~& \+ Y8 x7 u else num_buf <= "000", --numeric store into ram
/ n: z0 D- v6 I2 M. q cmd_buf <= "000"; --numeric store into ram: x! d6 S9 g+ m. o) u+ G S
num_fleg <= '0';
5 M. Y3 t2 Z+ C0 L( D | cmd_fleg <= '0';( L$ o, S# g* H& n/ S: n# v; b
end if;3 Y; o+ ?) q U, x/ \+ f7 B
digit <= cmd_buf & num_buf; -- out of key controller+ H+ Q% l! Z( b2 Y$ o
end if; M2 o. k4 u' _. J9 Q: N/ C
end process; ' W4 M/ s/ B, @: R Z2 T
-------------------
2 L z( `5 F2 f9 m5 p. ? key_out <= key_valid;6 C9 @* S! l) z% m8 ~5 N
digit <= seg4bit;7 Y! j e* ~6 C, A3 y
end behavioral; |
|