无延时去斗按键实现方法(不好CPU)
这一灵感来源于定时器计数的方法,最后可以实现的效果跟咱们电脑键盘按键的效果一样!我先来介绍下基本原理吧!
采用定时器中断的方法,比如定时器终端我们设置为5ms,我们需要按键按下超过40ms时才算有按键按下,如果按键超过500ms时,我们就确定按键是连续按下的!
那么我就需要一个变量来计数!每次定时器中断时,我们就需要检测下,某个按键是否按下,如果按下,那么我们就把他对应的计数变量加1,如果这个变量等于8(8 = 40ms/5ms)时,我们就给按键的标志位置为1,如果没有按键检测到那个按键没有按下,那么我们就把他对应的按键标志位清零,且他对应的计数变量清零。下面是他的流程图!
下面具体看程序,程序里面有说的很详细,只是我的英语不是怎么样,可能写的不是很通顺,但是我确定,程序肯定是写的令大家满意的!希望大家多多指点!
- /*
- * @for Key Board
- * Usage : First of All ,You Must Add Your Code to Scan Your Board Keys
- * : in Function of "unsigned char GetKeyValue(void)";
- * : Add function "void CheckKey(void)" into your Timer Interrupter Function;
- * : Meanwhile , your should Can modify "TYPECOUNT" to change Count Range;
- * : "KEYNUMBER" is a number of key;
- * : "LIMITCOUNT_VALUE" is to confirm weather there is key click;
- * : "MAXCOUNT_VALUE" is stand for a starting signal to repeat count;
- * Author : Chen Zhenwei
- * E-mail : ieczw@qq.com
- * Date : 2014-04-03
- * PS : My English is Poor,So There Must Be a Great Deal of Fault;
- */
- /*
- * @Get Key Value
- * Depend on Your Board,You Must Edit By yourself;
- * Return Key Value
- * 0 : No Valid Key Value
- * >0 : Valid Key Value
- */
- unsigned char GetKeyValue(void)
- {
- unsigned char KeyValue = 0;
- // To Add your Code to Get KeyValue
- return KeyValue;
- }
- /*
- * Define for Variable Type
- * You can Set The Type of Variable According to Your Requirement;
- */
- #define TYPECOUNT unsigned char // 0 ~ 255
- /*
- * Number of Key's Value
- */
- #define KEYNUMBER 16
- /*
- * Limit Value of Count
- * _ ____ ____________________ __ _
- * ___| |_| |__| |__| |_| |______
- * | | | | | | | | | | | | |
- * 1 2 3 4 5 6 7 8 9 10 11 12 13
- * You Can Set KEYNUMBER 6 ~ 9
- */
- #define LIMITCOUNT_VALUE 20
- /*
- * Model of Keeping Down
- * _ ____ ________________________________________________
- * ___| |_| |__| ....
- * | | | | | | | | | | | | | | | | ....
- * 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- */
- #define MAXCOUNT_VALUE 50
- /*
- * Declare a Global Variable for Key Count
- */
- TYPECOUNT KeyCount[KEYNUMBER];
- /*
- * Usage :
- * Get Value : (KeyFilg & (1<<n))
- * 1 : on click
- * 0 : no
- */
- unsigned long KeyFlag;
- typedef void (*FUNC)(void * arg);
- FUNC KeyFunction[KEYNUMBER]={
- // Register for Key Function
- // Key0
- // Key1
- // Add your Key Function
- };
- #define NULL 0x0000
- /*
- * To Check the Key Board
- */
- void CheckKey(void)
- {
- unsigned char ret;
- unsigned char i = 0;
- ret = GetKeyValue();
- for(i=0; i<KEYNUMBER; i++){
- if(i+1 == ret){
- // Count for Key Which is on Clicking
- KeyCount[i] ++;
- }
- else{
- // Clear Key Flag And Count
- KeyCount[i] = 0;
- KeyFlag &= ~(1<<i);
- }
- if(KeyCount[i] == LIMITCOUNT_VALUE){
- // Set Key Flag '1'
- KeyFlag |= (1<<i);
- // Do Your Given Key Function
- KeyFunction[i](NULL);
- }
- if(KeyCount[i] > MAXCOUNT_VALUE){
- KeyCount[i] = 0;
- KeyFlag &= ~(1<<i);
- }
- }
- }
- /*
- * Key Status
- */
- #define KEYDOWN 1L
- #define KEYUP 0L
- /*
- * You Can Use GetKeyStatus(i) to Get The Key Status;
- * And In Here , You Don't Need to Care About to Clear the Key Flag,
- * Because It Will Be Auto to Clear the Key Flag.
- */
- unsigned char GetKeyStatus(unsigned char KeyNumber)
- {
- unsigned long Status;
- // Get Status of KeyNumber
- Status = KeyFlag&(1<<KeyNumber);
- // Clear Key Flag
- KeyFlag &= ~(1<<KeyNumber);
- return (Status ? KEYDOWN : KEYUP);
- }
无延时去斗按键实现方法(不好CPU)的更多相关文章
- 【CC2530强化实训02】普通延时函数实现按键的长按与短按
[CC2530强化实训02]普通延时函数实现按键的长按与短按 [题目要求] 用一个按键实现单击与双击的功能已经是很多嵌入式产品的常用手法.使用定时器的间隔定时来计算按键按下的时间是通用的做法 ...
- 【CC2530强化实训01】普通延时函数实现按键的长按与短按
[CC2530强化实训01]普通延时函数实现按键的长按与短按 [题目要求] 用一个按键实现长按与短按的功能已经是很多嵌入式产品的常用手法.使用定时器的间隔定时来进行按键按下的时间是通用的做法, ...
- ChemDraw Pro绘制无环链结构的两种方法
ChemDraw Pro 14是一款专门针对化学图形绘制而开发制作的编辑软件,是目前工科类常用的绘制化学结构工具,用于快速绘制常用的环结构组成.以下教程讲解ChemDraw Pro绘制无环链结构的两种 ...
- Android适配底部虚拟按键的方法
---恢复内容开始--- 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近项目进行适配的时候发现部分(如华为手机)存在底部虚拟按键的手机会因为虚拟按键的存在导致挡住部分界面,因为需要全屏显示 ...
- CMD批处理延时启动的几个方法
批处理延时启动的几个方法 方法一:ping 缺点:时间精度为1秒,不够精确 @echo off @ping 127.0.0.1 -n 6 >nul start gdh.txt 方法二:vbs s ...
- 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法
[源码下载] 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法 作者:webabcd 介绍重新想象 Wi ...
- Repeater为空时显示“暂无数据”,很方便实用方法
Repeater为空时显示“暂无数据”,很方便实用方法 <FooterTemplate> <asp:Label ID="lblEmptyZP" Text=&q ...
- win7下安装Ubuntu后进不去win7的解决方法
win7下安装Ubuntu后进不去win7的解决方法 刚刚给同学在win7下安装了Ubuntu16.04,结果在安装完后竟然无法在电脑重启后,找到win7的进入选项. 在网上找了找,都不行!就差点重装 ...
- js数组和字符串去重复几种方法
js数组去重复几种方法 第一种:也是最笨的吧. Array.prototype.unique1 = function () { var r = new Array(); label:for(var i ...
随机推荐
- Spring Boot 集成 MQTT
本文代码有些许问题,处理方案见:解决 spring-integration-mqtt 频繁报 Lost connection 错误 一.添加配置 spring: mqtt: client: usern ...
- node-sass版本问题
node-sass sass-loader的问题 出现了版本的问题 版本太高 版本不兼容解决方法: cnpm i node-sass@4.14.1 cnpm i sass-loader@7.3.1 - ...
- Executor类
//测试的线程 public class Record implements Run ...
- Python使用import导入模块时报ValueError: source code string cannot contain null bytes的解决方案
老猿在导入一个Python模块时报错: >>> import restartnet.py Traceback (most recent call last): File " ...
- Node.js 应用---定时给自己发送邮件
参照传智播客的视频所写代码. js代码: //引用superagent包,用于服务器发送http请求 const request = require('superagent'); //导入cheeri ...
- js- for in 循环 只有一个目的,遍历 对象,通过对象属性的个数 控制循环圈数
for in 循环会返回 原型 以及原型链上面的属性,不会打印系统自带的属性 var obj ={ name:'suan', sex :'male', age:150, height:185, ...
- 2、tensorflow 变量的初始化
https://blog.csdn.net/mzpmzk/article/details/78636137 关于张量tensor的介绍 import tensorflow as tf import n ...
- 学习一下 SpringCloud (一)-- 从单体架构到微服务架构、代码拆分(maven 聚合)
一.架构演变 1.系统架构.集群.分布式系统 简单理解 (1)什么是系统架构? [什么是系统架构?] 系统架构 描述了 在应用程序内部,如何根据 业务.技术.灵活性.可扩展性.可维护性 等因素,将系统 ...
- 通过CSS绘制五星红旗
任务要求: 1.创建一个div作为红旗旗面,用CSS控制其比例宽高比为3:2,背景为红色. 2.再创建五个小的div,用CSS控制其大小和位置. 3.用CSS同时控制每个小div的大小.边框和位置,同 ...
- 【APIO2020】交换城市(Kruskal重构树)
Description 给定一个 \(n\) 个点,\(m\) 条边的无向连通图,边带权. \(q\) 次询问,每次询问两个点 \(x, y\),求两点间的次小瓶颈路.不存在输出 -1. Hint \ ...