key_drivers.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "key_drivers.h"
  2. #include "adc.h"
  3. Key_TypeDef Key_On_Off;
  4. Key_TypeDef Key_Walk;
  5. Key_TypeDef Key_Light;
  6. Key_TypeDef Key_Set;
  7. Key_TypeDef Key_Down;
  8. Key_TypeDef Key_Up;
  9. static void _GPIO_Read(Key_TypeDef* Key)
  10. {
  11. static uint8_t ReadData;
  12. if(*(Key->ADC_result)>3000)
  13. {
  14. ReadData = 0x00;
  15. }
  16. else if(*(Key->ADC_result)<1000)
  17. {
  18. ReadData = 0x01;
  19. }
  20. Key->Trg = ReadData & (ReadData ^ (Key->Cont));
  21. Key->Cont = ReadData;
  22. }
  23. void KeyParamInit(Key_TypeDef * Key)
  24. {
  25. Key->Cont = 0;
  26. Key->Trg = 0;
  27. Key->PressFlasg = 0;
  28. Key->TimeCnt = 0;
  29. Key->KeyStatus = Key_Status_NoPress;
  30. }
  31. void KeyParamInit_no_cont(Key_TypeDef * Key)
  32. {
  33. Key->Trg = 0;
  34. Key->PressFlasg = 0;
  35. Key->TimeCnt = 0;
  36. Key->KeyStatus = Key_Status_NoPress;
  37. }
  38. //按键GPIO初始化
  39. void KeyInitial(void)
  40. {
  41. //初始化Key_On_Off
  42. Key_On_Off.ADC_result =&ADC_Result[1] ;
  43. KeyParamInit(&Key_On_Off);
  44. //初始化Key_Walk
  45. Key_Walk.ADC_result =&ADC_Result[2] ;
  46. KeyParamInit(&Key_Walk);
  47. //初始化Key_Light
  48. Key_Light.ADC_result =&ADC_Result[3] ;
  49. KeyParamInit(&Key_Light);
  50. //初始化Key_Down
  51. Key_Down.ADC_result =&ADC_Result[4] ;
  52. KeyParamInit(&Key_Down);
  53. //初始化Key_Up
  54. Key_Up.ADC_result =&ADC_Result[5] ;
  55. KeyParamInit(&Key_Up);
  56. }
  57. //按键检测函数
  58. void Key_Check(Key_TypeDef* Key, uint8_t DelayTime)
  59. {
  60. _GPIO_Read(Key);
  61. if(Key->Trg & 0x01)//表示按键有触发(下降沿)
  62. {
  63. Key->PressFlasg = 1;
  64. }
  65. if(Key->PressFlasg == 1)//按键被触发一次
  66. {
  67. if(Key->Cont & 0x01)//表示按键未放开
  68. {
  69. Key->TimeCnt++;
  70. if(Key->TimeCnt >= DelayTime)//长按时间 DelayTime * 10ms
  71. {
  72. Key->TimeCnt = 0;
  73. Key->PressFlasg = 0;
  74. Key->KeyStatus = Key_Status_LongPress;
  75. return;
  76. }
  77. }
  78. else if(Key->Cont == 0x00)//按键弹起
  79. {
  80. Key->TimeCnt = 0;
  81. Key->PressFlasg = 0;
  82. Key->KeyStatus = Key_Status_ShortPress;
  83. return;
  84. }
  85. }
  86. return;
  87. }
  88. KeyScanCode_Struct_t KeyScan(void)
  89. {
  90. KeyScanCode_Struct_t Result;
  91. Result.Code = 0x00;
  92. if(*(Key_On_Off.ADC_result)>3000)
  93. Result.Key_Bit.Power = 0;
  94. else if(*(Key_On_Off.ADC_result)<1000)
  95. Result.Key_Bit.Power = 1;
  96. if(*(Key_Walk.ADC_result)>3000)
  97. Result.Key_Bit.Walk = 0;
  98. else if(*(Key_Walk.ADC_result)<1000)
  99. Result.Key_Bit.Walk = 1;
  100. if(*(Key_Up.ADC_result)>3000)
  101. Result.Key_Bit.Add = 0;
  102. else if(*(Key_Up.ADC_result)<1000)
  103. Result.Key_Bit.Add = 1;
  104. if(*(Key_Down.ADC_result)>3000)
  105. Result.Key_Bit.Dec = 0;
  106. else if(*(Key_Down.ADC_result)<1000)
  107. Result.Key_Bit.Dec = 1;
  108. // if(*(Key_Set.ADC_result)>4000)
  109. // Result.Key_Bit.Set = 0;
  110. // else if(*(Key_Set.ADC_result)<500)
  111. // Result.Key_Bit.Set = 1;
  112. if(*(Key_Light.ADC_result)>3000)
  113. Result.Key_Bit.Light = 0;
  114. else if(*(Key_Light.ADC_result)<1000)
  115. Result.Key_Bit.Light = 1;
  116. return Result;
  117. }