-- 矩阵键盘实验1:向用户介绍矩阵键盘扫描实现的方法,没有考虑去抖和判断键d起的问题;把相应的键值显示在数码管上
--
library IEEE
use IEEE.STD_LOGIC_1164.ALL
use IEEE.STD_LOGIC_ARITH.ALL
use IEEE.STD_LOGIC_UNSIGNED.ALL
ENTITY KEYSCAN IS
PORT (
clk : IN std_logic
rst : IN std_logic
row : OUT std_logic_vector(3 DOWNTO 0)-- 行线
column : IN std_logic_vector(3 DOWNTO 0) -- 列线
dataout : OUT std_logic_vector(7 DOWNTO 0)-- 数码管显示数据
en : OUT std_logic)--数码管显示使能
END KEYSCAN
ARCHITECTURE arch OF KEYSCAN IS
SIGNAL div_cnt : std_logic_vector(24 downto 0)
SIGNAL scan_key : std_logic_vector(3 DOWNTO 0)--扫描码寄存器
SIGNAL key_code : std_logic_vector(3 DOWNTO 0)
SIGNAL dataout_tmp: std_logic_vector(7 DOWNTO 0)
BEGIN
row <= scan_key
dataout <= dataout_tmp
en <= '0'
PROCESS(clk,rst)
BEGIN
IF (NOT rst = '1') THEN
div_cnt <= "0000000000000000000000000"
ELSIF(clk'EVENT AND clk = '1')THEN
div_cnt <= div_cnt + 1
END IF
END PROCESS
PROCESS(div_cnt(20 downto 19))
BEGIN
CASE div_cnt(20 downto 19) IS
WHEN "00"=>scan_key<="1110"
WHEN "01"=>scan_key<="1101"
WHEN "10"=>scan_key<="1011"
WHEN "11"=>scan_key<="0111"
END CASE
END PROCESS
PROCESS(clk,rst)
BEGIN
IF (NOT rst = '1') THEN
key_code <= "0000"
ELSIF(clk'EVENT AND clk='1')THEN
CASE scan_key IS --检测何处有键按下
WHEN "1110" =>
CASE column IS
WHEN "1110" =>
key_code <= "0000"
WHEN "1101" =>
key_code <= "0001"
WHEN "1011" =>
key_code <= "0010"
WHEN "0111" =>
key_code <= "0011"
WHEN OTHERS =>
NULL
END CASE
WHEN "1101" =>
CASE column IS
WHEN "1110" =>
key_code <= "0100"
WHEN "1101" =>
key_code <= "0101"
WHEN "1011" =>
key_code <= "0110"
WHEN "0111" =>
key_code <= "0111"
WHEN OTHERS =>
NULL
END CASE
WHEN "1011" =>
CASE column IS
WHEN "1110" =>
key_code <= "1000"
WHEN "1101" =>
key_code <= "1001"
WHEN "1011" =>
key_code <= "1010"
WHEN "0111" =>
key_code <= "1011"
WHEN OTHERS =>
NULL
END CASE
WHEN "0111" =>
CASE column IS
WHEN "1110" =>
key_code <= "1100"
WHEN "1101" =>
key_code <= "1101"
WHEN "1011" =>
key_code <= "1110"
WHEN "0111" =>
key_code <= "1111"
WHEN OTHERS =>
NULL
END CASE
WHEN OTHERS =>
key_code <= "1111"
END CASE
END IF
END PROCESS
-----显示键值
PROCESS(key_code)
BEGIN
CASE key_code IS
WHEN "0000" =>
dataout_tmp <= "00000011"
WHEN "0001" =>
dataout_tmp <= "10011111"
WHEN "0010" =>
dataout_tmp <= "00100101"
WHEN "0011" =>
dataout_tmp <= "00001101"
WHEN "0100" =>
dataout_tmp <= "10011001"
WHEN "0101" =>
dataout_tmp <= "01001001"
WHEN "0110" =>
dataout_tmp <= "01000001"
WHEN "0111" =>
dataout_tmp <= "00011111"
WHEN "1000" =>
dataout_tmp <= "00000001"
WHEN "1001" =>
dataout_tmp <= "00011001"
WHEN "1010" =>
dataout_tmp <= "00010001"
WHEN "1011" =>
dataout_tmp <= "11000001"
WHEN "1100" =>
dataout_tmp <= "01100011"
WHEN "1101" =>
dataout_tmp <= "10000101"
WHEN "1110" =>
dataout_tmp <= "01100001"
WHEN "1111" =>
dataout_tmp <= "01110001"
WHEN OTHERS =>
NULL
END CASE
END PROCESS
END arch
假设按下的是S1键进行如下检测(4*4键盘)先在P3口输出
p3 00001111
低四位 行会有变化
cord_h =00001111&00001110 =00001110
if !=00001111
延时0.1us
cord_h=00001110&00001111=00001110
if !=00001111
P3再输出11111110
P3=00001110|11110000=11111110
输出高四位
cord_l=P3&0xf0 //此时P3口就是输入值01111110 而不是上面的11111110
cord_l=01111110&11110000=01110000
cord_h+cord_l=00001110+01110000=01111110=0x7e //此编码即为S1的编码
#include <reg52.h>//包含头文件
#define uchar unsigned char
#define uint unsigned int
unsigned char const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71}//0-F
uchar keyscan(void)
void delay(uint i)
void main()
{
uchar key
P2=0x00//1数码管亮 按相应的按键,会显示按键上的字符
while(1)
{
key=keyscan()//调用键盘扫描,
switch(key)
{
case 0x7e:P0=table[0]break//0 按下相应的键显示相对应的码值
case 0x7d:P0=table[1]break//1
case 0x7b:P0=table[2]break//2
case 0x77:P0=table[3]break//3
case 0xbe:P0=table[4]break//4
case 0xbd:P0=table[5]break//5
case 0xbb:P0=table[6]break//6
case 0xb7:P0=table[7]break//7
case 0xde:P0=table[8]break//8
case 0xdd:P0=table[9]break//9
case 0xdb:P0=table[10]break//a
case 0xd7:P0=table[11]break//b
case 0xee:P0=table[12]break//c
case 0xed:P0=table[13]break//d
case 0xeb:P0=table[14]break//e
case 0xe7:P0=table[15]break//f
}
}
}
uchar keyscan(void)//键盘扫描函数,使用行列反转扫描法
{
uchar cord_h,cord_l//行列值
P3=0x0f //行线输出全为0
cord_h=P3&0x0f//读入列线值
if(cord_h!=0x0f)//先检测有无按键按下
{
delay(100) //去抖
cord_h=P3&0x0f //读入列线值
if(cord_h!=0x0f)
{
P3=cord_h|0xf0 //输出当前列线值
cord_l=P3&0xf0 //读入行线值
return(cord_h+cord_l)//键盘最后组合码值
}
}return(0xff)//返回该值
}
void delay(uint i)//延时函数
{
while(i--)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)