本功能中用51單片機做控制核心,實現的功能是鍵盤輸入數值,用一塊液晶顯示屏同時顯示一些提示字符,比如“welcome go home”、“the num is right”等,這個密碼鎖設計目前已經處于淘汰的邊緣,但是對于初學者來說算是一次比較復雜的設計嘗試,該設計中使用了矩陣鍵盤,液晶屏,繼電器等模塊,初始設計的密碼位數是3位,若想增加位數只需對程序中的初始密碼進行修改,同時更改主函數中的判定位數即可。最高支持16位密碼。密碼輸入正確繼電器動作,同時LED燈會閃爍3次。主要裝配的電路圖如下所示:LED和繼電器未標出,應連接P0.3和P0.4
仿真(版本8.11)
實物搭建
程序代碼:
#include <reg52.h> //方便觀看,1602未使用庫函數調用
#include <stdio.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define KeyPort P1
#define DataPort P0
sbit RS = P2^2;
sbit RW = P2^1;
sbit EN = P2^0;
sbit BEEP = P0^4;
sbit CON = P0^3;
uchar code password[]={3,3,3};初始密碼,若位數更改,主函數中對應位數也要修改
void DelayUs2x(uchar t)
{
while(--t);
}
/*------------------------------------------------
延時函數,若精確延時請使用匯編修改
------------------------------------------------*/
void DelayMs(uchar t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
LCD1602寫指令
------------------------------------------------*/
void write_com(uchar com)
{
RS=0;
RW=0;
DataPort = com;
EN=1;
DelayMs(1);
EN=0;
}
/*------------------------------------------------
LCD1602寫數據
------------------------------------------------*/
void write_data(uchar dat)
{
RS=1;
RW=0;
DataPort = dat;
EN=1;
DelayMs(1);
EN=0;
}
/*------------------------------------------------
清屏
------------------------------------------------*/
void clear(void)
{
write_com(0x01);
DelayMs(5);
}
/*------------------------------------------------
LCD1602寫字符
------------------------------------------------*/
void write_char(uchar x,uchar y,uchar Data)
{
if (y == 0)
{
write_com(0x80 + x);
}
else
{
write_com(0xC0 + x);
}
write_data(Data);
}
/*------------------------------------------------
LCD1602寫字符串
------------------------------------------------*/
void write_string(uchar x,uchar y,uchar *s)
{
while (*s)
{
write_char(x,y,*s);
s++;
x++;
}
}
/*------------------------------------------------
LCD1602初始化
------------------------------------------------*/
void LCD_Init(void)
{
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0C);
}
/*------------------------------------------------
行掃描法,返回對應鍵值
------------------------------------------------*/
uchar KeyScan(void)
{
uchar cord_h,cord_l;
KeyPort=0x0f;
cord_h=KeyPort&0x0f;
if(cord_h!=0x0f)
{
DelayMs(10);
if((KeyPort&0x0f)!=0x0f)
{
cord_h=KeyPort&0x0f;
KeyPort=cord_h|0xf0;
cord_l=KeyPort&0xf0;
while((KeyPort&0xf0)!=0xf0);
return(cord_h+cord_l);
}
}
return(0xff);
}
/*----------------------------------------------
按鍵處理函數,返回對應數值
------------------------------------------------*/
uchar KeyPro(void)
{
switch(KeyScan())
{
case 0x7e:return 0;break; //0 7E //此處對應矩陣鍵盤相對值
case 0xbe:return 1;break; //1 7D
case 0xde:return 2;break; //2 7B
case 0xee:return 3;break; //3 77
case 0x7d:return 4;break; //4 BE
case 0xbd:return 5;break; //5 BD
case 0xdd:return 6;break; //6 BB
case 0xed:return 7;break; //7 B7
case 0x7b:return 8;break; //8 DE
case 0xbb:return 9;break; //9 DD
case 0xdb:return 10;break; //a DB
case 0xeb:return 11;break; //b D7
case 0x77:return 12;break; //c EE
case 0xb7:return 13;break; //d ED
case 0xd7:return 14;break; //e EB
case 0xe7:return 15;break; //f E7
default:return 0xff;break;
}
}
void main()
{
uchar num,i,j;
uchar passwordtemp[3];
uchar inputtimes;
uchar passwordlength,PLEN;
bit Flag;
PLEN=sizeof(password)/sizeof(password[0]);
LCD_Init();
//DelayMs(10);
clear();
BEEP=0;
CON=0;
write_string(0,0," Welcome! ");
write_string(0,1,"Input password:");
while(1)
{
num=KeyPro();
if(num!=0xff)
{
if(i==0)
write_string(0,1," ");
if(i<3)//若初始密碼位數更改,此處對應的判定位數也要修改,要和前面的定義一致
{
passwordtemp[ i]=num;
write_string(0,0," Your nm is: ");
write_char(i,1,'*');
}
i++;
if(15==num) //鍵盤上的15號按鍵對應確定按鈕
{
passwordlength=i-1;
i=0;
if(passwordlength==PLEN)
{
Flag=1;
for(j=0;j<PLEN;j++)
{
Flag=Flag&&(passwordtemp[j]==password[j]);
}
}
if(Flag)
{
CON = 1;
BEEP=1;
write_string(0,0," ");
write_string(0,0," Welcome home! ");
write_string(0,1," ");
write_string(0,1,"Right Open!>>>>");
BEEP=1;DelayMs(2000);
BEEP=0;DelayMs(2000);
BEEP=1;DelayMs(2000);
BEEP=0;DelayMs(2000);
BEEP=1;DelayMs(2000);
BEEP=0;DelayMs(2000);
inputtimes=0;
Flag=0;
}
else
{
BEEP=0;
CON = 0;
write_string(0,1," ");
write_string(0,1,"Password Error! ");
inputtimes++;
if(inputtimes==3)
{
write_string(0,0," ");
write_string(0,0,"Wrong 3 times!");
write_string(0,1," ")
write_string(0,1,"Door is Locked.")
while(1);
}
}
}
}
}
}
全部資料51hei下載地址:
密碼鎖0409.rar
(121.4 KB, 下載次數: 29)
2021-7-17 14:23 上傳
點擊文件名下載附件
|