1、矩阵按键扫描原理:
先是把列置0(推挽输出),行是输入上拉,扫描行得到行的键值;再是把行置0(推完输出),列是输入上拉,扫描列得到列的键值;最后把行列的键值相加得到最后的总的键值。
2、#include "passWD.c"
#include "passWD.h"
#include "usart.h"
#include "delay.h"
void PassWordClockInit1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_OType_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void PassWordClockInit2(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
int LineScan(void)
{
int num=0;
PassWordClockInit1();
if(LINE1==0)
{
delay_ms(25);
if(LINE1==0)
{
num=1;
}
}
else if(LINE2==0)
{
delay_ms(25);
if(LINE2==0)
{
num=2;
}
}
else if(LINE3==0)
{
delay_ms(25);
if(LINE3==0)
{
num=3;
}
}
else if(LINE4==0)
{
delay_ms(25);
if(LINE4==0)
{
num=4;
}
}
return num;
}
int RowScan(void)
{
int num=0;
PassWordClockInit2();
if(ROW1==0)
{
if(ROW1==0)
{
num=1;
while(ROW1==0);
}
}
else if(ROW2==0)
{
if(ROW2==0)
{
num=2;
while(ROW2==0);
}
}
else if(ROW3==0)
{
if(ROW3==0)
{
num=3;
while(ROW3==0);
}
}
return num;
}
int PassWordClockScan(void)
{
int line=LineScan();
int row=0;
if(line==0)
{
return 0;
}
else
{
row=RowScan();
}
return (line-1)*3+row;
}
3、#include "passWD.h"
#ifndef __PASSWD_H
#define __PASSWD_H
#include "stm32f4xx.h"
#define ROW1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_0)
#define ROW2 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_1)
#define ROW3 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)
#define LINE1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
#define LINE2 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define LINE3 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5)
#define LINE4 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_6)
void PassWordClockInit(void);
int LineScan(void);
int RowScan(void);
int PassWordClockScan(void);
#endif
4、main.c
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //延时初始化
uart_init(115200); //串口初始化波特率为115200
while(1)
{
int temp=PassWordClockScan();
if(temp)
{
printf("%d\r\n",temp);
}
}
}
5、密码锁代码(扩展)
char num[5][4]={ {'0','0','0','0'},{'0','1','2','3'},{'0','4','5','6'},{'0','7','8','9'},{'0','*','0','#'}};
void SetPassWord(char *passWd)
{
int passWdIndex=-1;
int line=LineScan();
int row=0;
do
{
while(!(line=LineScan()));//检测到有按键按下,则执行下面的语句
row=RowScan();
passWdIndex++;
passWd[passWdIndex]=num[line][row];
printf("%s\r\n",passWd);
}while(passWd[passWdIndex]!='#');//密码终结符号
passWd[passWdIndex]='\0';
}
main()
{
char prePassWd[20];
char passWd[20];
while(1)
{
printf("请输入您的密码\r\n");
SetPassWord(prePassWd);
printf("请再次输入您的密码\r\n");
SetPassWord(passWd);
int i=strcmp(prePassWd,passWd);
if(i==0)
{
printf("密码设置成功\r\n");
}
else
{
printf("您两次输入的密码不一致,密码设置失败\r\n");
}
}
}