要求用4×4组成0-9数字键及确认键。用8位数码管组成显示电路提示信息,当输入密码时,只显示“8.”,当密码位数输入完毕按下确认键时,对输入的密码与设定的密码进行比较,若密码正确,则门开,此处用LED发光二极管亮一秒钟做为提示,同时发出“叮咚”声;若密码不正确,禁止按键输入3秒,同时发出“嘀、嘀”报警声;若在3秒之内仍有按键按下,则禁止按键输入3秒被重新禁止。
4*4矩阵键盘检测程序(新手用),本程序用于检测4*4矩阵按键,先检测是否有按键按下,如果有按键按下,由P1口读出相应的编码值,由P0经两片74HC573输出给8位数码管,P2^0位选,P2^1段选,P1接4*4矩阵按键#include <reg52.h>#include<intrins.h> //头文件#define uchar unsigned char //宏定义#define uint unsigned int sbit du=P2^1; //段选位sbit we=P2^0; //位选位uchar code duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};void delay(xms) //延时函数{ uchar i,j; for(xms=i,i>0;i--;) for(j=11;j>0;j--); }void main() //主函数{ we=1; //位选开 P0=0; //8位数码管全部显示 we=0; //位选关 du=1; //段选开 P1=0x00; //初始化,8位数码管无显示 while(1) { uchar a,b; P1=0xf0; a=P1; if(a!=0xf0) { delay(5); //去抖动 b=P1; if(a==b) { a=0xfe; do { P1=a; if(a!=P1) { switch(P1) { case 0x7e:{P0=duanma[0];break;} //编码检测,第一个键按下显示0,第16个按键按下显示F case 0xbe:{P0=duanma[1];break;} case 0xde:{P0=duanma[2];break;} case 0xee:{P0=duanma[3];break;} case 0x7d:{P0=duanma[4];break;} case 0xbd:{P0=duanma[5];break;} case 0xdd:{P0=duanma[6];break;} case 0xed:{P0=duanma[7];break;} case 0x7b:{P0=duanma[8];break;} case 0xbb:{P0=duanma[9];break;} case 0xdb:{P0=duanma[10];break;} case 0xeb:{P0=duanma[11];break;} case 0x77:{P0=duanma[12];break;} case 0xb7:{P0=duanma[13];break;} case 0xd7:{P0=duanma[14];break;} case 0xe7:{P0=duanma[15];break;} } } a=_crol_(a,1); //循环移位去检测按键 }while(a!=0xef); } } }}