要求:
設計一個電子密碼鎖
- 輸入密碼即可開鎖,可刪除信息,可通過輸入123456789ABCDEF等字符
- 密碼錯誤發(fā)出警報
- 用LCD1602顯示相關信息
當輸入正確時 D2會亮燈,表示輸入正確
密碼輸入錯誤時蜂鳴器會發(fā)出警報
部分程序:
#include <reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
//sbit 特殊功能位聲明:聲明某特殊功能寄存器中的一位
#define key P1????????????????? ?? //按鍵io口
sbit fen=P3^0;????????????????? ?? //蜂鳴器io口
sbit rs=P3^1;??????? //LCD控制口
sbit rw=P3^2;
sbit e=P3^3;
sbit led=P3^4;?????? //LED燈io口
//bit 位變量聲明:定義一個位變量的值
u8 keyz=17,j=0,biao1=0,a=10,b=10,c=10,d=10;
bit biao=0;
u16 zh;
u8 shu[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
u8 zi[]={"lnput password:"};
u8 num[]={"0123456789ABCDEF"};
void delay(u16 i){
while(i--);
}
/*蜂鳴器*/
void Fen(u8 a,b)?? //蜂鳴器發(fā)聲函數(shù)
{
u8 i,j;
for(i=0;i<a;i++)
{
for(j=0;j<100;j++)
{fen=~fen;delay(b);}
}
}
/*LCD*/
void writecom(u8 com){????????????????? //LCD寫指令
rs=0;
rw=0;
e=0;
P0=com;
delay(5);
e=1;
e=0;
}
void writedat(u8 dat){???????????? //LCD寫數(shù)據(jù)
rs=1;
rw=0;
e=0;
P0=dat;
delay(5);
e=1;
e=0;
}
void initlcd(){??????????????????? ?? //LCD初始化函數(shù)
writecom(0x38);?? //顯示模式設置
writecom(0x0c);
writecom(0x06);
}
void xian(){????? //LCD顯示數(shù)字函數(shù)
writedat(num[keyz]);
if(a==10){a=keyz; }????????????? ?? //對abcd,四位數(shù)的單獨賦值
else if(b==10){b=keyz;}
else if(c==10){c=keyz;}
else{d=keyz;}
if(j!=3){j++;}biao=0;
}
/*鍵盤*/
void Key(){u8 a=0;??????????????????? //矩形式鍵盤函數(shù)
if(key!=0x0f){delay(1000);
if(key!=0x0f){
key=0x0f;
switch(key){
case 0x07: keyz=0;biao=1;break;
case 0x0b: keyz=1;biao=1;break;
case 0x0d: keyz=2;biao=1;break;
case 0x0e: keyz=3;biao=1;break;
}
key=0xf0;
switch(key){
case 0x70: keyz=keyz;biao=1;break;
case 0xb0: keyz=keyz+4;biao=1;break;
case 0xd0: keyz=keyz+8;biao=1;break;
case 0xe0: keyz=keyz+12;biao=1;break;
}
}
while((a<50)&&(key!=0xf0)){a++;delay(10000);}
}
}
void shi(){u8 i; ? // 按鍵值判斷函數(shù)
if(biao&&keyz!=15){? //判斷標志位biao是否為1,為1則有按鍵操作并且按鍵的值不等于15
writecom(0x80+0x40+j);??????????????????????? ? //數(shù)字顯示位置進行累加操作
if(j<0){j=0;}?????????????????????????????????????? ? //數(shù)字顯示位置邊界
switch(keyz){?????????????????????????????????????????????????? ? //switch判斷按鍵并執(zhí)行對應操作
case 0:xian(); break;
case 1:xian(); break;
case 2:xian(); break;
case 3:xian(); break;
case 4:xian(); break;
case 5:xian(); break;
case 6:xian(); break;
case 7:xian(); break;
case 8:xian(); break;
case 9:xian(); break;
case 10:writedat(num[keyz]);
j++;biao=0;break;
case 11:writedat(num[keyz]);
j++;biao=0;break;
case 12:
biao1=0;???????????????????????????????????????????? ? //重置標志位biao1,上鎖
biao=0;break;
case 13:writecom(0x01);??????? delay(1000);??? ? //全屏清除
for(i=0;i<sizeof(zi);i++){
writecom(0x80+i);
writedat(zi[i]);???????????????? }
j=0;biao=0;break;
case 14: ????????????????????????????????????????????????????????????????????? ??//對當前數(shù)字位刪除的操作
writedat(' ');
j--;
writecom(0x80+0x40+j);writedat(' ');
biao=0;break;
case 15:??????????????????? ? //確定按鍵
j++;biao=0;break;
}
}
if(keyz==15){?? ?????????????????????????????????????????????????????????? ?//判斷按鍵值如果等于15,便視為按下確定鍵
writecom(0x01);delay(1000);
zh=a*1000+b*100+c*10+d;??????????????????????????????????????????? //將ABCD,四位數(shù)合并并進行判斷是否為正確密碼
if(zh==1234){? //設密碼為1234
j=0;
biao1=1;?????????????????????????????????????????????????????????????????????? //將biao1標志位置1便為密碼正確
keyz=17;?????????????????? ? ??????????????????????????????????????????????? ?//使按鍵值跳出最大值15便不會循壞顯示
}
else{
j=0;
biao1=0;???????????????????????????????????????????????????????????????????????????????????? //反之密碼錯誤
keyz=17;
Fen(20,200); ?????????????????? ???????????//蜂鳴器報警
}
zh=0;a=10,b=10,c=10,d=10;???????????????????????????????????? //重置ABCD變量
for(i=0;i<sizeof(zi);i++){
writecom(0x80+i);
writedat(zi[i]);????????????????????? ?? }
}
}
內容包括:
需要完整的資料可在我的資源里下載,也可以加入我的紛傳圈子,里面有資源壓縮包的百度網(wǎng)盤下載地址及提取碼。
紛傳點擊用微信打開即可,過程有點繁瑣請見諒。