Key.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*******************************************************************************
  2. * File Name: Key.c
  3. * Function Describe:device for the Keys
  4. * Relate Module:
  5. * Explain: Hardware version is HS110C
  6. * Writer: ShiLiangWen
  7. * Date: 2015.1.26
  8. *******************************************************************************/
  9. #include "stm32f10x.h"
  10. #include "Key.h"
  11. #include "includes.h"
  12. //key value variable
  13. unsigned char g_ucKeyValue;
  14. unsigned char g_ucDKC=0;
  15. unsigned char g_ucUKC=0;
  16. unsigned char g_ucKeyFree=0;
  17. unsigned char g_ucKeyMode=0;
  18. ////////////////////////// function declare //////////////////////////////
  19. unsigned char KeyScanPort(void);
  20. ////////////////////////// end declare //////////////////////////////
  21. /********************************************************************************
  22. KeyInit
  23. PB5 CH_DM_KEY
  24. PB6 PPT_KEY
  25. PB7 CH_UP_KEY
  26. PB8 FUN_KEY
  27. *******************************************************************************/
  28. void KeyInit(void)
  29. {
  30. GPIO_InitTypeDef GPIO_InitStructure;
  31. /*GPIO先全部设置为上拉输入*/
  32. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  33. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  34. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
  35. GPIO_Init(GPIOB, &GPIO_InitStructure);
  36. EXTIX_Init();
  37. g_ucKeyMode=0;//持续模式
  38. }
  39. /********************************************************************************
  40. * Function:KeyScanPort
  41. *******************************************************************************/
  42. unsigned char KeyScanPort(void)
  43. {
  44. unsigned long k;
  45. unsigned char ck;
  46. k=GPIOB->IDR;
  47. k&=0x01e0;
  48. #ifdef HS110C_MB_B
  49. k^=0x100;
  50. #endif
  51. ck=(unsigned char)(k>>4);
  52. return ck;
  53. }
  54. /************************************************************
  55. *GetKey
  56. 读键盘数据
  57. 建议20ms调用一次此函数
  58. 有按键按下返回1 否则返回0
  59. 持续按下按键分2种情况:
  60. g_ucKeyMode=1时,只产生1次按键按下
  61. g_ucKeyMode=0时,每秒产生1次按键按下实践
  62. *************************************************************/
  63. unsigned char GetKey(void)
  64. {
  65. static unsigned char sucCt=0;//连续模式下响应频率控制
  66. static unsigned char sucKey=0;
  67. static unsigned char sucFlag=0;
  68. static unsigned char Key1,Key2;
  69. //去抖动
  70. if(++sucFlag>3){
  71. sucFlag=0;
  72. }
  73. if(sucFlag==0){
  74. Key1=KeyScanPort();
  75. }else if(sucFlag==2){
  76. Key2=KeyScanPort();
  77. }else return 0;
  78. if(Key1!=Key2)return 0;
  79. //无按键按下
  80. if(Key1==0x1e){
  81. g_ucKeyFree=1;
  82. g_ucDKC=0;
  83. sucKey=0;
  84. sucFlag=0;
  85. sucCt=0;
  86. return 0;
  87. }
  88. //有按键按下
  89. g_ucUKC=0;
  90. if(g_ucKeyMode){//一次模式
  91. if(sucKey!=Key1){
  92. sucKey=Key1;
  93. g_ucKeyValue=sucKey;
  94. g_ucKeyFree=0;
  95. g_ucUKC=0;
  96. return 1;
  97. }
  98. }else{//连续模式
  99. if(sucKey!=Key1){//第1次按下
  100. sucKey=Key1;
  101. g_ucKeyValue=sucKey;
  102. sucCt=50;//第1次和第2次之间的间隔计数器
  103. g_ucUKC=0;
  104. g_ucKeyFree=0;
  105. return 1;
  106. }
  107. if(--sucCt==0){
  108. sucCt=10; //第2次以后的间隔计数器
  109. g_ucUKC=0;
  110. g_ucKeyFree=0;
  111. return 1;
  112. }
  113. }
  114. return 0;
  115. }
  116. void KeyCount(void)
  117. {
  118. if(g_ucDKC<255)g_ucDKC++;
  119. if(g_ucUKC<255)g_ucUKC++;
  120. }
  121. /********************************************************************************
  122. * End of Module
  123. *******************************************************************************/