機電之家資源網(wǎng)
單片機首頁|單片機基礎(chǔ)|單片機應(yīng)用|單片機開發(fā)|單片機文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
4×4鍵盤及8位數(shù)碼管顯示構(gòu)成的電子密碼鎖
4×4鍵盤及8位數(shù)碼管顯示構(gòu)成的電子密碼鎖
 更新時間:2010-1-29 16:35:22  點擊數(shù):0
【字體: 字體顏色
1.實驗任務(wù)
  用4×4組成0-9數(shù)字鍵及確認(rèn)鍵。
  用8位數(shù)碼管組成顯示電路提示信息,當(dāng)輸入密碼時,只顯示“8.”,當(dāng)密碼位數(shù)輸入完畢按下確認(rèn)鍵時,對輸入的密碼與設(shè)定的密碼進行比較,若密碼正確,則門開,此處用LED發(fā)光二極管亮一秒鐘做為提示,同時發(fā)出“叮咚”聲;若密碼不正確,禁止按鍵輸入3秒,同時發(fā)出“嘀、嘀”報警聲;若在3秒之內(nèi)仍有按鍵按下,則禁止按鍵輸入3秒被重新禁止。
2.電路原理圖

                  圖4.33.1
3.系統(tǒng)板上硬件連線
(1).把“單片機系統(tǒng)”區(qū)域中的P0.0-P0.7用8芯排線連接到“動態(tài)數(shù)碼顯示”區(qū)域中的ABCDEFGH端子上。
(2).把“單片機系統(tǒng)“區(qū)域中的P2.0-P2.7用8芯排線連接到“動態(tài)數(shù)碼顯示”區(qū)域中的S1S2S3S4S5S6S7S8端子上。
(3).把“單片機系統(tǒng)”區(qū)域中的P3.0-P3.7用8芯排線連接到“4×4行列式鍵盤”區(qū)域中的R1R2R3R4C1C2C3C4端子上。
(4).把“單片機系統(tǒng)”區(qū)域中的P1.0用導(dǎo)線連接到“八路發(fā)光二極管模塊”區(qū)域中的L2端子上。
(5).把“單片機系統(tǒng)”區(qū)域中的P1.7用導(dǎo)線連接到“音頻放大模塊”區(qū)域中的SPK IN端子上。
(6).把“音頻放大模塊”區(qū)域中的SPK OUT接到喇叭上。
4.程序設(shè)計內(nèi)容
(1).4×4行列式鍵盤識別技術(shù):有關(guān)這方面內(nèi)容前面已經(jīng)討論過,這里不再重復(fù)。
(2).8位數(shù)碼顯示,初始化時,顯示“P   ”,接著輸入最大6位數(shù)的密碼,當(dāng)密碼輸入完后,按下確認(rèn)鍵,進行密碼比較,然后給出相應(yīng)的信息。在輸入密碼過程中,顯示器只顯示“8.”。當(dāng)數(shù)字輸入超過6個時,給出報警信息。在密碼輸入過程中,若輸入錯誤,可以利用“DEL”鍵刪除剛才輸入的錯誤的數(shù)字。
(3).4×4行列式鍵盤的按鍵功能分布圖如圖4.33.2所示:

         圖4.33.2
#include "at89x51"
unsigned char ps[]={1,2,3,4,5};
unsigned char code dispbit[] ={0xfe,0xfd,0xfb,0xf7,
                               0xef,0xdf,0xbf,0x7f};
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,
                               0x6d,0x7d,0x07,0x7f,0x6f,
                               0x77,0x7c,0x39,0x5e,0x79,0x71,
                               0x00,0x40,0x73,0xff};
unsigned char dispbuf[8]={18,16,16,16,16,16,16,16};
unsigned char dispcount;
unsigned char flashcount;
unsigned char temp;
unsigned char key;
unsigned char keycount;
unsigned char pslen=5;
unsigned char getps[6];
bit keyoverflag;
bit errorflag;
bit rightflag;
unsigned int second3;
unsigned int aa,bb;
unsigned int cc;
bit okflag;
bit alarmflag;
bit hibitflag;
unsigned char oka,okb;
void main(void)
{
  unsigned char i,j;
  TMOD=0x01;
  TH0=(65536-500)/256;
  TL0=(65536-500)%6;
  TR0=1;
  ET0=1;
  EA=1;
  while(1)
    {
      P3=0xff;
      P3_4=0;
      temp=P3;
      temp=temp & 0x0f;
      if (temp!=0x0f)
        {
          for(i=10;i>0;i--)
          for(j=248;j>0;j--);
          temp=P3;
          temp=temp & 0x0f;
          if (temp!=0x0f)
            {
              temp=P3;
              temp=temp & 0x0f;             
              switch(temp)
                {
                  case 0x0e:
                    key=7;
                    break;
                  case 0x0d:
                    key=8;
                    break;
                  case 0x0b:
                    key=9;
                    break;
                  case 0x07:
                    key=10;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                        {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }
        }
      P3=0xff;
      P3_5=0;
      temp=P3;
      temp=temp & 0x0f;
      if (temp!=0x0f)
        {
          for(i=10;i>0;i--)
          for(j=248;j>0;j--);
          temp=P3;
          temp=temp & 0x0f;
          if (temp!=0x0f)
            {
              temp=P3;
              temp=temp & 0x0f;             
              switch(temp)
                {
                  case 0x0e:
                    key=4;
                    break;
                  case 0x0d:
                    key=5;
                    break;
                  case 0x0b:
                    key=6;
                    break;
                  case 0x07:
                    key=11;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                        {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a4;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a4:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }
        }
 
      P3=0xff;
      P3_6=0;
      temp=P3;
      temp=temp & 0x0f;
      if (temp!=0x0f)
        {
          for(i=10;i>0;i--)
          for(j=248;j>0;j--);
          temp=P3;
          temp=temp & 0x0f;
          if (temp!=0x0f)
            {
              temp=P3;
              temp=temp & 0x0f;             
              switch(temp)
                {
                  case 0x0e:
                    key=1;
                    break;
                  case 0x0d:
                    key=2;
                    break;
                  case 0x0b:
                    key=3;
                    break;
                  case 0x07:
                    key=12;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                        {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a3;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a3:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }
        }
      P3=0xff;
      P3_7=0;
      temp=P3;
      temp=temp & 0x0f;
      if (temp!=0x0f)
        {
          for(i=10;i>0;i--)
          for(j=248;j>0;j--);
          temp=P3;
          temp=temp & 0x0f;
          if (temp!=0x0f)
            {
              temp=P3;
              temp=temp & 0x0f;             
              switch(temp)
                {
                  case 0x0e:
                    key=0;
                    break;
                  case 0x0d:
                    key=13;
                    break;
                  case 0x0b:
                    key=14;
                    break;
                  case 0x07:
                    key=15;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                     , ;   {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a2;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a2:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }                         
        }
 
    }
}
void t0(void) interrupt 1 using 0
{
  TH0=(65536-500)/256;
  TL0=(65536-500)%6;
  flashcount++;
  if(flashcount==8)
    {
      flashcount=0;
      P0=dispcode[dispbuf[dispcount]];
      P2=dispbit[dispcount];
      dispcount++;
      if(dispcount==8)
        {
          dispcount=0;
        }
    }
  if((errorflag==1) && (rightflag==0))
    {
      bb++;
      if(bb==800)
        {
          bb=0;
          alarmflag=~alarmflag;
        }
      if(alarmflag==1)//sound alarm signal
        {
          P1_7=~P1_7;
        }
      aa++;
      if(aa==800)//light alarm signal
        {
          aa=0;
          P1_0=~P1_0;
        }
      second3++;
      if(second3==6400)
        {
          second3=0;
          errorflag=0;
          rightflag=0;
          alarmflag=0;
          bb=0;
          aa=0;
        }     
    }
    else if((errorflag==0) && (rightflag==1))
      {
        P1_0=0;
        cc++;
        if(cc<1000)
          {
            okflag=1;
          }
          else if(cc<2000)
            {
              okflag=0;
            }
            else
              {
                errorflag=0;
                rightflag=0;
                P1_7=1;
                cc=0;
                oka=0;
                okb=0;
                okflag=0;
                P1_0=1;
              }
        if(okflag==1)
          {
            oka++;
            if(oka==2)
              {
                oka=0;
                P1_7=~P1_7;
              }
          }
          else
            {
              okb++;
              if(okb==3)
                {
                  okb=0;
                  P1_7=~P1_7;
                }
            }
      }
  if(keyoverflag==1)
    {
      P1_7=~P1_7;
    }
}
  • 上一篇: 128x64液晶KS0108控制器驅(qū)動程序
  • 下一篇: 沒有了
  • 發(fā)表評論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機電之家 | 會員助手 | 免費鏈接

    點擊這里給我發(fā)消息66821730(技術(shù)支持)點擊這里給我發(fā)消息66821730(廣告投放) 點擊這里給我發(fā)消息41031197(編輯) 點擊這里給我發(fā)消息58733127(審核)
    本站提供的機電設(shè)備,機電供求等信息由機電企業(yè)自行提供,該企業(yè)負責(zé)信息內(nèi)容的真實性、準(zhǔn)確性和合法性。
    機電之家對此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請聯(lián)系機電之家,機電之家將及時作出處理。
    Copyright 2007 機電之家 Inc All Rights Reserved.機電之家-由機電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機電一體化學(xué)會
    中國行業(yè)電子商務(wù)100強網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1