|
各位大大,想請問一下,我現在要做,當我鍵盤連續輸入,結果只取最後一個鍵盤輸入的值: J8 W8 O0 D- A! B7 D9 n8 S
0 W( f! F2 S2 s! b1 R3 j- ?0 O2 M
這是一個自動控制鍵盤的控制器,我可以先輸入數字,然後再輸入指令,我的機器就會動那個指令n次
: w) [6 _& F# j: O4 k+ o4 M0 b, h/ w" \# m. k
例如,先按5再按前進,自東車就會前進5次,如果只按前進,則自動車會自動前進1次(預設值)9 c, I# X4 `, k; s" @2 L% H
, y: K/ R/ D6 N" {( v! @2 ]3 F( r當我遇到連續輸入字數時,我不管要輸入多少數字,永遠取最後一個的數字當動作次數
2 M& h# l0 y' e
" a4 L7 L* E# A9 t現在我卡在不知道如何判別這種機制,希望有高手可以給我一點提示幫我解決,謝謝! y- v. C& K& X# I( x& M0 m
! h) x/ }9 C) h+ _) P6 n3 |. T% z9 m
library IEEE;
6 y7 R3 [1 k& r* @9 W5 luse IEEE.STD_LOGIC_1164.ALL;
8 F' K+ y, I o& i7 l3 Cuse IEEE.STD_LOGIC_ARITH.ALL;; |( z& x0 `3 |( b- u( y) U0 I7 U7 W0 a
use IEEE.STD_LOGIC_UNSIGNED.ALL;
' `4 |7 L; u4 a D8 Y ^; U. zENTITY key_controller IS7 I5 e% P& r! _9 J g6 g4 Q, a
PORT(
+ [) L% j, i% d! U$ T; V: G5 D4 P clk : IN std_logic;
# W3 b2 O. N' F8 j col : OUT std_logic_vector ( 3 DOWNTO 0 );
" s. T) s+ d3 l key_out : OUT std_logic;0 `% b% o. |) f* ^% n
row : IN std_logic_vector ( 3 DOWNTO 0 );
7 ~# {8 n0 w# X- ~/ C# T digit : OUT std_logic_vector ( 5 DOWNTO 0 )
; y; x5 T Y2 y; _; N );% M. B* D2 s3 v- L: a, l
END key_controller ;) g" t1 o/ v" l" ^ _- e
architecture behavioral of key_controller is5 _& w- P5 ?* B8 B8 p$ x
signal clkm : std_logic;
9 \: A9 G# }9 Q, ksignal key_pressed : std_logic;) W- B+ N! R) r, l) a7 _+ Q8 \
signal key_valid : std_logic := '0';
( G u7 Y# T' z# G9 A+ b+ B: q8 ssignal dbnq : std_logic_vector( 6 downto 0 ) := ( others => '0' ); --debounce5 g; O; d2 t5 K9 V3 T
signal scan_cnt : std_logic_vector( 3 downto 0 ) := ( others => '0' );
- m. b7 A1 E' B" K8 d; Isignal cmd_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
- }) R9 D+ R* t& A+ Csignal num_buf : std_logic_vector( 2 downto 0 ) := ( others => '0' );
, N' a. G! [2 x, u: a7 A8 P4 A, Ybegin
8 }5 X) [' n3 e) O4 t3 d$ _2 y$ p1 mcount : process( clk )0 M6 N: A+ p' Z* E, H$ Y
begin) J6 q$ ~3 i$ o
if rising_edge( clk ) then5 t( p+ X% w9 I5 T7 R! a+ X
count_t <= count_t + 1;
4 {: C3 C' t w2 l9 J end if;4 c6 ]: l$ T! y! O
clkm <= count_t( 15 ); --scaning clock generaterd$ d- D. w0 s7 }, f6 N
end process; 0 c8 K+ x8 w& M, y6 Y! I
-----------------------------------------------
[7 O2 u3 P( Z& ?7 C& Z: bkeyboard: process( clkm, scan_cnt, key_pressed )
- ]* j- h* c( Bbegin3 U, Q% M; F/ x- y/ w
--scan_cnt is a 4 bit up scan counter
! P( i9 ?+ C' T2 e0 D case scan_cnt( 3 downto 2 ) is --keypad column scaning, decoder functional # M3 U( y: {9 R0 v5 H" U8 R
when "00" => col <= "0111";5 X) N$ |5 ^6 S$ e
when "01" => col <= "1011";9 t! i: K/ i) L) m
when "10" => col <= "1101";
8 z- H8 Z3 R I" a8 G5 E$ D. E when "11" => col <= "1110";* z3 U& ^ O1 C% T$ U
when others => col <= "1111"; ; K. g. s% S! V9 A2 p9 c) s* ~
end case;
7 j( ]# {& F, z+ I2 ]& c4 X! z ---------------------------------------------
6 R5 P3 J2 k) k; b- Q case scan_cnt( 1 downto 0 ) is --keypad row scaning, mux functional$ j6 n8 ~- A6 _1 a
when "00" => key_pressed <= row(0);
* i& Y! f. b4 L6 Q6 J" N( [ when "01" => key_pressed <= row(1);) L" v: b- C1 t1 F# v1 q5 q2 ]" u
when "10" => key_pressed <= row(2);
/ x2 a& g1 u( u1 z) ~1 i6 T. u when "11" => key_pressed <= row(3); K6 r5 r7 F* h5 N ^- k! _3 s7 F
when others => null;
# y; d. x/ p% `- p# t$ Q5 q end case;) A/ n5 ?- ` R& x$ Z; c
----------------------------------------------------------------" |$ U$ G& ~! g, d! [& p' t; f, B- V1 t
if rising_edge( clkm ) then 9 Y! y4 W( K9 j5 g8 ^% d; L: Z
if key_pressed = '1' then --no key_pressed in, continue to scan% ~% J! S1 c- E0 d
scan_cnt <= scan_cnt + 1;
3 {* ?1 y; O w% j5 g, Y dbnq <= "1111111";
! v4 Y: E+ T: I; y2 k* n elsif key_pressed = '0' then --key_pressed in$ U G+ ?# E. f1 M) F, ~1 s
dbnq <= dbnq - 1;
% \" [% y% f( M8 h, c end if;6 Q) L/ W b3 T t8 e
----- debounce
' V# T, Y0 y) y if dbnq = "1001111" then --key_pressed debounce
[( Y' V9 [1 j6 j; n# V! w key_valid <= '1'; 9 o5 L& o" B$ u J, ?: g; f
else key_valid <= '0';
/ a% W( s6 Q' I2 l" ] end if;
. z* s* `. A' h3 j" P" S! ~& ^0 H7 | end if;8 u9 N/ _4 ^! S r' |
-- scan_out <= scan_cnt;
5 T1 f5 O9 v7 S end process;4 j t' \" L7 L0 w% Q2 J
---------------------------------------------------------------------------
+ d+ R6 Q' ^; O. r0 V" O- ~1 X# ukeycode : process( clk, scan_cnt, key_valid )9 E; s7 v$ k9 X' s* s$ C" l7 P( F
begin1 m- [2 f8 M" v0 t4 P
------------- asynchronous expression
/ C0 w) B# c5 M& z if key_valid'event and key_valid = '1' then8 O7 Z! h! f2 b0 e1 ?) X9 P% _
if scan_cnt( 3 ) = '1' then
6 |3 A# M, O* [& j5 ? cmd_flag <= '1';
7 H2 t1 z+ T2 y% I- W5 I5 q3 B cmd_buf <= scan( 2 downto 0 ); --command store into ram" ]+ x! k1 g9 e1 e. S t/ }2 G) t
elsif scan_cnt( 3 ) = '0' then" x+ d# L9 u+ i: o7 v
num_flag <= '1'; 8 Z2 ~! B6 p' K* c1 T& G# W& D
num_buf <= scan( 2 downto 0 ); --numeric store into ram! }& i& Q# M5 T4 {
else num_buf <= "000", --numeric store into ram 6 ?% {! e/ O) ^
cmd_buf <= "000"; --numeric store into ram% ~' z9 S! B0 ^+ m, S5 M
num_fleg <= '0';
- {6 b" n7 u* C( v1 R) ]5 K cmd_fleg <= '0';
' d8 y% L1 r6 x& D/ r0 q end if;
( c- } E ?/ T. N! n c digit <= cmd_buf & num_buf; -- out of key controller
6 G, a9 t8 d- ?+ l% X end if;
8 a* V/ |$ E' z ~( L" } end process;
& \( }' m( V# o# o9 w-------------------
! w) y5 H+ g# d! p. |# N7 `3 `( s key_out <= key_valid;
7 f! T" ~1 D% P5 w3 o digit <= seg4bit;
. u* Y- P3 |/ e# b# y" Jend behavioral; |
|