4X4 rectangular keyboard
ready
The chip I chose is STM32F407, a 4X4 rectangular keyboard.
We use DuPont cable to connect the keyboard and the chip, because I choose the pins for easy plugging, I chose the following pins.
The pins on the left (4-18) are selected, so that we have exactly 8 pins in a row and 8 DuPont wires are connected.
Then according to the hardware schematic diagram, see which pin corresponds to the chip.
It’s not difficult to find that the 8 pins are
/*
PD6 PD7 PC6 PC8
PC11 PE5 PA6 PG9
*/
When you do this, you are not far from success.
Configuration pin
We have to divide these eight pins into two groups (above, I have divided them into upper and lower two groups, because this is related to the principle of obtaining keyboard values).
Below is the code
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);
}
Above we used the library function to configure all 8 pins, and the following is to scan.
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;
}
So we have completed the use of the 4x4 keyboard, and how to use it next is up to you.