Key.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "Key.h"
  2. #include "gd32e23x_rcu.h"
  3. static unsigned char s_arrKeyDownLevel[KEY_NAME_MAX] = {0};
  4. static void ConfigKeyGPIO(void);
  5. static void ConfigKeyGPIO(void)
  6. {
  7. rcu_periph_clock_enable(KEY_A_RCU);
  8. rcu_periph_clock_enable(KEY_B_RCU);
  9. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY1);
  10. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY2);
  11. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY3);
  12. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY4);
  13. gpio_mode_set(KEY_A_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY5);
  14. }
  15. void InitKey(void)
  16. {
  17. ConfigKeyGPIO();
  18. }
  19. unsigned char ScanKey(unsigned int gpio_periph,unsigned int pin)
  20. {
  21. if(gpio_input_bit_get(gpio_periph,pin)==0)
  22. return KEY_ON;
  23. else
  24. return KEY_OFF;
  25. }
  26. unsigned char ReadKeyValue(void)
  27. {
  28. static unsigned char SlaveAddress;
  29. s_arrKeyDownLevel[0] = ScanKey(KEY_B_IO,KEY1);
  30. s_arrKeyDownLevel[1] = ScanKey(KEY_B_IO,KEY2);
  31. s_arrKeyDownLevel[2] = ScanKey(KEY_B_IO,KEY3);
  32. s_arrKeyDownLevel[3] = ScanKey(KEY_B_IO,KEY4);
  33. s_arrKeyDownLevel[4] = ScanKey(KEY_A_IO,KEY5);
  34. SlaveAddress=(~((s_arrKeyDownLevel[0]<<0)|(s_arrKeyDownLevel[1]<<1)|(s_arrKeyDownLevel[2]<<2)|(s_arrKeyDownLevel[3]<<3)|(s_arrKeyDownLevel[4]<<4)))& 0x1f;
  35. return SlaveAddress;
  36. }