| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #include "Key.h"
- #include "SysTick.h"
- #include "gd32e230x_conf.h"
- #define KEY_NAME_MAX 5
- #define KEY_A_RCU RCU_GPIOA
- #define KEY_A_IO GPIOA
- #define KEY_B_RCU RCU_GPIOB
- #define KEY_B_IO GPIOB
- #define KEY_F_RCU RCU_GPIOF
- #define KEY_F_IO GPIOF
- #define KEY_ON 0
- #define KEY_OFF 1
- #define KEY1 GPIO_PIN_4
- #define KEY2 GPIO_PIN_5
- #define KEY3 GPIO_PIN_6
- #define KEY4 GPIO_PIN_7
- #define KEY5 GPIO_PIN_15
- #define DERECTION_GPIO_1 GPIOA
- #define DERECTION_IO_1 GPIO_PIN_11
- #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOA
- #define DERECTION_IO_1_PIN DERECTION_IO_11
- #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN11
- #define DERECTION_IO_1_EXIT EXTI_11
-
- #if (SUPPORT_2_DETECTION)
- #define DERECTION_GPIO_2 GPIOB
- #define DERECTION_IO_2 GPIO_PIN_3
- #define DERECTION_IO_2_RCU EXTI_SOURCE_GPIOB
- #define DERECTION_IO_2_PIN DERECTION_IO_3
- #define DERECTION_IO_2_SOURCE EXTI_SOURCE_PIN3
- #define DERECTION_IO_2_EXIT EXTI_3
- #endif
- //#if SUPPORT_DETECTION
- uint8_t key_detection_1 = 0;
- uint8_t key_detection_2 = 0;
- //#endif
- extern uint16_t detection_count_1;
- extern uint16_t detection_count_2;
- static unsigned char s_arrKeyDownLevel[KEY_NAME_MAX]; //使用前要在InitKey函数中进行初始化
- static void ConfigKeyGPIO(void); //配置按键的GPIO
- static void ConfigDetection(void);
- static void ConfigKeyGPIO(void)
- {
- //使能RCU相关时钟
- rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA的时钟
- rcu_periph_clock_enable(RCU_GPIOF); //使能GPIOF的时钟
- rcu_periph_clock_enable(RCU_GPIOB);
- gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY1); //配置PF7为上拉输入K1
- gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY2); //配置PF6为上拉输入K2
- gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY3); //配置PA12为上拉输入K3
- gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY4); //配置PA12为上拉输入K3
- gpio_mode_set(KEY_A_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY5); //配置PA12为上拉输入K3
- }
- static void ConfigDetection(void)
- {
- #if SUPPORT_DETECTION
- rcu_periph_clock_enable(RCU_CFGCMP);
- rcu_periph_clock_enable(RCU_GPIOA);
- #if SUPPORT_2_DETECTION
- rcu_periph_clock_enable(RCU_GPIOB);
- #endif
- //config io
- gpio_mode_set(DERECTION_GPIO_1, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_1);
- #if SUPPORT_2_DETECTION
- gpio_mode_set(DERECTION_GPIO_2, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_2);
- #endif
- nvic_irq_enable(EXTI4_15_IRQn,2);
- syscfg_exti_line_config(DERECTION_IO_1_RCU,DERECTION_IO_1_SOURCE);
- exti_init(DERECTION_IO_1_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
- exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
-
- //config interrupt
- #if SUPPORT_2_DETECTION
- nvic_irq_enable(EXTI2_3_IRQn,2);
- syscfg_exti_line_config(DERECTION_IO_2_RCU,DERECTION_IO_2_SOURCE);
- exti_init(DERECTION_IO_2_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
- exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
- #endif
-
- #endif
- }
- void InitKey(void)
- {
- ConfigKeyGPIO(); //配置按键的GPIO
- ConfigDetection();
- }
- unsigned char ScanKey(unsigned int gpio_periph,unsigned int pin)
- {
- if(gpio_input_bit_get(gpio_periph,pin)==0)
- return KEY_ON;
- else
- return KEY_OFF;
- }
- unsigned char ReadKeyValue(void)
- {
- static unsigned char SlaveAddress;
- s_arrKeyDownLevel[0]=ScanKey(KEY_B_IO,KEY1);
- s_arrKeyDownLevel[1]=ScanKey(KEY_B_IO,KEY2);
- s_arrKeyDownLevel[2]=ScanKey(KEY_B_IO,KEY3);
- s_arrKeyDownLevel[3]=ScanKey(KEY_B_IO,KEY4);
- s_arrKeyDownLevel[4]=ScanKey(KEY_A_IO,KEY5);
- SlaveAddress=(~((s_arrKeyDownLevel[4]<<4)|(s_arrKeyDownLevel[3]<<3)|(s_arrKeyDownLevel[2]<<2)|(s_arrKeyDownLevel[1]<<1)|s_arrKeyDownLevel[0]))&0x1f;
- return SlaveAddress;
- }
- void EXTI2_3_IRQHandler(void)
- {
- #if SUPPORT_2_DETECTION
- if(RESET != exti_interrupt_flag_get(DERECTION_IO_2_EXIT))
- {
- key_detection_2 = 1;
- detection_count_2 = 0;
- exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
- }
- #endif
- }
- void EXTI4_15_IRQHandler(void){
-
- if(RESET != exti_interrupt_flag_get(DERECTION_IO_1_EXIT))
- {
- key_detection_1 = 1;
- detection_count_1 = 0;
- exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
- }
- }
- uint8_t get_detection(void)
- {
- uint8_t value = 0;
- #if SUPPORT_DETECTION
-
- if(key_detection_1 > 0)
- {
- #if SUPPORT_DRICION
- value |= (1 << 1);
- #else
- value |= (1 << 0);
- #endif
- }
- #if SUPPORT_2_DETECTION
- if(key_detection_2 > 0)
- {
- #if SUPPORT_DRICION
- value |= (1 << 0);
- #else
- value |= (1 << 1);
- #endif
- }
- #endif
- #endif
- return value;
- }
|