最近在学习FPGA,就试着写了个按键扫描的程序。虽说有过基于单片机的按键扫描处理经验,对于按键的处理还是有一些概念。
但是单片机程序的编写通常都采用C写,也有用汇编,而FPGA却是采用VHDL或者Verilog这种硬件描述语言来编写。初次利用VHDL编写
控制程序,最开始就有点反应不过来了。采用VHDL语言编写程序与用C语言编写,在思维上会有很大的不一样,因为C程序时顺序执行的,而VHDL语言
各个进程之间是并行执行的,这就会要考虑到时序方面的问题,这正也合乎了硬件工作实际过程,毕竟各个功能模块之间既独立又相关的嘛。
说回基于FPGA的按键扫描处理的问题,通常对按键的处理都要进行延时去抖,以避免按键按下产生的机械抖动等因素影响对按键状态的正确判决。
而且除了要扫描判断按键是否按下,也要扫描判断按键是否弹开(否则一次按键按下如果时间相对长一些,那么程序很容易会误认为连续多次按下)。
基于以上分析,下面是我整理了一下用VHDL语言编写的扫描代码,程序实现按键单击,双击,长按的判断处理。(程序中时钟对应50MHZ,根据不同的主时钟对分频应做相应的修改)

----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_unsigned.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity key is
port(key_state:in BIT;--按键转态,1->按下,0->未按下
clk:in std_logic;
mode1,mode2:out integer range 0 to 2;
q:out integer range 0 to 2
);
end key;
architecture Behavioral of key is
signal cntDiv: std_logic_vector(28 downto 0);
signal key_count,up_key:integer range 0 to 200000;
signal twit_count:integer range 0 to 3;--用于判断是否为双击状态
signal flag_state:integer range 0 to 1;--用于判断是否有按键按下以及按下一次还是连续两次按下

type state is (short_key,long_key,twis_key);--no_key,
signal pr_state:state;
--signal LONG_KEY,TWICE_KEY:integer range 0 to 1000;
begin
process(clk)
begin
if(clk'event and clk = '1')then
cntDiv <= cntDiv + '1';
end if;
end process;
process(cntDiv(7),key_state)
variable flag1,flag2:integer range 0 to 2;------------用于区分短按以及长按的各自不同功能
------------flag1=0/1 => 功能1/功能2;
------------flag2=0/1 => 功能3/功能4;
begin
if(cntDiv(7)'event and cntDiv(7) = '1')then
if(key_state = '1')then
key_count <= key_count +1;
up_key <= 0;
flag_state <= 1;
else
if(key_count > 20 and key_count < 50000)then
twit_count <= twit_count + 1; --判断是否长按的标志
key_count <= 0;
elsif(key_count > 50000)then --------------长按
pr_state <= long_key;
key_count <= 0;
up_key <= 0;
flag_state <= 0;
--////////////根据设计需要添加的代码//////////////////
--代码......................
------------------------------------------------
else
key_count <= 0;
end if;
-------双击,up_key为按键跳起至下一次按下持续时间
if(up_key > 20 and up_key < 50000 and twit_count = 2 )then pr_state <=twis_key;
flag_state <= 0;
up_key <= 0;
twit_count <= 0;
flag2 := flag2 +1;
if(flag2 = 2)then flag2 :=0;
end if;
mode2 <= flag2;
--////////////根据设计需要添加的代码//////////////////
--代码......................
------------------------------------------------
end if;
if(up_key > 50000 and twit_count = 1)then --短按
pr_state <= short_key;
flag_state <= 0;
up_key <= 0;
twit_count <= 0;
flag1 := flag1 +1;
if(flag1 = 2)then flag1 :=0;end if;
--////////////根据设计需要添加的代码//////////////////
--代码......................
------------------------------------------------
end if;
if(flag_state = 1)then
up_key <= up_key + 1;
end if;
end if;
end if;
end process;
process(pr_state)
--variable flag1,flag2:integer range 0 to 2;
begin
case pr_state is
when short_key => q <= 0;------短按返回结果标志
when long_key => q <= 1;------长按结果标志
when twis_key => q <= 2;------双击结果标志
end case;
end process;
end Behavioral;

基于FPGA的按键扫描程序的更多相关文章

  1. C 语言实现基于 Linux 的端口扫描程序

    Socket 常用函数: ⑴int socket(int protofamily, int type, int protocol); protofamily:即协议域,又称为协议族(family).常 ...

  2. 基于FPGA的飞机的小游戏

    基于FPGA的飞机的小游戏 实验原理 该实验主要分为4个模块,采用至上而下的设计方法进行设计.由50M的晶振电路提供时钟源,VGA显示控制模块.图形显示控制模块.移动模块的时钟为25M,由时钟分频电路 ...

  3. 基于FPGA的红外遥控解码与PC串口通信

    基于FPGA的红外遥控解码与PC串口通信 zouxy09@qq.com http://blog.csdn.net/zouxy09 这是我的<电子设计EDA>的课程设计作业(呵呵,这个月都拿 ...

  4. 基于FPGA的5寸LCD显示屏的显示控制

    基于FPGA的5寸LCD显示屏的显示控制 作者:lee神 1,图像处理基础知识 数字图像处理是指将图像信号转换成数字信号并利用计算机对其进行处理的过程.图像处理最早出现于 20 世纪 50 年代,当时 ...

  5. 基于FPGA的图像开发平台 其他摄像头附件说明(OV5642 OV9655)

    基于FPGA的图像开发平台 其他摄像头附件说明 FPGA_VIP_V101 编者 奇迹再现 个人博客 http://www.cnblogs.com/ccjt/ 联系邮箱 Shenyae86@163.c ...

  6. 基于FPGA的VGA显示实验设计

    基于FPGA的VGA显示实验设计 成果展示(优酷视频): 视频: 基于FPGA的VGA显示技术(手机控制) http://v.youku.com/v_show/id_XNjk4ODE3ODUy.htm ...

  7. 基于FPGA的线阵CCD图像测量系统研究——笔记

    本文是对基于FPGA的线阵CCD图像测量系统研究(作者:高尚)的阅读笔记 第一章绪论 1. 读读看 读了前面的摘要依然没有看懂作者要做什么.接着往下读....终于看到了一个字眼“基于机器视觉的图像测量 ...

  8. 基于FPGA的DW8051移植(三)

    总结一下问题: 1) http://www.cnblogs.com/sepeng/p/4137405.html  基于FPGA的DW8051移植(一)里面用modelsim观测波形发现程序进入了ida ...

  9. 基于FPGA的OLED真彩色动态图像显示的实现

    源:基于FPGA的OLED真彩色动态图像显示的实现 作为第3代显示器,有机电致发光器件(Organic Light Emitting Diode,OLED)由于其主动发光.响应快.高亮度.全视角.直流 ...

随机推荐

  1. Java 中队列的使用

    刚才看见群里的一个朋友在问队列的使用,确实在现实的写代码中非常少使用队列的,反正我是从来没使用过.仅仅是学数据结构的时候学过. 以下是我写的一个小样例,希望有不足之处请提出改正.O(∩_∩)O~ 看代 ...

  2. JDK1.6官方下载_JDK6官方下载_JDK1.6API(chm)下载_JDK6APICHM中文参

    JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin- ...

  3. A Simple Actions Recognition System

    1. Problem Definition There's no doubt that researches and applications on the foundation of videos ...

  4. apache vhost 访问权限配置

    apache的<directory>     </directory>语句,查考如下: 如何访问根目录下的目录http://192.168.1.12/test/ 第一.缺省ap ...

  5. 小白日记36:kali渗透测试之Web渗透-手动漏洞挖掘(二)-突破身份认证,操作系统任意命令执行漏洞

    手动漏洞挖掘 ###################################################################################### 手动漏洞挖掘 ...

  6. customerized convert from field type to DB field's type

    @LastModifiedDate @Convert(converter = LocalDateTime2TimestampConverter.class) @Slf4j public class L ...

  7. Class类中getMethods() 与getDeclaredMethods() 方法的区别

    一:jdk API中关于两个方法的解释 1:getMethods(),该方法是获取本类以及父类或者父接口中所有的公共方法(public修饰符修饰的) 2:getDeclaredMethods(),该方 ...

  8. next_permutation()—遍历全排列

    # next_permutation()--遍历全排列 template <class BidirectionalIterator> bool next_permutation (Bidi ...

  9. 在IIS7.5上添加.NET4.0程序的虚拟目录时提示ASP.NET 4.0尚未在 Web 服务器上注册

    使用VS2010创建web应用程序时出现如下提示: ASP.NET 4.0尚未在 Web 服务器上注册. 解决方法: 首先IIS应用程序池添加 net framework4.0版本. 接着找到C:\W ...

  10. Scala中的数组

    数组 数组的两种声明方式,建议声明数组时指定类型. 访问数组元素时获取数组下标 数组Array类本身有很多非常方便的方法 变长数组ArrayBuffer,能够动态增加元素,也可以实现与Array的互转 ...