#include "matrix_key.h"
#include "stm32f10x.h"
#include "delay.h"
void KEYPad_Init1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);//开启时钟,使能GPIOB时钟
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 B0-B3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉输入 F4-B7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
void KEYPad_Init2(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);//开启时钟,使能GPIOB时钟
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉输入 B0-B3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 B4-B7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
int KEYPAD_Scan()
{
static u16 keyvalue = 0;
u32 readvalue = 0;
KEYPad_Init1();
readvalue = GPIO_ReadInputData(GPIOF);
readvalue &= 0x00ff;
//return readvalue;
if(readvalue != 0x000f)
{
delay_ms(8);//消抖10ms
if(readvalue != 0x000f)
{
readvalue = GPIO_ReadInputData(GPIOF);//再次读取状态
readvalue &= 0x00ff;
//return readvalue;
switch(readvalue)
{
case (0x001f):keyvalue = 0;break;
case (0x002f):keyvalue = 1;break;
case (0x004f):keyvalue = 2;break;
case (0x008f):keyvalue = 3;break;
}
//return readvalue;
KEYPad_Init2();
delay_ms(5);
readvalue = GPIO_ReadInputData(GPIOF);//再次读取状态
readvalue |= 0x00f0;
readvalue &= 0x00ff;
//return readvalue;
switch(readvalue)
{
case (0x00f1):keyvalue += 0;break;
case (0x00f2):keyvalue += 4;break;
case (0x00f4):keyvalue += 8;break;
case (0x00f8):keyvalue += 12;break;
}
while(readvalue != 0x00f0)//检测按键松开
{
readvalue = GPIO_ReadInputData(GPIOF);//再次读取状态
readvalue |= 0x00f0;
readvalue &= 0x00ff;
}
//return readvalue;
}
}
return keyvalue;
}
|