Key.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*********************************************************************************************************
  2. * Module : key.c
  3. * Abstract : key modules,key be used to determing modebus address and It content interfaces of scankey to read!
  4. * Version : 1.0.0
  5. * Author : Leopul(COPYRIGHT 2025 - 2027 Leopul. All rights reserved.)
  6. * Complete : 2025-02-16
  7. * Content : None
  8. * Note : None
  9. *********************************************************************************************************/
  10. #include "Key.h"
  11. #include "gd32e230x_conf.h"
  12. #define KEY_NAME_MAX 5
  13. #define KEY_ON 0
  14. #define KEY_OFF 1
  15. #define KEY1 GPIO_PIN_12
  16. #define KEY2 GPIO_PIN_6
  17. #define KEY3 GPIO_PIN_7
  18. #define KEY4 GPIO_PIN_8
  19. #define KEY5 GPIO_PIN_15
  20. static unsigned char s_arrKeyDownLevel[KEY_NAME_MAX];
  21. static void ConfigKeyGPIO(void);
  22. static void ConfigKeyGPIO(void)
  23. {
  24. rcu_periph_clock_enable(RCU_GPIOA);
  25. rcu_periph_clock_enable(RCU_GPIOF);
  26. rcu_periph_clock_enable(RCU_GPIOB);
  27. gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY1);
  28. gpio_mode_set(GPIOF, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY2);
  29. gpio_mode_set(GPIOF, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY3);
  30. gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY4);
  31. gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY5);
  32. }
  33. void InitKey(void)
  34. {
  35. ConfigKeyGPIO();
  36. }
  37. unsigned char ScanKey(unsigned int gpio_periph,unsigned int pin)
  38. {
  39. if(gpio_input_bit_get(gpio_periph,pin)==0)
  40. return KEY_ON;
  41. else
  42. return KEY_OFF;
  43. }
  44. unsigned char ReadKeyValue(void)
  45. {
  46. static unsigned char SlaveAddress;
  47. s_arrKeyDownLevel[0]=ScanKey(GPIOA,KEY1);
  48. s_arrKeyDownLevel[1]=ScanKey(GPIOF,KEY2);
  49. s_arrKeyDownLevel[2]=ScanKey(GPIOF,KEY3);
  50. s_arrKeyDownLevel[3]=ScanKey(GPIOA,KEY4);
  51. s_arrKeyDownLevel[4]=ScanKey(GPIOB,KEY5);
  52. SlaveAddress=(~((s_arrKeyDownLevel[4]<<4)|(s_arrKeyDownLevel[3]<<3)|(s_arrKeyDownLevel[2]<<2)|(s_arrKeyDownLevel[1]<<1)|s_arrKeyDownLevel[0]))&0x1f;
  53. return SlaveAddress;
  54. }