GPIO_KEY.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "GPIO_KEY.h"
  2. #include "app_def.h"
  3. #include "bsp.h"
  4. signed char KEY[1]={-1}; //-1表示未按下,0表示按住,1表示短按,2表示长按
  5. static U08 s_u8keyCnt =0;
  6. extern unsigned int LocalTime;
  7. static VU16 s_vu16KeyTaskTmr = {0};
  8. int GPIO_KEY_init(void)
  9. {
  10. GPIO_InitTypeDef GPIO_InitStructure;
  11. //打开相应GPIO口的时钟
  12. KEY0_RccOpen();
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  14. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  15. GPIO_InitStructure.GPIO_Pin = KEY0_LINE;
  16. GPIO_Init(KEY0_Port,&GPIO_InitStructure);
  17. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  18. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  19. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  20. GPIO_Init(GPIOC,&GPIO_InitStructure);
  21. KEY[0]=-1;
  22. return 1;
  23. }
  24. void KeyTaskTmr(void)
  25. {
  26. if(s_vu16KeyTaskTmr!=0xffff) s_vu16KeyTaskTmr++;
  27. }
  28. void GPIO_KEY_Tick(void)
  29. {
  30. BOOL port = FALSE;
  31. port = GPIO_ReadInputDataBit(KEY0_Port,KEY0_LINE);
  32. if (port) { //no press
  33. s_u8keyCnt ++;
  34. if (s_u8keyCnt > 200) {
  35. s_u8keyCnt =0;
  36. KEY[0]= -1;
  37. s_vu16KeyTaskTmr =0;
  38. }
  39. } else {
  40. s_u8keyCnt =0;
  41. if (s_vu16KeyTaskTmr > OS_TIMER_100MS) {
  42. if ( KEY[0] != 0x01) {
  43. SYS_Reset();
  44. Mcu_Reset(1);
  45. }
  46. KEY[0]= 1 ;
  47. }
  48. }
  49. }