-----Title:CPLD键盘扫描数码管显示键值--循环扫描方式
----ALTERA: QUARTUS II 7.2 编译 2009-3-4 12:00:40
---Target : EPM718SLC84
---Crystal: 4.0000Mhz 有源晶振接全局时钟引脚
--Author :borlittle
--- qq:634172943
---Function:四乘四矩阵键盘扫描按键识别
---备 :本程序经调试好使,如果程序不好使,请注意检查引脚设置,以及全局时钟、独立时钟等约束可以参照文件夹中的截图
---程序能准确识别按键并显示,但是似乎对于连按不好识别
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity keyc is
port( clk0,clk2:in std_logic;
cr:in std_logic;---扫描时钟信号输入,按键开关控制扫描计数器的初始化,可以用电解电容220UF串接一个10K电阻
----电解电容正极接电源,电阻另一脚接地,二者串联的中间点接到CR引脚已实现上电复位循环码计数----器
col:in std_logic_vector(3 downto 0); ---列扫描信号输入
al,bl,cl,dl,el,fl,gl:out std_logic; ---输出显示段码
row:out std_logic_vector(3 downto 0)
); ---行扫描信号输出
end keyc;
architecture a of keyc is
signal line:std_logic_vector(3 downto 0);
signal bcdmin,bcdmo:std_logic_vector(3 downto 0);
signal clr:std_logic;
begin
pl:process(clk0,clr) ---pl进程描述一个循环码计数器,计数器输出作为键盘扫描信号
begin
if(clr='0')then
line<="1000";
elsif clk0'event and clk0='1' then ---检测上升沿
line(3 downto 0)<=line(2 downto 0)&line(3);
end if;
end process pl;
row(3)<=line(3); ---行扫描信号输出到端口,以驱动键盘行线
row(2)<=line(2);
row(1)<=line(1);
row(0)<=line(0);
p2:process (line,col) ---p2进程描述键盘输入信号的译码过程,键盘值译码器,列线需要接四个下拉接地的电阻
begin
if(line="1000" and col="1000") then ---1
bcdmin<="0001";
elsif(line="1000"and col="0100") then ---2
bcdmin<="0010";
elsif(line="1000"and col="0010") then ---3
bcdmin<="0011";
elsif(line="1000"and col="0001") then ---4
bcdmin<="0100";
elsif(line="0100" and col="1000") then ---5
bcdmin<="0101";
elsif(line="0100" and col="0100") then ---6
bcdmin<="0110";
elsif(line="0100" and col="0010") then ---7
bcdmin<="0111";
elsif(line="0100" and col="0001") then ---8
bcdmin<="1000";
elsif(line="0010" and col="1000") then ---9
bcdmin<="1001";
elsif(line="0010" and col="0100") then ---0
bcdmin<="1010";
elsif(line="0010" and col="0010") then ---A
bcdmin<="1011";
elsif(line="0010" and col="0001") then ---B
bcdmin<="1100";
elsif(line="0001" and col="1000") then ---C
bcdmin<="1101";
elsif(line="0001" and col="0100") then ---D
bcdmin<="1110";
elsif(line="0001" and col="0010") then --E
bcdmin<="1111";
else
bcdmin<="0000"; ---wu
end if;
end process p2;
p3:process(clk2) ---p3进程描述把按键的信号译码输出到寄存器寄存,寄存器
begin
if(clk2'event and clk2='1') then
if(col(0) or col(1) or col(2) or col(3))='1' then
bcdmo<=bcdmin;---关键步鄹,若有按键,第一次出现得正确译码被寄存到存储器
else
bcdmo<=bcdmo;---如有按键抖动,则寄存器中的数据保持原有状态,已写入数据不受干扰
end if;
end if;
end process p3;
p4:process (bcdmo) ---将寄存器输出得4位码转换为驱动七段数码管得代码
begin
if(bcdmo="0001") then ---1
al<='1';
bl<='0';
cl<='0';
dl<='1';
el<='1';
fl<='1';
gl<='1';
elsif(bcdmo="0010") then ---2
al<='0';
bl<='0';
cl<='1';
dl<='0';
el<='0';
fl<='1';
gl<='0';
elsif(bcdmo="0011") then ---3
al<='0';
bl<='0';
cl<='0';
dl<='0';
el<='1';
fl<='1';
gl<='0';
elsif(bcdmo="0100") then ---4
al<='1';
bl<='0';
cl<='0';
dl<='1';
el<='1';
fl<='0';
gl<='0';
elsif(bcdmo="0101") then ---5
al<='0';
bl<='1';
cl<='0';
dl<='0';
el<='1';
fl<='0';
gl<='0';
elsif(bcdmo="0110") then ---6
al<='0';
bl<='1';
cl<='0';
dl<='0';
el<='0';
fl<='0';
gl<='0';
elsif(bcdmo="0111") then --7
al<='0';
bl<='0';
cl<='0';
dl<='1';
el<='1';
fl<='1';
gl<='1';
elsif(bcdmo="1000") then ---8
al<='0';
bl<='0';
cl<='0';
dl<='0';
el<='0';
fl<='0';
gl<='0';
elsif(bcdmo="1001") then ---9
al<='0';
bl<='0';
cl<='0';
dl<='0';
el<='1';
fl<='0';
gl<='0';
elsif(bcdmo="1010") then ---0
al<='0';
bl<='0';
cl<='0';
dl<='0';
el<='0';
fl<='0';
gl<='1';
elsif(bcdmo="1011") then ---A
al<='0';
bl<='0';
cl<='0';
dl<='1';
el<='0';
fl<='0';
gl<='0';
elsif(bcdmo="1100") then ---B
al<='1';
bl<='1';
cl<='0';
dl<='0';
el<='0';
fl<='0';
gl<='0';
elsif(bcdmo="1101") then ---C
al<='0';
bl<='1';
cl<='1';
dl<='0';
el<='0';
fl<='0';
gl<='1';
elsif(bcdmo="1110") then ---D
al<='1';
bl<='0';
cl<='0';
dl<='0';
el<='0';
fl<='1';
gl<='0';
elsif(bcdmo="1111") then ---E
al<='0';
bl<='1';
cl<='1';
dl<='0';
el<='0';
fl<='0';
gl<='0';
else ---无
al<='1';
bl<='1';
cl<='1';
dl<='1';
el<='1';
fl<='1';
gl<='1';
end if;
end process;
p5:process(cr) ---用一个端口按键开关控制扫描计数器的初始化,一个T触发器,下降沿触发
begin
if(cr'event and cr='0')then
clr<=NOT clr;
end if;
end process p5;
end a;
源文件:https://static.assets-stash.eet-china.com/album/old-resources/2009/3/5/ce7f423f-f9af-409c-99f1-f75464902939.rar
文章评论(0条评论)
登录后参与讨论