51单片机第5篇章-矩阵按键

213 阅读7分钟

追求以通俗的语言让小白可以更容易上手学单片机,在此同时巩固知识(很多图片来自江科大和普中,作者也是学这个来的,只是通过自己的语言想把它复述出来)。

矩阵按键是什么,和独立按键的区别?

矩阵按键在单片中的位置

微信图片_20221114225906.png 从外观上看,独立按键是4个,而矩阵按键是4X4=16个。

那有什么异同?

同:

都是按键触发,写一个监测按键的函数,当触发了哪个按键,就会有对应的执行操作/数据读入

异:

在单片机32个引脚中,独立按键这4个按键,有P3_0到P3_3四个输入端io口可进行匹配;
但是矩阵按键16个按键,若和独立按键一样,那会占据很多引脚,所以行不通,设计者将其输入端优化,16->8
如图,输入端为P1_0到P1_7

Snipaste_2022-11-14_23-12-29.png

Snipaste_2022-11-14_23-11-47.png

矩阵按键又是怎么实现功能的?

硬件角度

不同于独立按键,直接一个引脚控制一个按键,根据其触发波形的读取机制(代码),便可以独立判断一个按键是否触发了

独立按键的按动触发读取机制可以点这里看

矩阵按键的读取机制(扫描)如下:

关键--一个按键连接两个输入端,一端置低电平,按钮按下会导通,另一端就会为低电平。
这里只介绍一种扫描方法---一列一列扫描

微信图片_20221114235951.png 步骤如下

1. 令P1_3=0,同时P1_2,P1_1,P1_0都为1;此时第一行会为全都为0
2.从P1_7,P1_6,P1_5,P1_4按顺序一个一个判断,如果为0,则说明这个按键按下了
3.若2无法监测按下了按钮,则令P1_2为0,其他为1,再执行2.
4......依次进行,直到找到这个按键,就终止整个函数

代码设计逻辑思路

void Delay(time){//延时函数
while(time--) ;
}
unsigned char MatrixKey()
{
unsigned char KeyNumber=0;

P1=0xFF;//一开始把全部置高电平
P1_3=0;//从第一列开始扫描
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}

P1=0xFF;//再把全部置高电平,把上面的置0都清除
P1_2=0;//从第二列开始扫描
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}

P1=0xFF;//再把全部置高电平,把上面的置0都清除
P1_1=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}

P1=0xFF;//再把全部置高电平,把上面的置0都清除
P1_0=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}

return KeyNumber;//返回这个值
}

可能你还是不明白,假如我按的是第一行第二列,那按照代码不就会检测到第一行第一列的按键吗? 我当时也这样迷茫(甚至做这个文章的时候也是这样,整了一晚),后来我想了想,if(P1_7==0)应该是代表说 P1_3=0的同时,按下的第一行那个按键,促使(该按钮)导通,才能使得P1_7=0,(并不是说按下了按钮,整个第一行就都导通,要明确置0和按下按键是会带来不同的效果)不然可能像读者说的那样,无法理解。

矩阵按键实现功能

按键显示数字

主函数

#include <REGX52.H>
#include "LCD1602.h"//下面密码锁有该函数
#include "MatrixKey.h"//下面密码锁有该函数
unsigned char Key;
void main()
{

LCD_Init();
LCD_ShowString(1,1,"Hello World!");
while(1){
Key=MatrixKey();
if(Key){
LCD_ShowNum(2,1,Key,2);}
}
}

按键密码锁

主函数

     #include <regx52.h>
    #include "LCD1602.h"
    #include "MatrixKey.h"
    u8 key;
    u16  PassWord,count;
   void main() {

LCD_Init();
LCD_ShowString(1,1,"CODE");
while(1) {
	key=MatrixKey();
	if(key) {
		if(key<=10) {
			if(count<4) {
				PassWord*=10;
				PassWord+=key%10;
				count++;
			}
		}
		LCD_ShowNum(2,1,PassWord,4);
	}
	if(key==11) {
		if(PassWord==2345) {
			LCD_ShowString(1,11,"yes");
			count=0;
			PassWord=0;
			LCD_ShowNum(2,1,PassWord,4);
		} else {
			LCD_ShowString(1,11,"err");
			count=0;
			PassWord=0;
			LCD_ShowNum(2,1,PassWord,4);
		}
	}
	if(key==12) {
		count=0;
		PassWord=0;
		LCD_ShowNum(2,1,PassWord,4);
	}
}
}

LCD 关于LCD,后面我会补充其知识点,下面是封装了的功能(输入位置和值即可在LCD屏上显示)

  #include <REGX52.H>
//引脚配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0

//函数定义:
/**
  * @brief  LCD1602延时函数,12MHz调用可延时1ms
  * @param  无
  * @retval 无
  */
void LCD_Delay()
{
unsigned char i, j;

i = 2;
j = 239;
do
{
	while (--j);
        } while (--i);
}

/**
  * @brief  LCD1602写命令
  * @param  Command 要写入的命令
  * @retval 无
  */
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}

/**
  * @brief  LCD1602写数据
  * @param  Data 要写入的数据
  * @retval 无
  */
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}

/**
  * @brief  LCD1602设置光标位置
  * @param  Line 行位置,范围:1~2
  * @param  Column 列位置,范围:1~16
  * @retval 无
  */
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
	LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
	LCD_WriteCommand(0x80|(Column-1+0x40));
}
}

/**
  * @brief  LCD1602初始化函数
  * @param  无
  * @retval 无
  */
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}

/**
  * @brief  在LCD1602指定位置上显示一个字符
  * @param  Line 行位置,范围:1~2
  * @param  Column 列位置,范围:1~16
  * @param  Char 要显示的字符
  * @retval 无
  */
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}

/**
  * @brief  在LCD1602指定位置开始显示所给字符串
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  String 要显示的字符串
  * @retval 无
  */
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
    unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
	LCD_WriteData(String[i]);
}
}

/**
  * @brief  返回值=X的Y次方
  */
int LCD_Pow(int X,int Y)
{
    unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
	Result*=X;
}
return Result;
}

/**
  * @brief  在LCD1602指定位置开始显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~65535
  * @param  Length 要显示数字的长度,范围:1~5
  * @retval 无
  */
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
     unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
	LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}

/**
  * @brief  在LCD1602指定位置开始以有符号十进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:-32768~32767
  * @param  Length 要显示数字的长度,范围:1~5
  * @retval 无
  */
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
	LCD_WriteData('+');
	Number1=Number;
}
else
{
	LCD_WriteData('-');
	Number1=-Number;
}
for(i=Length;i>0;i--)
{
	LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
}
}

/**
  * @brief  在LCD1602指定位置开始以十六进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~0xFFFF
  * @param  Length 要显示数字的长度,范围:1~4
  * @retval 无
  */
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i,SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
	SingleNumber=Number/LCD_Pow(16,i-1)%16;
	if(SingleNumber<10)
	{
		LCD_WriteData(SingleNumber+'0');
	}
	else
	{
		LCD_WriteData(SingleNumber-10+'A');
	}
}
}

/**
  * @brief  在LCD1602指定位置开始以二进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~1111 1111 1111 1111
  * @param  Length 要显示数字的长度,范围:1~16
  * @retval 无
  */
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
	LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
}
}

矩阵按键

#include <REGX52.H>
void Delay(time){
while(time--) ;
}
unsigned char MatrixKey()
{
unsigned char KeyNumber=0;

P1=0xFF;
P1_3=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}

P1=0xFF;
P1_2=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}

P1=0xFF;
P1_1=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}

P1=0xFF;
P1_0=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}

return KeyNumber;
}