2 矩阵键盘

就是把一堆按钮并联集中,以扫描的形式读取数据,以达到节省I/O口的目的
![[Pasted image 20250117160829.png]]

1. 读取按键编号至LCD

通过不断对I/O口读取电平值来进行扫描以实现识别按键的功能

#include <REGX52.H>
#include "DELAY.h"
unsigned int keyNumber;

unsigned int martixKey()
{
    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;
}
#include "LCD1602.h"
#include "martix.h"

void main()
{
    LCD_Init();
    LCD_ShowString(1,1,"MartixKey");
    while (1)
    {
        unsigned char reKeyNumber = martixKey();
        if (reKeyNumber != 0)
        {
            /* code */
            LCD_ShowNum(2,1,reKeyNumber,3);
        }
        

    }
    
}

要注意变量数据类型的选择

2.矩阵键盘密码锁

在原先的基础上加入数值判断

#include "LCD1602.h"
#include "martix.h"

unsigned int reKeyNumber;
unsigned int count;
unsigned int password;

void main()
{
    LCD_Init();
    LCD_ShowString(1,1,"Password?");
    while (1)
    {
        reKeyNumber = martixKey();
        if (reKeyNumber != 0)
        {
            if(reKeyNumber <= 10)
            {
                if (count <= 4)
                {
                    password *= 10;
                    password += reKeyNumber % 10;
                    count++;
                }
                LCD_ShowNum(2,1,password,4);
            }
            if (reKeyNumber == 11)
            {
                if (password == 9876)
                {
                    LCD_ShowString(1,12,"OK!");
                    password = 0;
                    count = 0;
                    LCD_ShowNum(2,1,password,4);
                }
                else
                {
                    LCD_ShowString(1,12,"ERR");
                }
            }

            if (reKeyNumber == 12)
            {
                password = 0;
                count = 0;
                LCD_ShowNum(2,1,password,4);
            }
                
        }
        

    }
    
}
posted @ 2025-01-17 17:20  Lain_surime  阅读(34)  评论(0)    收藏  举报