VHDL实现矩阵键盘检测

矩阵键盘相比独立键盘,其实就是起到节约IO口的作用,尤其在需要多个按键的时候。原理图(摘自其他地方):


比如0键,没按下时,ROW0因为上拉而为高电平。当0按下时,如果此时COL0为低电平,则ROW0为低电平。因此矩阵键盘就是要让几列(或行)轮流置零,并检测行(或列)的电平高低,从而确定某一按键是否按下。这就是扫描过程。

以下是VHDL代码:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;


entity matrix is

port(clk: in std_logic;

row:  in std_logic_vector(3 downto 0);

col: out std_logic_vector(3 downto 0);

keyout: out std_logic_vector(3 downto 0);

flag: out std_logic);

end matrix;


architecture behave of matrix is

signal colreg: std_logic_vector(3 downto 0);

signal con: std_logic_vector(7 downto 0);

signal cnt: std_logic_vector(15 downto 0);

signal clkreg: std_logic;

begin

--分频,在1~10KHZ左右的时钟进行键盘扫描--

process(clk)

begin

if clk'event and clk = '1' then

if cnt = "1100001101001111" then

cnt<="0000000000000000";

clkreg<='0';

else if cnt = "0110000110100111" then

cnt<=cnt+"0000000000000001";

clkreg<='1';

else

cnt<=cnt+"0000000000000001";

end if;

end if;

end process;

--产生列扫描信号--

process(clkreg)

begin

if clkreg'event and clkreg = '1' then

case colreg is

when "1110" => colreg<="1101";

when "1101" => colreg<="1011";

when "1011" => colreg<="0111";

when "0111" => colreg<="1110";

when others => colreg<="1110";

end case;

end if;

end process;

--对行信号和列信号进行组合--

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值