Key.c 788 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "stm32f10x.h" // Device header
  2. #include "Delay.h"
  3. void Key_Init(void)
  4. {
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
  9. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  10. GPIO_Init(GPIOB, &GPIO_InitStructure);
  11. }
  12. uint8_t Key_GetNum(void)
  13. {
  14. uint8_t KeyNum = 0;
  15. if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
  16. {
  17. Delay_ms(20);
  18. while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);
  19. Delay_ms(20);
  20. KeyNum = 1;
  21. }
  22. if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
  23. {
  24. Delay_ms(20);
  25. while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);
  26. Delay_ms(20);
  27. KeyNum = 2;
  28. }
  29. return KeyNum;
  30. }