高中生
最后登录1970-1-1
在线时间 小时
注册时间2018-9-20
|

楼主 |
发表于 2018-9-29 15:06:11
|
显示全部楼层
本帖最后由 咸鱼之身 于 2018-9-29 15:07 编辑
#ifndef __KEY_H
#define __KEY_H
#include"stm32f4xx.h"
#endif /* _KEY_H */
//#define KEY1 GPIO_ReadInputDataBit(GPIOI,GPIO_Pin_8)
//#define KEY2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)
//#define KEY3 GPIO_ReadInputDataBit(GPIOI,GPIO_Pin_11)
#define KEY1_ON 0
#define KEY1_OFF 1
void KEY_Init(void);
uint8_t Key_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
——————————————————————————————————————
#include"key.h"
void Delay(uint32_t t)
{
while(t!=0)
t--;
}
void KEY1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOI, &GPIO_InitStructure);
}
uint8_t Key_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)==KEY1_ON)
{
Delay(0xFFF); // 消抖
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)==KEY1_ON)
{
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)==KEY1_ON);
return KEY1_ON;
}
else return KEY1_OFF;
}
else return KEY1_OFF;
}
_____________________________________________________________________
#include"stm32f4xx.h"
#include "led.h"
#include "key.h"
int main(void)
{
Init_LED1();
KEY1_Init();
while(1)
{
if( Key_Scan(GPIOI,GPIO_Pin_8)= KEY1_ON )
LED1_ON;
}
}
|
|