key.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "stm32f10x.h"
  2. #include "key.h"
  3. #include "sys.h"
  4. #include "board.h"
  5. //按键初始化函数
  6. void key_init(void) //IO初始化
  7. {
  8. GPIO_InitTypeDef GPIO_InitStructure;
  9. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_KEY1;
  10. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  11. GPIO_Init(BOARD_PORT_KEY1, &GPIO_InitStructure);//初始化
  12. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_KEY2;
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  14. GPIO_Init(BOARD_PORT_KEY2, &GPIO_InitStructure);//初始化
  15. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_KEY3;
  16. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  17. GPIO_Init(BOARD_PORT_KEY3, &GPIO_InitStructure);//初始化
  18. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_KEY4;
  19. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  20. GPIO_Init(BOARD_PORT_KEY4, &GPIO_InitStructure);//初始化
  21. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_KEY5;
  22. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  23. GPIO_Init(BOARD_PORT_KEY5, &GPIO_InitStructure);//初始化
  24. }
  25. key_value_te keyScan(void)
  26. {
  27. key_value_te keyReturn = KEY_VALUE_NULL;
  28. if (GPIO_ReadInputDataBit(BOARD_GPIO_KEY1) == 0)
  29. {
  30. keyReturn = KEY_VALUE_KEY1;
  31. }
  32. else if (GPIO_ReadInputDataBit(BOARD_GPIO_KEY2) == 0)
  33. {
  34. keyReturn = KEY_VALUE_KEY2;
  35. }
  36. else if (GPIO_ReadInputDataBit(BOARD_GPIO_KEY3) == 0)
  37. {
  38. keyReturn = KEY_VALUE_KEY3;
  39. }
  40. else if (GPIO_ReadInputDataBit(BOARD_GPIO_KEY4) == 0)
  41. {
  42. keyReturn = KEY_VALUE_KEY4;
  43. }
  44. else if (GPIO_ReadInputDataBit(BOARD_GPIO_KEY5) == 0)
  45. {
  46. keyReturn = KEY_VALUE_KEY5;
  47. }
  48. return keyReturn;// 无按键按下
  49. }