- /*=====================key.h=======================*/
- #ifndef __KEY_H
- #define __KEY_H
- #include "stm32f10x.h"
- //Key 引脚定义
- #define KEY1_GPIO_CLK RCC_APB2Periph_GPIOA
- #define KEY1_GPIO_PORT GPIOA
- #define KEY1_GPIO_PIN GPIO_Pin_0
- #define KEY_ON 1 //按键按下
- #define KEY_OFF 0 //按时松开
- void Key_GPIO_Config(void);
- uint8_t Key_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);
- #endif /* __KEY_H */
- /*=====================key.c=======================*/
- #include "key.h"
- void Key_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /*开启按键端口的时钟*/
- RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK,ENABLE);
- //选择按键Key1的引脚
- GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
- // 设置按键Key1的引脚为浮空输入
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- //使用结构体初始化按键
- GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
- }
- /*====================按键检测函数===================*/
- uint8_t Key_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
- {
- /*检测是否有按键按下 */
- if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON )
- {
- /*等待按键释放 */
- while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON);
- return KEY_ON;
- }
- else
- return KEY_OFF;
- }
|