|
|
@@ -1,5 +1,6 @@
|
|
|
#include "Key.h"
|
|
|
#include "SysTick.h"
|
|
|
+#include "gd32e230x_conf.h"
|
|
|
|
|
|
#define KEY_NAME_MAX 5
|
|
|
|
|
|
@@ -30,23 +31,64 @@
|
|
|
#define DERECTION_GPIO GPIOB
|
|
|
#define DERECTION_IO_1 GPIO_PIN_8
|
|
|
#define DERECTION_IO_2 GPIO_PIN_9
|
|
|
+
|
|
|
+ #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOB
|
|
|
+ #define DERECTION_IO_1_PIN DERECTION_IO_8
|
|
|
+ #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN8
|
|
|
+ #define DERECTION_IO_1_EXIT EXTI_8
|
|
|
+
|
|
|
+ #define DERECTION_IO_2_RCU EXTI_SOURCE_GPIOB
|
|
|
+ #define DERECTION_IO_2_PIN DERECTION_IO_9
|
|
|
+ #define DERECTION_IO_2_SOURCE EXTI_SOURCE_PIN9
|
|
|
+ #define DERECTION_IO_2_EXIT EXTI_9
|
|
|
+
|
|
|
#elif((RelaySlaveChaNum <= 4) && (!SUPPORT_TWIN_FIRE))
|
|
|
#define DERECTION_GPIO GPIOB
|
|
|
#define DERECTION_IO_1 GPIO_PIN_10
|
|
|
+
|
|
|
+ #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOB
|
|
|
+ #define DERECTION_IO_1_PIN DERECTION_IO_1
|
|
|
+ #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN10
|
|
|
+ #define DERECTION_IO_1_EXIT EXTI_10
|
|
|
+
|
|
|
#elif ((RelaySlaveChaNum == 8) && (SUPPORT_2_DETECTION))
|
|
|
#define DERECTION_GPIO GPIOB
|
|
|
#define DERECTION_IO_1 GPIO_PIN_1
|
|
|
#define DERECTION_IO_2 GPIO_PIN_2
|
|
|
+
|
|
|
+ #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOB
|
|
|
+ #define DERECTION_IO_1_PIN DERECTION_IO_1
|
|
|
+ #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN1
|
|
|
+ #define DERECTION_IO_1_EXIT EXTI_1
|
|
|
+
|
|
|
+ #define DERECTION_IO_2_RCU EXTI_SOURCE_GPIOB
|
|
|
+ #define DERECTION_IO_2_PIN DERECTION_IO_2
|
|
|
+ #define DERECTION_IO_2_SOURCE EXTI_SOURCE_PIN2
|
|
|
+ #define DERECTION_IO_2_EXIT EXTI_2
|
|
|
+
|
|
|
#else
|
|
|
#define DERECTION_GPIO GPIOA
|
|
|
#define DERECTION_IO_1 GPIO_PIN_1
|
|
|
+ #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOA
|
|
|
+ #define DERECTION_IO_1_PIN DERECTION_IO_1
|
|
|
+ #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN1
|
|
|
+ #define DERECTION_IO_1_EXIT EXTI_1
|
|
|
+#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)
|
|
|
{
|
|
|
@@ -54,30 +96,63 @@ static void ConfigKeyGPIO(void)
|
|
|
rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA的时钟
|
|
|
rcu_periph_clock_enable(RCU_GPIOF); //使能GPIOF的时钟
|
|
|
rcu_periph_clock_enable(RCU_GPIOB);
|
|
|
-
|
|
|
-
|
|
|
gpio_mode_set(KEY_F_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY1); //配置PF7为上拉输入K1
|
|
|
gpio_mode_set(KEY_F_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY2); //配置PF6为上拉输入K2
|
|
|
gpio_mode_set(KEY_A_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY3); //配置PA12为上拉输入K3
|
|
|
gpio_mode_set(KEY_A_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY4); //配置PA12为上拉输入K3
|
|
|
gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY5);
|
|
|
+}
|
|
|
+
|
|
|
+static void ConfigDetection(void)
|
|
|
+{
|
|
|
#if SUPPORT_DETECTION
|
|
|
+ rcu_periph_clock_enable(RCU_CFGCMP);
|
|
|
+ #if (RelaySlaveChaNum == 6)
|
|
|
+ rcu_periph_clock_enable(RCU_GPIOB);
|
|
|
+ #elif((RelaySlaveChaNum <= 4) && (!SUPPORT_TWIN_FIRE))
|
|
|
+ rcu_periph_clock_enable(RCU_GPIOB);
|
|
|
+ #elif ((RelaySlaveChaNum == 8) && (SUPPORT_2_DETECTION))
|
|
|
+ rcu_periph_clock_enable(RCU_GPIOB);
|
|
|
+ #else
|
|
|
+ rcu_periph_clock_enable(RCU_GPIOA);
|
|
|
+ #endif
|
|
|
+ //config io
|
|
|
#if (RelaySlaveChaNum == 6 || ((RelaySlaveChaNum == 8) && SUPPORT_2_DETECTION))
|
|
|
- gpio_mode_set(DERECTION_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_1);
|
|
|
gpio_mode_set(DERECTION_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_2);
|
|
|
- #else
|
|
|
+ #endif
|
|
|
gpio_mode_set(DERECTION_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_1);
|
|
|
+
|
|
|
+ //config interrupt
|
|
|
+ #if (RelaySlaveChaNum == 6)
|
|
|
+ nvic_irq_enable(EXTI4_15_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);
|
|
|
+ #elif((RelaySlaveChaNum <= 4) && (!SUPPORT_TWIN_FIRE))
|
|
|
+ nvic_irq_enable(EXTI4_15_IRQn,2);
|
|
|
+ #elif ((RelaySlaveChaNum == 8) && (SUPPORT_2_DETECTION))
|
|
|
+ nvic_irq_enable(EXTI0_1_IRQn,2);
|
|
|
+ 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);
|
|
|
+ #else
|
|
|
+ nvic_irq_enable(EXTI0_1_IRQn,2);
|
|
|
#endif
|
|
|
-#endif
|
|
|
|
|
|
+ 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);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
+
|
|
|
void InitKey(void)
|
|
|
{
|
|
|
- ConfigKeyGPIO(); //配置按键的GPIO
|
|
|
+ ConfigKeyGPIO(); //配置按键的GPIO
|
|
|
+ ConfigDetection();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
unsigned char ScanKey(unsigned int gpio_periph,unsigned int pin)
|
|
|
{
|
|
|
if(gpio_input_bit_get(gpio_periph,pin)==0)
|
|
|
@@ -97,34 +172,66 @@ unsigned char ReadKeyValue(void)
|
|
|
SlaveAddress=(~((s_arrKeyDownLevel[4]<<4)|(s_arrKeyDownLevel[3]<<3)|(s_arrKeyDownLevel[2]<<2)|(s_arrKeyDownLevel[1]<<1)|s_arrKeyDownLevel[0]))&0x1f;
|
|
|
return SlaveAddress;
|
|
|
}
|
|
|
-#if SUPPORT_DETECTION
|
|
|
|
|
|
-unsigned char get_detection()
|
|
|
+
|
|
|
+
|
|
|
+void EXTI0_1_IRQHandler(void)
|
|
|
{
|
|
|
- uint8_t value = 0;
|
|
|
- int count = 50;
|
|
|
- #if ((RelaySlaveChaNum == 6) || ((RelaySlaveChaNum == 8) && SUPPORT_2_DETECTION)) //six relay board
|
|
|
- if(gpio_input_bit_get(DERECTION_GPIO,DERECTION_IO_1)==0)
|
|
|
- value |=(1<<0);
|
|
|
- if(gpio_input_bit_get(DERECTION_GPIO,DERECTION_IO_2)==0)
|
|
|
- value |=(1<<1);
|
|
|
- #else
|
|
|
- if(gpio_input_bit_get(DERECTION_GPIO,DERECTION_IO_1)==0){
|
|
|
- while(count --);
|
|
|
- if(gpio_input_bit_get(DERECTION_GPIO,DERECTION_IO_1)==0)
|
|
|
- {
|
|
|
- value |=(1<<0);
|
|
|
- }
|
|
|
- }else
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void EXTI2_3_IRQHandler(void)
|
|
|
+{
|
|
|
+#if ((RelaySlaveChaNum == 8) && (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);
|
|
|
+ }
|
|
|
+#if (RelaySlaveChaNum == 6)
|
|
|
+ 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
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+uint8_t get_detection(void)
|
|
|
+{
|
|
|
+ uint8_t value = 0;
|
|
|
+#if SUPPORT_DETECTION
|
|
|
+ if(key_detection_1 > 0)
|
|
|
{
|
|
|
- DelayNms(3);
|
|
|
- if(gpio_input_bit_get(DERECTION_GPIO,DERECTION_IO_1)==0)
|
|
|
- {
|
|
|
- value |=(1<<0);
|
|
|
- }
|
|
|
+ value |= (1 << 0);
|
|
|
}
|
|
|
- #endif
|
|
|
+ if(key_detection_2 > 0)
|
|
|
+ {
|
|
|
+ value |= (1 << 1);
|
|
|
+ }
|
|
|
+#endif
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
-#endif
|
|
|
+
|
|
|
+
|