矩陣按鍵
一、矩陣按鍵基礎(chǔ)知識
-
矩陣按鍵工作原理:
-
逐行掃描:通過高四位輪流輸出低電平來對矩陣鍵盤進(jìn)行掃描,當(dāng)?shù)退奈唤邮盏降臄?shù)據(jù)不全為1的時候,說明有按鍵按下,然后通過判斷低四位數(shù)據(jù)中哪一位為零來判斷哪一個按鍵被按下。
-
逐列掃描:通過低四位輪流輸出低電平來對矩陣鍵盤進(jìn)行掃描,當(dāng)?shù)退奈唤邮盏降臄?shù)據(jù)不全為1的時候,說明有按鍵按下,然后通過判斷高四位數(shù)據(jù)中哪一位為零來判斷哪一個按鍵被按下。
-
行列掃描:高四位全部輸出低電平,低四位輸出高電平。當(dāng)接收到數(shù)據(jù),低四位不全部為高電平時,說明有按鍵按下,然后通過接收的數(shù)據(jù)值,判斷哪一列有按鍵按下,然后再反過來,高四位輸出高電平,低四位輸出低電平,然后根據(jù)接收到的高四位的值判斷是哪一行有按鍵按下,這樣就能確定是哪一個按鍵被按下。
-
-
矩陣按鍵原理圖:
-
LCD1602原理圖
二、矩陣按鍵系列代碼
1. 矩陣按鍵操作(顯示數(shù)字)
(1)仿真電路圖
(2)源代碼
LCD1602.c
#include <REGX52.H>
#include "LCD1602.h"
//引腳配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
//函數(shù)定義:
/**
* @brief LCD1602延時函數(shù),12MHz調(diào)用可延時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寫數(shù)據(jù)
* @param Data 要寫入的數(shù)據(jù)
* @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設(shè)置光標(biāo)位置
* @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初始化函數(shù)
* @param 無
* @retval 無
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位數(shù)據(jù)接口,兩行顯示,5*7點陣
LCD_WriteCommand(0x0c);//顯示開,光標(biāo)關(guān),閃爍關(guān)
LCD_WriteCommand(0x06);//數(shù)據(jù)讀寫操作后,光標(biāo)自動加一,畫面不動
LCD_WriteCommand(0x01);//光標(biāo)復(fù)位,清屏
}
/**
* @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指定位置開始顯示所給數(shù)字
* @param Line 起始行位置,范圍:1~2
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~65535
* @param Length 要顯示數(shù)字的長度,范圍: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指定位置開始以有符號十進(jìn)制顯示所給數(shù)字
* @param Line 起始行位置,范圍:1~2
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:-32768~32767
* @param Length 要顯示數(shù)字的長度,范圍: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指定位置開始以十六進(jìn)制顯示所給數(shù)字
* @param Line 起始行位置,范圍:1~2
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~0xFFFF
* @param Length 要顯示數(shù)字的長度,范圍: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指定位置開始以二進(jìn)制顯示所給數(shù)字
* @param Line 起始行位置,范圍:1~2
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~1111 1111 1111 1111
* @param Length 要顯示數(shù)字的長度,范圍: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');
}
}
LCD1602.h
#ifndef __LCD1602_H__
#define __LCD1602_H__
//用戶調(diào)用函數(shù):
void LCD_Init();
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
#endif
Delay.c
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
Delay.h
#ifndef __DELAY_H__
#define __DELAY_H__
void Delay(unsigned int xms);
#endif
MatrixKey.c
#include <REGX52.H>
#include "Delay.h"
/**
* @brief 矩陣鍵盤讀取按鍵鍵碼
* @param 無
* @retval KeyNumber 按下按鍵的鍵碼值
如果按鍵按下不放,程序會停留在此函數(shù),松手的一瞬間,返回按鍵鍵碼,沒有按鍵按下時,返回0
*/
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;
}
MatrixKey.h
#ifndef __MATRIXKEY_H__
#define __MATRIXKEY_H__
unsigned char MatrixKey();
#endif
main.c
文章來源地址http://www.zghlxwxcb.cn/news/detail-733776.html
#include <REGX52.H>
#include "LCD1602.h"
#include "MatrixKey.h"
unsigned char KeyNum;
void main()
{
LCD_Init();
LCD_ShowString(1,5,"20230423");
while(1)
{
KeyNum = MatrixKey();
if(KeyNum)
{
LCD_ShowNum(2,1,KeyNum,2);
}
}
}
(3)實驗結(jié)果
2. 矩陣按鍵操作(控制數(shù)碼管)
(1)仿真電路圖
(2)源代碼
LCD1602.c
:與上述同名文件相同
LCD1602.h
:與上述同名文件相同
Delay.c
: 與上述同名文件相同
Delay.h
:與上述同名文件相同
MatrixKey.c
: 與上述同名文件相同
MatrixKey.h
:與上述同名文件相同文章來源:http://www.zghlxwxcb.cn/news/detail-733776.html
smg.c
#include "smg.h"
#include <REGX52.H>
#include "Delay.h"
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned int smgduan[]={0x00,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void smg_duan(unsigned char lsa,unsigned char lsb,unsigned char lsc)
{
LSA = lsa;
LSB = lsb;
LSC = lsc;
}
void DigDisplay()
{
unsigned int i;
for(i = 0;i < 8;i++)
{
switch(i)
{
case(0):
LSA = 0; LSB = 0; LSC = 0; P3 = smgduan[9]; break;
case(1):
LSA = 1; LSB = 0; LSC = 0; P3 = smgduan[1]; break;
case(2):
LSA = 0; LSB = 1; LSC = 0; P3 = smgduan[4]; break;
case(3):
LSA = 1; LSB = 1; LSC = 0; P3 = smgduan[0]; break;
case(4):
LSA = 0; LSB = 0; LSC = 1; P3 = smgduan[3]; break;
case(5):
LSA = 1; LSB = 0; LSC = 1; P3 = smgduan[2]; break;
case(6):
LSA = 0; LSB = 1; LSC = 1; P3 = smgduan[0]; break;
case(7):
LSA = 1; LSB = 1; LSC = 1; P3 = smgduan[2]; break;
}
Delay(20);
P3 = 0x00;
}
}
smg.h
#ifndef __SMG_H__
#define __SMG_H__
extern unsigned int smgduan[];
void smg_duan(unsigned char lsa,unsigned char lsb,unsigned char lsc);
void DigDisplay(void);
#endif
main.c
#include <REGX52.H>
#include "LCD1602.h"
#include "MatrixKey.h"
#include "smg.h"
unsigned char KeyNum;
void main()
{
smg_duan(1,1,1);
LCD_Init();
LCD_ShowString(1,5,"20230423");
while(1)
{
KeyNum = MatrixKey();
if(KeyNum)
{
LCD_ShowNum(2,1,KeyNum,2);
P3 = smgduan[KeyNum];
}
}
}
(3)實驗結(jié)果
3. 矩陣按鍵操作(電子密碼鎖)
(1)仿真電路圖
(2)源代碼
LCD1602.c
:與上述同名文件相同
LCD1602.h
:與上述同名文件相同
Delay.c
:與上述同名文件相同
Delay.h
:與上述同名文件相同
MatrixKey.c
:與上述同名文件相同
MatrixKey.h
:與上述同名文件相同
main.c
#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "MatrixKey.h"
unsigned char KeyNum;
unsigned int Password,Count;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"Password:");
while(1)
{
KeyNum=MatrixKey();
if(KeyNum)
{
if(KeyNum<=10) //如果S1~S10按鍵按下,輸入密碼
{
if(Count<4) //如果輸入次數(shù)小于4
{
Password*=10; //密碼左移一位
Password+=KeyNum%10; //獲取一位密碼
Count++; //計次加一
}
LCD_ShowNum(2,1,Password,4); //更新顯示
}
if(KeyNum==11) //如果S11按鍵按下,確認(rèn)
{
if(Password==2345) //如果密碼等于正確密碼
{
LCD_ShowString(1,14,"OK "); //顯示OK
LCD_ShowString(2,1,"open 202352188" );
Delay(3000);
LCD_ShowString(2,1," " );
}
else //否則
{
LCD_ShowString(1,14,"ERR"); //顯示ERR
Delay(2000);
}
LCD_ShowString(1,14," ");
Password=0; //密碼清零
Count=0; //計次清零
LCD_ShowNum(2,1,Password,4); //更新顯示
}
if(KeyNum==12) //如果S12按鍵按下,取消
{
Password=0; //密碼清零
Count=0; //計次清零
LCD_ShowNum(2,1,Password,4); //更新顯示
}
}
}
}
(3)實驗結(jié)果
到了這里,關(guān)于嵌入式51單片機(jī)04-矩陣按鍵系列的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!