这一灵感来源于定时器计数的方法,最后可以实现的效果跟咱们电脑键盘按键的效果一样!我先来介绍下基本原理吧!

采用定时器中断的方法,比如定时器终端我们设置为5ms,我们需要按键按下超过40ms时才算有按键按下,如果按键超过500ms时,我们就确定按键是连续按下的!

那么我就需要一个变量来计数!每次定时器中断时,我们就需要检测下,某个按键是否按下,如果按下,那么我们就把他对应的计数变量加1,如果这个变量等于8(8 = 40ms/5ms)时,我们就给按键的标志位置为1,如果没有按键检测到那个按键没有按下,那么我们就把他对应的按键标志位清零,且他对应的计数变量清零。下面是他的流程图!

下面具体看程序,程序里面有说的很详细,只是我的英语不是怎么样,可能写的不是很通顺,但是我确定,程序肯定是写的令大家满意的!希望大家多多指点!

  1. /*
  2. * @for Key Board
  3. * Usage : First of All ,You Must Add Your Code to Scan Your Board Keys
  4. *       : in Function of "unsigned char GetKeyValue(void)";
  5. *       : Add function "void CheckKey(void)" into your Timer Interrupter Function;
  6. *       : Meanwhile , your should Can modify "TYPECOUNT" to change Count Range;
  7. *       : "KEYNUMBER" is a number of key;
  8. *       : "LIMITCOUNT_VALUE" is to confirm weather there is key click;
  9. *       : "MAXCOUNT_VALUE" is stand for a starting signal to repeat count;
  10. * Author : Chen Zhenwei
  11. * E-mail : ieczw@qq.com
  12. * Date : 2014-04-03
  13. * PS : My English is Poor,So There Must Be a Great Deal of Fault;
  14. */
  15. /*
  16. * @Get Key Value
  17. * Depend on Your Board,You Must Edit By yourself;
  18. * Return Key Value
  19. * 0 : No Valid Key Value
  20. * >0 : Valid Key Value
  21. */
  22. unsigned char GetKeyValue(void)
  23. {
  24. unsigned char KeyValue = 0;
  25. // To Add your Code to Get KeyValue
  26. return KeyValue;
  27. }
  28. /*
  29. * Define for Variable Type
  30. * You can Set The Type of Variable According to Your Requirement;
  31. */
  32. #define TYPECOUNT   unsigned char         // 0 ~ 255
  33. /*
  34. * Number of Key's Value
  35. */
  36. #define KEYNUMBER   16
  37. /*
  38. * Limit Value of Count
  39. *     _   ____    ____________________    __   _
  40. * ___| |_|    |__|                    |__|  |_| |______
  41. *  |   |   |   |   |   |   |   |   |   |   |   |   |
  42. *  1   2   3   4   5   6   7   8   9   10  11  12  13
  43. * You Can Set KEYNUMBER 6 ~ 9
  44. */
  45. #define LIMITCOUNT_VALUE    20
  46. /*
  47. * Model of Keeping Down
  48. *     _   ____    ________________________________________________
  49. * ___| |_|    |__|                                                     ....
  50. *  |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   ....
  51. *  1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16
  52. */
  53. #define MAXCOUNT_VALUE      50
  54. /*
  55. * Declare a Global Variable for Key Count
  56. */
  57. TYPECOUNT     KeyCount[KEYNUMBER];
  58. /*
  59. * Usage :
  60. * Get Value : (KeyFilg & (1<<n))
  61. *              1 : on click
  62. *              0 : no
  63. */
  64. unsigned long KeyFlag;
  65. typedef void (*FUNC)(void * arg);
  66. FUNC KeyFunction[KEYNUMBER]={
  67. // Register for Key Function
  68. // Key0
  69. // Key1
  70. // Add your Key Function
  71. };
  72. #define NULL    0x0000
  73. /*
  74. * To Check the Key Board
  75. */
  76. void CheckKey(void)
  77. {
  78. unsigned char ret;
  79. unsigned char i = 0;
  80. ret = GetKeyValue();
  81. for(i=0; i<KEYNUMBER; i++){
  82. if(i+1 == ret){
  83. // Count for Key Which is on Clicking
  84. KeyCount[i] ++;
  85. }
  86. else{
  87. // Clear Key Flag And Count
  88. KeyCount[i] = 0;
  89. KeyFlag &= ~(1<<i);
  90. }
  91. if(KeyCount[i] == LIMITCOUNT_VALUE){
  92. // Set Key Flag '1'
  93. KeyFlag |= (1<<i);
  94. // Do Your Given Key Function
  95. KeyFunction[i](NULL);
  96. }
  97. if(KeyCount[i] > MAXCOUNT_VALUE){
  98. KeyCount[i] = 0;
  99. KeyFlag &= ~(1<<i);
  100. }
  101. }
  102. }
  103. /*
  104. * Key Status
  105. */
  106. #define KEYDOWN     1L
  107. #define KEYUP       0L
  108. /*
  109. * You Can Use GetKeyStatus(i) to Get The Key Status;
  110. * And In Here , You Don't Need to Care About to Clear the Key Flag,
  111. * Because It Will Be Auto to Clear the Key Flag.
  112. */
  113. unsigned char GetKeyStatus(unsigned char KeyNumber)
  114. {
  115. unsigned long Status;
  116. // Get Status of KeyNumber
  117. Status = KeyFlag&(1<<KeyNumber);
  118. // Clear Key Flag
  119. KeyFlag &= ~(1<<KeyNumber);
  120. return (Status ? KEYDOWN : KEYUP);
  121. }

最新文章

  1. 软件测试人员必备Linux命令(初、中、高级)
  2. eclipse 和 android studio 快捷键对比
  3. hibernate 自生双向一对多 多对一管理 (树)
  4. hibernate导入大量数据时,为了避免内存中产生大量对象,在编码时注意什么,如何去除?
  5. CSS-BFC
  6. MySQL中innodb表主键设计原则
  7. 1.4.2.5. 测试(Core Data 应用程序实践指南)
  8. ERP的基础管理-物料编码
  9. JDBC基础学习(三)&mdash;处理BLOB类型数据
  10. MySql基础总结
  11. 详解Bootstrap实现基本布局的方法
  12. ORA-00984: 列在此处不允许 SQL parse error location
  13. WordPress数据结构分析
  14. JS里面的装箱和拆箱操作
  15. UML和模式应用5:细化阶段(10)---UML交互图
  16. C/C++-标准输入/输出重定向为文件输入/输出
  17. HDU 1686 Oulipo(KMP)题解
  18. kafka入门(1)- 基本概念
  19. select rows by values in a column from Dataframe
  20. WEB安全基础之sql注入基础

热门文章

  1. 创建实验楼课程app模块以及配置图片路径
  2. 解决调用WebService报基础连接已经关闭: 服务器关闭了本应保持活动状态的连接的错误的方法
  3. Hadoop完全分布式模式安装部署
  4. PyQt(Python+Qt)学习随笔:Designer中PushButton按钮flat属性
  5. Metasploit魔鬼训练营第一章作业
  6. flask对数据库的外键 主键
  7. 使用k8s部署springboot+redis简单应用
  8. Making Games with Python &amp; Pygame 中文翻译
  9. CSS基础-边框
  10. vue项目中增加打印功能