Key.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "includes.h"
  2. unsigned long g_ulKeyValue;
  3. unsigned char g_ucKeyFree=0;
  4. unsigned char g_ucKeyMode=0;
  5. unsigned char g_ucKeyPPTPress=0;
  6. unsigned char g_ucUKC=0;
  7. void PowerOffCheck(void)
  8. {
  9. //开机后,长按电源键关机
  10. static unsigned int secondTick=0;
  11. if((GPIOB->IDR & GPIO_Pin_3) == 0)
  12. {//按下了
  13. if(++secondTick >= 900000)
  14. {//关机
  15. IapTrace("Power on");
  16. GPIOB->BSRR=GPIO_Pin_8;
  17. // while(1)
  18. // {
  19. //
  20. // }
  21. }
  22. }else{
  23. secondTick=0;
  24. }
  25. }
  26. void CheckPwrKeyOn(void)
  27. {//长按电源键直到电筒灯亮后释放
  28. unsigned int tick=0;
  29. if(KeyPwrStatus !=0)
  30. {
  31. POWER_ON_FAILED:
  32. IapTrace("\r\nPwrOnFailed");
  33. DelayMs(10);
  34. PWR_EN_LOW;
  35. while(1);
  36. }
  37. while(KeyPwrStatus==0)
  38. {
  39. if(++tick >= 200000)
  40. {
  41. IapTrace("\r\nPwrOnOk");
  42. PWR_EN_HIGH;//
  43. return;
  44. }
  45. }
  46. IapTrace("\r\nPwr erro");
  47. goto POWER_ON_FAILED;
  48. }
  49. void KeyInit(void)
  50. {
  51. GPIO_InitTypeDef GPIO_InitStructure;
  52. //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  53. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
  54. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
  55. /*GPIO先全部设置为上拉输入*/
  56. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  57. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  58. //PPT
  59. GPIO_InitStructure.GPIO_Pin=KEY_PPT_PIN;
  60. GPIO_Init(KEY_PPT_PORT, &GPIO_InitStructure);
  61. // INFO VOL+
  62. GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN;
  63. GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure);
  64. // FUNC VOL-
  65. GPIO_InitStructure.GPIO_Pin=KEY_FUNC_PIN;
  66. GPIO_Init(KEY_FUNC_PORT, &GPIO_InitStructure);
  67. //KEY_POWER 电源检测几秒
  68. /*GPIO设置为浮空*/
  69. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  70. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  71. GPIO_InitStructure.GPIO_Pin=KEY_POWER_PIN;
  72. GPIO_Init(KEY_POWER_PORT, &GPIO_InitStructure);
  73. CheckPwrKeyOn();
  74. g_ucKeyMode=0;//连续触发模式
  75. }
  76. static unsigned long KeyScanPort(void)
  77. {
  78. unsigned long k1,k2;
  79. k1=GPIOB->IDR;
  80. k1&=KEY_ALL_PORTB;
  81. k1<<=16;
  82. return (k1);
  83. }
  84. /************************************************************
  85. *GetKey
  86. 读键盘数据
  87. 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
  88. 参数:
  89. g_ucKeyMode=1时,单次模式
  90. g_ucKeyMode=0时,持续模式
  91. 返回值:
  92. 0--按键无变化
  93. 1 --按键有按下:
  94. 单次模式下,当按键第一次按下才返回一次该值;
  95. 持续模式下,按着按键不放将间隔返回该值
  96. g_ulKeyValue保存当前按键值
  97. -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
  98. 释放后,g_ulKeyValue保存上次按下的按键值
  99. *************************************************************/
  100. unsigned char PttKeyFlag;
  101. u32 Flashtick=0;
  102. void KeyCount(void)
  103. {
  104. //if(g_ucDKC<255)g_ucDKC++;
  105. if(g_ucUKC<255)g_ucUKC++;
  106. }