Key.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "Key.h"
  2. #include "SysTick.h"
  3. #include "gd32e230x_conf.h"
  4. #define KEY_NAME_MAX 5
  5. #define KEY_A_RCU RCU_GPIOA
  6. #define KEY_A_IO GPIOA
  7. #define KEY_B_RCU RCU_GPIOB
  8. #define KEY_B_IO GPIOB
  9. #define KEY_F_RCU RCU_GPIOF
  10. #define KEY_F_IO GPIOF
  11. #define KEY_ON 0
  12. #define KEY_OFF 1
  13. #define KEY1 GPIO_PIN_4
  14. #define KEY2 GPIO_PIN_5
  15. #define KEY3 GPIO_PIN_6
  16. #define KEY4 GPIO_PIN_7
  17. #define KEY5 GPIO_PIN_15
  18. #define DERECTION_GPIO_1 GPIOA
  19. #define DERECTION_IO_1 GPIO_PIN_11
  20. #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOA
  21. #define DERECTION_IO_1_PIN DERECTION_IO_11
  22. #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN11
  23. #define DERECTION_IO_1_EXIT EXTI_11
  24. #if (SUPPORT_2_DETECTION)
  25. #define DERECTION_GPIO_2 GPIOB
  26. #define DERECTION_IO_2 GPIO_PIN_3
  27. #define DERECTION_IO_2_RCU EXTI_SOURCE_GPIOB
  28. #define DERECTION_IO_2_PIN DERECTION_IO_3
  29. #define DERECTION_IO_2_SOURCE EXTI_SOURCE_PIN3
  30. #define DERECTION_IO_2_EXIT EXTI_3
  31. #endif
  32. //#if SUPPORT_DETECTION
  33. uint8_t key_detection_1 = 0;
  34. uint8_t key_detection_2 = 0;
  35. //#endif
  36. extern uint16_t detection_count_1;
  37. extern uint16_t detection_count_2;
  38. static unsigned char s_arrKeyDownLevel[KEY_NAME_MAX]; //使用前要在InitKey函数中进行初始化
  39. static void ConfigKeyGPIO(void); //配置按键的GPIO
  40. static void ConfigDetection(void);
  41. static void ConfigKeyGPIO(void)
  42. {
  43. //使能RCU相关时钟
  44. rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA的时钟
  45. rcu_periph_clock_enable(RCU_GPIOF); //使能GPIOF的时钟
  46. rcu_periph_clock_enable(RCU_GPIOB);
  47. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY1); //配置PF7为上拉输入K1
  48. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY2); //配置PF6为上拉输入K2
  49. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY3); //配置PA12为上拉输入K3
  50. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY4); //配置PA12为上拉输入K3
  51. gpio_mode_set(KEY_A_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY5); //配置PA12为上拉输入K3
  52. }
  53. static void ConfigDetection(void)
  54. {
  55. #if SUPPORT_DETECTION
  56. rcu_periph_clock_enable(RCU_CFGCMP);
  57. rcu_periph_clock_enable(RCU_GPIOA);
  58. #if SUPPORT_2_DETECTION
  59. rcu_periph_clock_enable(RCU_GPIOB);
  60. #endif
  61. //config io
  62. gpio_mode_set(DERECTION_GPIO_1, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_1);
  63. #if SUPPORT_2_DETECTION
  64. gpio_mode_set(DERECTION_GPIO_2, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_2);
  65. #endif
  66. nvic_irq_enable(EXTI4_15_IRQn,2);
  67. syscfg_exti_line_config(DERECTION_IO_1_RCU,DERECTION_IO_1_SOURCE);
  68. exti_init(DERECTION_IO_1_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  69. exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
  70. //config interrupt
  71. #if SUPPORT_2_DETECTION
  72. nvic_irq_enable(EXTI2_3_IRQn,2);
  73. syscfg_exti_line_config(DERECTION_IO_2_RCU,DERECTION_IO_2_SOURCE);
  74. exti_init(DERECTION_IO_2_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  75. exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
  76. #endif
  77. #endif
  78. }
  79. void InitKey(void)
  80. {
  81. ConfigKeyGPIO(); //配置按键的GPIO
  82. ConfigDetection();
  83. }
  84. unsigned char ScanKey(unsigned int gpio_periph,unsigned int pin)
  85. {
  86. if(gpio_input_bit_get(gpio_periph,pin)==0)
  87. return KEY_ON;
  88. else
  89. return KEY_OFF;
  90. }
  91. unsigned char ReadKeyValue(void)
  92. {
  93. static unsigned char SlaveAddress;
  94. s_arrKeyDownLevel[0]=ScanKey(KEY_B_IO,KEY1);
  95. s_arrKeyDownLevel[1]=ScanKey(KEY_B_IO,KEY2);
  96. s_arrKeyDownLevel[2]=ScanKey(KEY_B_IO,KEY3);
  97. s_arrKeyDownLevel[3]=ScanKey(KEY_B_IO,KEY4);
  98. s_arrKeyDownLevel[4]=ScanKey(KEY_A_IO,KEY5);
  99. SlaveAddress=(~((s_arrKeyDownLevel[4]<<4)|(s_arrKeyDownLevel[3]<<3)|(s_arrKeyDownLevel[2]<<2)|(s_arrKeyDownLevel[1]<<1)|s_arrKeyDownLevel[0]))&0x1f;
  100. return SlaveAddress;
  101. }
  102. void EXTI2_3_IRQHandler(void)
  103. {
  104. #if SUPPORT_2_DETECTION
  105. if(RESET != exti_interrupt_flag_get(DERECTION_IO_2_EXIT))
  106. {
  107. key_detection_2 = 1;
  108. detection_count_2 = 0;
  109. exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
  110. }
  111. #endif
  112. }
  113. void EXTI4_15_IRQHandler(void){
  114. if(RESET != exti_interrupt_flag_get(DERECTION_IO_1_EXIT))
  115. {
  116. key_detection_1 = 1;
  117. detection_count_1 = 0;
  118. exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
  119. }
  120. }
  121. uint8_t get_detection(void)
  122. {
  123. uint8_t value = 0;
  124. #if SUPPORT_DETECTION
  125. if(key_detection_1 > 0)
  126. {
  127. #if SUPPORT_DRICION
  128. value |= (1 << 1);
  129. #else
  130. value |= (1 << 0);
  131. #endif
  132. }
  133. #if SUPPORT_2_DETECTION
  134. if(key_detection_2 > 0)
  135. {
  136. #if SUPPORT_DRICION
  137. value |= (1 << 0);
  138. #else
  139. value |= (1 << 1);
  140. #endif
  141. }
  142. #endif
  143. #endif
  144. return value;
  145. }