Key.c 3.1 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. g_ucKeyMode=0;//持续模式
  37. }
  38. /********************************************************************************
  39. * Function:KeyScanPort
  40. *******************************************************************************/
  41. unsigned char KeyScanPort(void)
  42. {
  43. unsigned long k;
  44. unsigned char ck;
  45. k=GPIOB->IDR;
  46. k&=0x01e0;
  47. #ifdef HS110C_MB_B
  48. k^=0x100;
  49. #endif
  50. ck=(unsigned char)(k>>4);
  51. return ck;
  52. }
  53. /************************************************************
  54. *GetKey
  55. 读键盘数据
  56. 建议20ms调用一次此函数
  57. 有按键按下返回1 否则返回0
  58. 持续按下按键分2种情况:
  59. g_ucKeyMode=1时,只产生1次按键按下
  60. g_ucKeyMode=0时,每秒产生1次按键按下实践
  61. *************************************************************/
  62. unsigned char GetKey(void)
  63. {
  64. static unsigned char sucCt=0;//连续模式下响应频率控制
  65. static unsigned char sucKey=0;
  66. static unsigned char sucFlag=0;
  67. static unsigned char Key1,Key2;
  68. //去抖动
  69. if(++sucFlag>3){
  70. sucFlag=0;
  71. }
  72. if(sucFlag==0){
  73. Key1=KeyScanPort();
  74. }else if(sucFlag==2){
  75. Key2=KeyScanPort();
  76. }else return 0;
  77. if(Key1!=Key2)return 0;
  78. //无按键按下
  79. if(Key1==0x1e){
  80. g_ucKeyFree=1;
  81. g_ucDKC=0;
  82. sucKey=0;
  83. sucFlag=0;
  84. sucCt=0;
  85. return 0;
  86. }
  87. //有按键按下
  88. g_ucUKC=0;
  89. if(g_ucKeyMode){//一次模式
  90. if(sucKey!=Key1){
  91. sucKey=Key1;
  92. g_ucKeyValue=sucKey;
  93. g_ucKeyFree=0;
  94. g_ucUKC=0;
  95. return 1;
  96. }
  97. }else{//连续模式
  98. if(sucKey!=Key1){//第1次按下
  99. sucKey=Key1;
  100. g_ucKeyValue=sucKey;
  101. sucCt=50;//第1次和第2次之间的间隔计数器
  102. g_ucUKC=0;
  103. g_ucKeyFree=0;
  104. return 1;
  105. }
  106. if(--sucCt==0){
  107. sucCt=10; //第2次以后的间隔计数器
  108. g_ucUKC=0;
  109. g_ucKeyFree=0;
  110. return 1;
  111. }
  112. }
  113. return 0;
  114. }
  115. void KeyCount(void)
  116. {
  117. if(g_ucDKC<255)g_ucDKC++;
  118. if(g_ucUKC<255)g_ucUKC++;
  119. }
  120. /********************************************************************************
  121. * End of Module
  122. *******************************************************************************/