void LED_Init(void)
{
//GPIOF9初始化设置
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//LED对应IO口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化GPIO
GPIO_SetBits(GPIOF,GPIO_Pin_9);//GPIOF9设置高,灯灭
//GPIO_ResetBits(GPIOF,GPIO_Pin_9);//输出低电平,灯亮
}
void Key_InIt(void)
{
//GPIOF9初始化设置
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOF时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//LED对应IO口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIO
}
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;//按键按松开标志
if(mode)key_up=1; //支持连按
if(key_up&&(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0))
{
delay_ms(10);//去抖动
key_up=0;
if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0)return 1;
}else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==1)key_up=1;
return 0;// 无按键按下
}
int main(void)
{
u8 key;
u8 sw=0;
LED_Init();
Key_InIt(); //保存键值
delay_init(168); //初始化延时函数
while(1)
{
key=KEY_Scan(0); //得到键值
if(key)
{
if(sw==0)
{
GPIO_ResetBits(GPIOF,GPIO_Pin_9);//输出低电平,灯亮
sw=1;
}
else
{
GPIO_SetBits(GPIOF,GPIO_Pin_9);//GPIOF9设置高,灯灭
sw=0;
}
}else delay_ms(10);
}
}