STM32 库函数 3*4矩阵键盘

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");
		}
		
	}
	
}

相关推荐

  1. STM32 函数 3*4矩阵键盘

    2024-03-27 09:24:03       41 阅读
  2. STM32用HAL函数实现硬件IIC

    2024-03-27 09:24:03       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 09:24:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 09:24:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 09:24:03       87 阅读
  4. Python语言-面向对象

    2024-03-27 09:24:03       96 阅读

热门阅读

  1. Hive安装配置

    2024-03-27 09:24:03       34 阅读
  2. php获取文件列表(所有子目录文件)

    2024-03-27 09:24:03       44 阅读
  3. Docker搭建Etcd集群

    2024-03-27 09:24:03       45 阅读
  4. Spring Boot设置io临时目录

    2024-03-27 09:24:03       36 阅读
  5. go实现链表

    2024-03-27 09:24:03       38 阅读
  6. 计算机网络——网络基础1

    2024-03-27 09:24:03       40 阅读
  7. vue3 之 Pinia

    2024-03-27 09:24:03       35 阅读
  8. 第三十三章 配置服务器访问 - SSL TLS 参数

    2024-03-27 09:24:03       37 阅读
  9. React组件如何通信

    2024-03-27 09:24:03       40 阅读
  10. react setState函数的使用与异步更新

    2024-03-27 09:24:03       39 阅读
  11. React 18中hook函数详解之useState和useEffect

    2024-03-27 09:24:03       42 阅读
  12. LORA模型和稳定扩散模型的区别是什么?

    2024-03-27 09:24:03       38 阅读
  13. jetson-Ubuntu-指令

    2024-03-27 09:24:03       39 阅读