使用其中的KEY和LED例程,功能正常。但是增加3个按键KEY4,KEY5,KEY6,分别定义如下,扫描的键值不便,请大家指点下吧,谢谢。
/*********************************************************************************************************
¶¨Òå¿ØÖÆKEYµÄGPIO¶Ë¿ÚºÍ¹Ü½Å
*********************************************************************************************************/
static GPIO_TypeDef *KEY1_PORT;
static uint16_t KEY1_PIN;
static GPIO_TypeDef *KEY2_PORT;
static uint16_t KEY2_PIN;
static GPIO_TypeDef *KEY3_PORT;
static uint16_t KEY3_PIN;
static GPIO_TypeDef *KEY4_PORT;
static uint16_t KEY4_PIN;
static GPIO_TypeDef *KEY5_PORT;
static uint16_t KEY5_PIN;
static GPIO_TypeDef *KEY6_PORT;
static uint16_t KEY6_PIN;
/*********************************************************************************************************
¶¨ÒåKEY
*********************************************************************************************************/
#define KEY1 0x01
#define KEY2 0x02
#define KEY3 0x04
#define KEY4 0x08
#define KEY5 0x10
#define KEY6 0x20
/*********************************************************************************************************
KEY³õʼ»¯
*********************************************************************************************************/
void KeyInit(uint8_t KEY, GPIO_TypeDef *GPIOx, uint16_t PINn)
{
switch (KEY)
{
case KEY1:
KEY1_PORT = GPIOx;
KEY1_PIN = PINn;
ValidFlag |= KEY1;
gpioPeriphEnable(KEY1_PORT);
gpioModeConfig(KEY1_PORT, KEY1_PIN, GPIO_MODE_IN_PU);
break;
case KEY2:
KEY2_PORT = GPIOx;
KEY2_PIN = PINn;
ValidFlag |= KEY2;
gpioPeriphEnable(KEY2_PORT);
gpioModeConfig(KEY2_PORT, KEY2_PIN, GPIO_MODE_IN_PU);
break;
case KEY3:
KEY3_PORT = GPIOx;
KEY3_PIN = PINn;
ValidFlag |= KEY3;
gpioPeriphEnable(KEY3_PORT);
gpioModeConfig(KEY3_PORT, KEY3_PIN, GPIO_MODE_IN_PU);
break;
case KEY4:
KEY4_PORT = GPIOx;
KEY4_PIN = PINn;
ValidFlag |= KEY4;
gpioPeriphEnable(KEY4_PORT);
gpioModeConfig(KEY4_PORT, KEY4_PIN, GPIO_MODE_IN_PU);
break;
case KEY5:
KEY5_PORT = GPIOx;
KEY5_PIN = PINn;
ValidFlag |= KEY5;
gpioPeriphEnable(KEY5_PORT);
gpioModeConfig(KEY5_PORT, KEY5_PIN, GPIO_MODE_IN_PU);
break;
case KEY6:
KEY6_PORT = GPIOx;
KEY6_PIN = PINn;
ValidFlag |= KEY6;
gpioPeriphEnable(KEY6_PORT);
gpioModeConfig(KEY6_PORT, KEY6_PIN, GPIO_MODE_IN_PU);
break;
default:
break;
}
}
/*********************************************************************************************************
Åж¨KEYÊÇ·ñÓÐЧ£¨Ö»Óгõʼ»¯Ö®ºó²Å»áÉúЧ£©
*********************************************************************************************************/
#define KeyIsValid(KEY) (ValidFlag & (KEY))
/*********************************************************************************************************
¶ÁÈ¡KEYÖµ
*********************************************************************************************************/
uint8_t KeyGet(void)
{
uint8_t key = 0x00;
if (KeyIsValid(KEY1))if (!gpioPinRead(KEY1_PORT, KEY1_PIN)) key |= KEY1;
if (KeyIsValid(KEY2))if (!gpioPinRead(KEY2_PORT, KEY2_PIN)) key |= KEY2;
if (KeyIsValid(KEY3))if (!gpioPinRead(KEY3_PORT, KEY3_PIN)) key |= KEY3;
if (KeyIsValid(KEY4))if (!gpioPinRead(KEY4_PORT, KEY4_PIN)) key |= KEY4;
if (KeyIsValid(KEY5))if (!gpioPinRead(KEY5_PORT, KEY5_PIN)) key |= KEY5;
if (KeyIsValid(KEY6))if (!gpioPinRead(KEY6_PORT, KEY6_PIN)) key |= KEY6;
return (key);
}
不知道这么改动错在哪里了? |