單片機(jī)控制的密碼鎖設(shè)計(jì)(含電路圖及源程序)
key_val: 返回按鍵的值 255-表示無(wú)按鍵按下
key_index: 當(dāng)前按鍵是哪一位密碼
T1_cnt: 定時(shí)器計(jì)數(shù)溢出數(shù)
cnt_val_15s: 報(bào)警計(jì)時(shí)的數(shù)值
cnt_val_5s: 待機(jī)時(shí)間計(jì)時(shí)
cnt_val_4s: 輸入正確,等待4秒清除開(kāi)鎖信號(hào)
cnt_state: 計(jì)時(shí)狀態(tài)
error_num: 錯(cuò)誤次數(shù)
led_seg_code:數(shù)碼管7段碼
*/
#include reg51.h
/*說(shuō)明key0=P1^0; key1=P1^1;key2=P1^2; key3=P1^3;key4=P1^4;key5=P1^5;enter=P1^6;esc=P1^7;*/
sbit relay_open=P3^0; //電磁鎖開(kāi)鎖驅(qū)動(dòng)
sbit pw_error=P3^1; //密碼錯(cuò)誤信號(hào)
sbit alarm_out=P3^2; //報(bào)警輸出
sbit open_lock=P3^3; //已開(kāi)鎖指示信號(hào)
sbit audio_out=P3^4; //有源蜂鳴器
unsigned char data cnt_val_15s,cnt_val_5s,cnt_val_4s,cnt_state;
unsigned int data T1_cnt;
unsigned char data key_val,key_index,key_val_old;
unsigned char data state_val,error_num;
unsigned char data show_val[6];
char code init_val[6]={1,2,3,4,5,0};
char code led_seg_code[11]={0x3f,0x06,0x05b,0x04f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
//led_seg_code[0-9]代表0-9 led_seg_code[10]=0x00數(shù)碼管不顯示任何內(nèi)容
//--------延時(shí)程序----------------
void delay(unsigned int i)//延時(shí)
{ while(--i); }
//--------清除輸入內(nèi)容----------
void init_variant()
{unsigned char i;
for(i=0;i6;i++)
show_val[i]=10; //led_seg_code[10]=0x00表示數(shù)碼管不顯示任何內(nèi)容
key_index=0; //沒(méi)有任何輸入或清除所有輸入時(shí),保存當(dāng)前鍵的位置
}
//---------按鍵掃描---------------
unsigned char scan_key()
{ unsigned char i,k;
i=P1;
if (i==0xff cnt_state!=2)
{ k=255; } //無(wú)鍵按下
else //有鍵按下
{ delay(500); //延時(shí)去抖動(dòng)
if(i!=P1)
{k=255;}
else
{ TR1=1; //有鍵按下則開(kāi)定時(shí)器,啟動(dòng)待機(jī)計(jì)時(shí)
cnt_val_5s=0;
switch (i)
{ case 0xfe: k=0; break;
case 0xfd: k=1; break;
case 0xfb: k=2; break;
case 0xf7: k=3; break;
case 0xef: k=4; break;
case 0xdf: k=5; break;
case 0xbf: k=6; break;
case 0x7f: k=7; break;
}
}
}
return k;
}
//---------數(shù)碼管顯示---------------
void led_show()
{P0=led_seg_code[show_val[0]];
P2=0xdf;
delay(500);
P0=led_seg_code[show_val[1]];
P2=0xef;
delay(500);
P0=led_seg_code[show_val[2]];
P2=0xf7;
delay(500);
P0=led_seg_code[show_val[3]];
P2=0xfb;
delay(500);
P0=led_seg_code[show_val[4]];
P2=0xfd;
delay(500);
P0=led_seg_code[show_val[5]];
P2=0xfe;
delay(500);
}
//--------定時(shí)器T1中斷服務(wù)程序-----------------
void timer1() interrupt 3 //T1中斷
{ T1_cnt++;
if(T1_cnt>3999) //如果計(jì)數(shù)>3999, 計(jì)時(shí)1s
{ T1_cnt=0;
switch (cnt_state)
{ case 0: //待機(jī),需要計(jì)時(shí)5s
if(cnt_val_5s5)
{ cnt_val_5s++;}
else
{ cnt_val_5s=0;
init_variant();//待機(jī)計(jì)時(shí)到5秒時(shí),清除輸入的內(nèi)容
TR1=0; //停止計(jì)時(shí)
}
break;
case 1://密碼輸入正確,需要計(jì)時(shí)4s
if(cnt_val_4s4)
{ cnt_val_4s++;}
else
{ cnt_val_4s=0;
init_variant();//密碼輸入正確,計(jì)時(shí)到4秒時(shí),清除輸入的內(nèi)容
open_lock=1; //已開(kāi)鎖信號(hào)清零
relay_open=1; //開(kāi)鎖信號(hào)清零
cnt_state=0;
TR1=0; //停止計(jì)時(shí)
}
break;
case 2: //密碼輸入錯(cuò)誤3次,計(jì)時(shí)15s
if(cnt_val_15s15)
{ cnt_val_15s++;}
else
{ cnt_val_15s=0;
init_variant();//三次密碼錯(cuò)誤時(shí),計(jì)時(shí)15秒,清除輸入的內(nèi)容
open_lock=1; // 清除所有指示和報(bào)警
relay_open=1;
alarm_out=1;
pw_error=1;
cnt_state=0;
TR1=0; //停止計(jì)時(shí)
}
break;
}
}
}
//--------判斷鍵盤(pán)輸入內(nèi)容與密碼是否一致------
unsigned char check_input_pw()
{ unsigned char i,k;
k=1;
for(i=0;i6;i++)
{ k=k (show_val[i]==init_val[i]); }
return k;
}
//---------主程序----------------
main()
{ //初始化各變量
audio_out=1;
P3=0xff;
cnt_val_15s=0;
cnt_val_5s=0;
cnt_val_4s=0;
cnt_state=0;
//0-待機(jī)計(jì)時(shí)5s狀態(tài);1-密碼正確,計(jì)時(shí)4s狀態(tài) ;2-三次密碼錯(cuò)誤,處于計(jì)時(shí)15秒狀態(tài)。
T1_cnt=0;
error_num=0;
key_val_old=255;
init_variant();
電路圖符號(hào)相關(guān)文章:電路圖符號(hào)大全
評(píng)論