按键去抖动
一、实验目的
1、学习基于VHDL 描述状态机的方法;
2、学习 VHDL 语言的规范化编程,学习按键去抖动的原理方法。
二、实验平台
微机一台(Windows XP 系统、安装QuartusⅡ等相关软件)、CPLD 学习板一块、5V 电源线一个、下载线一条。
三、设计要求
机械式轻触按键是常用的一种外围器件,由于机械原因导致的抖动会使得按键输入出现毛
刺。
设计一个按键去抖动电路,并用按键作为时钟,结合计数器观察去抖动前后的效果有什么不同。
四设计方案
思路提示:按键去抖动通常采用延时判断的方法,去除按键过程中出现的毛刺。
其实现过程是:当查询到按键按下时,延时一段时间再去判断按键是否仍然被按下,若是则此次按键有
效,否则看作是干扰。
这可以利用状态机来实现,
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity qudou is
port(
clk, en: in std_logic;
sp: out integer range 0 to 7);
end qudou ;
architecture behave of qudou is
type state is (S0,s1,s2);
signal z: std_logic;
signal q: integer range 0 to 2; signal a: integer range 0 to 7; signal s: state;
begin
p1:process(clk)
begin
if(clk'event and clk = '1') then
if en='1' then
if q=2 then
q<=q;
else q<=q+1;
end if;
else q<=0;
end if;
if q=2 then
z<='1';
else z<='0';
end if;
case s is
when s0=>
if (z = '0') then
s<=s0;a<=a;
else
s<=s1;a<=a+1;
end if;
when s1=>
if (z='0') then
s<=s0;a<=a;
else
s<=s2;a<=a;
end if;
when s2=>
if (z='0') then
s<=s0;a<=a;
else
s<=s2;a<=a;
end if;
end case;
sp<=a;
end if;
end process p1;
end behave;
五.实验结果:。