4X4矩形键盘
准备
我选用的芯片是STM32F407的芯片,一个4X4的矩形键盘。
我们用杜邦线将键盘和芯片连接起来,因为我选引脚为了插起来方便,我选了以下引脚。
选用了左边的引脚(4-18),这样我们刚好一排8个引脚,接上8根杜邦线。
然后根据硬件原理图,看看分别对应芯片的哪个引脚。
我们不难发现,其中的8个引脚分别是
/*
PD6 PD7 PC6 PC8
PC11 PE5 PA6 PG9
*/
当你做到这里,你离成功已经不远了。
配置引脚
这八个引脚我们要把他分成两组(上面,我已经分成上下两组,因为这有关获取键盘的数值的原理)。
下面是代码
void Keyborad_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);//GPIOD组时钟
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; //引脚 6
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //输出速度
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOD, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; //引脚 7
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //输出速度
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOD, &GPIO_InitStruct);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_8; //引脚6 8
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //输出速度
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOC, &GPIO_InitStruct);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //GPIOC组时钟
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11; //引脚11
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //输出速度
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //下拉
GPIO_Init(GPIOC, &GPIO_InitStruct);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //GPIOE组时钟
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5; //引脚5
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //输出速度
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //下拉
GPIO_Init(GPIOE, &GPIO_InitStruct);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //GPIOA组时钟
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; //引脚6
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //输出速度
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //下拉
GPIO_Init(GPIOA, &GPIO_InitStruct);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE); //GPIOG组时钟
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; //引脚9
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //输出速度
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //下拉
GPIO_Init(GPIOG, &GPIO_InitStruct);
}
上面我们就用了库函数把8个引脚都配置好了,下面就是进行扫描。
u16 Key_scan(void)
{
u16 key_val=100; //初始化获取值可以自行设置 不设置为0是因为键盘有0
delay_us(15);
//--------------------------------------scan 1st
PDout(6) = 0; PDout(7) = 1; PCout(6) = 1; PCout(8) = 1;
if(PCin(11) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PCin(11) == 0)
delay_us(15); //消抖
key_val = 1;
}
if(PEin(5) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PEin(5) == 0)
delay_us(15); //消抖
key_val = 4;
}
if(PAin(6) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PAin(6) == 0)
delay_us(15); //消抖
key_val = 7;
}
if(PGin(9) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PGin(9) == 0)
delay_us(15); //消抖
key_val = 15; //*
}
//--------------------------------------scan 2st
PDout(6) = 1; PDout(7) = 0; PCout(6) = 1; PCout(8) = 1;
if(PCin(11) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PCin(11) == 0)
delay_us(15); //消抖
key_val = 2;
}
if(PEin(5) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PEin(5) == 0)
delay_us(15); //消抖
key_val = 5;
}
if(PAin(6) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PAin(6) == 0)
delay_us(15); //消抖
key_val = 8;
}
if(PGin(9) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PGin(9) == 0)
delay_us(15); //消抖
key_val = 0;
}
//--------------------------------------scan 3st
PDout(6) = 1; PDout(7) = 1; PCout(6) = 0; PCout(8) = 1;
if(PCin(11) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PCin(11) == 0)
delay_us(15); //消抖
key_val = 3;
}
if(PEin(5) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PEin(5) == 0)
delay_us(15); //消抖
key_val = 6;
}
if(PAin(6) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PAin(6) == 0)
delay_us(15); //消抖
key_val = 9;
}
if(PGin(9) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PGin(9) == 0)
delay_us(15); //消抖
key_val = 14;
}
//--------------------------------------scan 4st
PDout(6) = 1; PDout(7) = 1; PCout(6) = 1; PCout(8) = 0;
if(PCin(11) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PCin(11) == 0)
delay_us(15); //消抖
key_val = 10;
}
if(PEin(5) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PEin(5) == 0)
delay_us(15); //消抖
key_val = 11;
}
if(PAin(6) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PAin(6) == 0)
delay_us(15); //消抖
key_val = 12;
}
if(PGin(9) == 0)
{
delay_us(15); //消抖
Key_beep();
while(PGin(9) == 0)
delay_us(15); //消抖
key_val = 13;
}
return key_val;
}
这样我们就完成了4x4键盘的使用,接下来如何使用就看你们了。