–Key.h
#ifndef __Key_h
#define __Key_h
#include "Led.h"
#define KEY1 GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_8)
#define KEY2 GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_9)
#define KEY3 GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_10)
#define KEY4 GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_11)
#define KVALUE1 LED1 = ~LED1_IN
#define KVALUE2 LED2 = ~LED2_IN
#define KVALUE3 LED3 = ~LED3_IN
#define KVALUE4 LED4 = ~LED4_IN
void Scan_Key_Configuration(void);
int Key_Scan();
void Key_Delay(int ms);
#endif
–Key.c
#include <stdio.h>
#include "stm32f10x.h"
#include "Key.h"
void Scan_Key_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
| GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//上拉输入模式
GPIO_Init(GPIOD ,&GPIO_InitStructure);
}
int Key_Scan()
{
if(KEY1 == 0 || KEY2 == 0 || KEY3 == 0 || KEY4 == 0)
//如果按下键盘
{
Key_Delay(2);
if(KEY1 == 0)
{
while(!KEY1);
//去抖
return KVALUE1;
}
else if(KEY2 == 0)
{
while(!KEY2);
return KVALUE2;
}
else if(KEY3 == 0)
{
while(!KEY3);
return KVALUE3;
}
else if(KEY4 == 0)
{
while(!KEY4);
return KVALUE4;
}
}else if((KEY1 == 1) && (KEY2 == 1) && (KEY3 == 1) && (KEY4 == 1))
{
return 0;
}
}
void Key_Delay(int ms)
{
int i = 0;
int j = 0;
for( i = 0 ; i < 100 ; i++)
for(j = 0 ; j < ms ; j++);
}
void Set_LED_Reverse(int t)
{
if(t == 1) LED1 = ~LED1_IN;
//小灯亮灭
else if(t == 2) LED2 = ~LED2_IN;
else if(t == 3) LED3 = ~LED3_IN;
else if(t == 4) LED4 = ~LED4_IN;
else if(t == 0) return;
}