找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3105|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

單片機基本密碼鎖

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:111751 發(fā)表于 2016-4-1 12:05 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
根據(jù)設(shè)定好的密碼, 采用六個按鍵實現(xiàn)密碼的輸入功能,當(dāng)密碼輸入完后,按確認(rèn)鍵驗證,正確之后,鎖就打開, 如果輸入的三次的密碼不正確, 就鎖定按鍵 3 秒鐘, 同時發(fā)出報警聲,直到?jīng)]有按鍵按下 3 秒后, 才打開按鍵鎖定功能; 否則在 3 秒鐘內(nèi)仍有按鍵按下,就重新鎖定按鍵 3 秒時間并報警。若按鍵過程中發(fā)現(xiàn)按錯了,可通過取消鍵取消,然后重新輸入�?梢酝ㄟ^一個繼電器控制一個燈的亮滅來模擬鎖的打開。報警要聲光報警。按密碼次數(shù)需要一個數(shù)碼管顯示出來。報警聲自行設(shè)定。

/*C語言代碼*/
#include<reg51.h>
#include<intrins.h>
#include<string.h>
#define uint unsigned int
#define uchar unsigned char
sbit Beep=P2^0;
sbit Red=P2^1;
sbit Green=P2^2;
sbit Lamp=P2^3;
uchar Wrong=0;          //錯誤次數(shù)
uchar Count=0;   //開鎖次數(shù)
uchar Timer=0;   //中斷計數(shù)器
uchar k=0;                 //數(shù)組累加        
uchar Sure_temp,Cancel_temp;//保存按下確認(rèn)鍵和取消鍵P1的值
//數(shù)碼管段碼0~9
uchar code Seg_Play[]={0x3f,0x06,0X5B,0X4F,
                                              0X66,0X6D,0X7D,0X07,
                                           0X7F,0X6F,0X77,0X7C,
                                           0X39,0X5E,0X79,0X71};
//鍵盤特征碼
uchar code Key_Table[]={0xee,0xde,0xbe,0x7e,
                                                  0xed,0xdd,0xbd,0x7d,
                                                  0xeb,0xdb,0xbb,0x7b,
                                                  0xe7,0xd7,0xb7,0x77};        
uchar Buffer[]={0,0,0,0,0,0};        //按鍵數(shù)值緩存數(shù)組
uchar code Cipher[]={2,0,1,6,5,7}; //密碼初值數(shù)組 (比賽日期)
//延時
void Delay(uint x)
{
        uchar i;
        while(x--)for(i=0;i<120;i++);
}
//聲光報警
void Alarm()
{
        uchar i;
        Red=0;
        for(i=0;i<120;i++)
        {
                Beep=~Beep;
                Delay(2);               
        }
}

//確認(rèn)鍵處理 按鍵檢測
void Sure_Key()
{        
        uchar flag;
        uchar i;
        P0=0XFF;
        P0=Seg_Play[++Count];                  //顯示輸入次數(shù)
        Delay(200);
        if(Count>=10){P0=0XC0;Count=0;}
        for(i=0;i<6;i++)
        {
                if(Buffer[ i]==Cipher[ i]){flag=1;continue;}
                else {flag=0;break;}
        }
        //return flag;
        if(flag==1){k=0;Green=0;Lamp=0;Red=1;Delay(200);}                 //正確綠燈亮,鎖打開
        else {k=0;Alarm();Wrong++;Green=1;Lamp=1;Delay(200);}         //錯誤則聲光報警
        if(Wrong==3) {Sure_temp=P1;TR0=1;Wrong=0;}                                                          //錯誤三次則鎖定鍵盤3秒鐘
}
//取消鍵處理
void Cancel_Key()
{        
        uchar i;
        k=0;
        Buffer[0]=0;
        Buffer[1]=0;
        Buffer[2]=0;
        Buffer[3]=0;
        Buffer[4]=0;
        Buffer[5]=0;
        Delay(20);
}
//輸入密碼并存入緩存
void Key_Play()
{        
        uchar i=0;
        uchar num;
        uchar temp;
        uchar Key,c;
        P1=0xf0;
        if(P1!=0xf0)
        {
                Delay(5);
                if(P1!=0xf0)
                {
                        temp=P1;
                        P1=0x0f;
                        Key=temp|P1;
                        for(i=0;i<12;i++)
                        if(Key==Key_Table) {num=i;break;}
                        Buffer[k++]=num;
                        if(num==10)Sure_Key();
                        if(num==11)Cancel_Key();
                }
                do{                           //等待按鍵抬起
                          temp=P1;
                         temp=temp&0x0f;
                 }         
                  while(temp!=0x0f);
        }
}        
//主函數(shù)
void main()
{        
        

        P0=0x3f;//次數(shù)默認(rèn)為0
        P2=0XFF;//初始全關(guān)
        TMOD=0X01;               
        TH0=(65536-50000)/256;               
        TL0=(65536-50000)%256;
        EA=1;
        ET0=1;                                                 ////密碼錯誤定時器中斷0
        while(1)
        {        
                Key_Play();
                Delay(2);        
        }
}

//定時器0中斷 鍵盤鎖定3秒
void Inter_Timer0() interrupt 1
{
                uchar temp;
                Timer++;
                TH0=(65536-50000)/256;               
                TL0=(65536-50000)%256;
                Beep=~Beep;Delay(20);
                P1=0xf0;
                if(P1!=0xf0)
                {
                        Delay(5);
                        Timer=0;
                }
                 //如果3秒內(nèi)無按鍵則回復(fù)正常
                if(Timer==60){Timer=0;Beep=0;Red=1;TR0=0;}
}

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:121009 發(fā)表于 2016-5-15 18:35 | 只看該作者
你好 ,這個程序有沒有C51的匯編語言版本?
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表