/********************************************************************************************************* * Module : key.c * Abstract : key modules,key be used to determing modebus address and It content interfaces of scankey to read! * Version : 1.0.0 * Author : Leopul(COPYRIGHT 2025 - 2027 Leopul. All rights reserved.) * Complete : 2025-02-16 * Content : None * Note : None *********************************************************************************************************/ #include "Key.h" #include "gd32e230x_conf.h" #define KEY_NAME_MAX 5 #define KEY_ON 0 #define KEY_OFF 1 #define KEY1 GPIO_PIN_12 #define KEY2 GPIO_PIN_6 #define KEY3 GPIO_PIN_7 #define KEY4 GPIO_PIN_8 #define KEY5 GPIO_PIN_15 static unsigned char s_arrKeyDownLevel[KEY_NAME_MAX]; static void ConfigKeyGPIO(void); static void ConfigKeyGPIO(void) { rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_GPIOF); rcu_periph_clock_enable(RCU_GPIOB); gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY1); gpio_mode_set(GPIOF, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY2); gpio_mode_set(GPIOF, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY3); gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY4); gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY5); } void InitKey(void) { ConfigKeyGPIO(); } 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(GPIOA,KEY1); s_arrKeyDownLevel[1]=ScanKey(GPIOF,KEY2); s_arrKeyDownLevel[2]=ScanKey(GPIOF,KEY3); s_arrKeyDownLevel[3]=ScanKey(GPIOA,KEY4); s_arrKeyDownLevel[4]=ScanKey(GPIOB,KEY5); SlaveAddress=(~((s_arrKeyDownLevel[4]<<4)|(s_arrKeyDownLevel[3]<<3)|(s_arrKeyDownLevel[2]<<2)|(s_arrKeyDownLevel[1]<<1)|s_arrKeyDownLevel[0]))&0x1f; return SlaveAddress; }