stc89c52做的密码锁

  我们宿舍经常忘带钥匙,所以自己用stc89c52弄个密码锁(其实用个stc12c2052那种20个脚的就可以了~),其实还可以加上修改密码的功能,再有可能的话加上那种非接触式ic卡,关于这个ic卡这个自己还不知怎么弄。

非常简单,就是在键盘扫描基础上加几个代码。

[sourcecode language="c"]

#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar linshi,key;
sbit bibi=P3^4;
uchar code table[]={
 
                0x3F,  //"0"
                0x06,  //"1"
                0x5B,  //"2"
                0x4F,  //"3"
                0x66,  //"4"
                0x6D,  //"5"
                0x7D,  //"6"
                0x07,  //"7"
                0x7F,  //"8"
                0x6F,  //"9"
 
                         };

void yanshi(uint x)
 {
  uchar j;
  uint i;
  for(i=x;i>0;i--)
   for(j=110;j>0;j--);
 }
void xianshi(uchar a)
 {
   P1=0xff;
  P2=table[a];
 }
void didi(uint a)
 {
  bibi=0;
  yanshi(a);
  bibi=1;
  yanshi(200);
 }

void scan()
 {
   uchar temp;
   P0=0xfe;
   temp=P0;
   temp=temp&0xf0;
   if(temp!=0xf0)
    {
    yanshi(10);
    temp=P0;
    temp=temp&0xf0;
    if(temp!=0xf0)
     {
      linshi=linshi+1;
       temp=P0;
      switch(temp)
       {
         case 0xee:
          key=0;
         break;
         case 0xde:
          key=1;
         break;
         case 0xbe:
          key=2;
         break;
         case 0x7e:
          key=3;
         break;
       }
      while(temp!=0xf0)
       {
         temp=P0;
        temp=temp&0xf0;
       }
      didi(50);
              
     }
   }
   P0=0xfd;
   temp=P0;
   temp=temp&0xf0;
   if(temp!=0xf0)
    {
    yanshi(10);
    temp=P0;
    temp=temp&0xf0;
    if(temp!=0xf0)
     {
      linshi=linshi+1;
       temp=P0;
      switch(temp)
       {
         case 0xed:
          key=4;
         break;
         case 0xdd:
          key=5;
         break;
         case 0xbd:
          key=6;
         break;
         case 0x7d:
          key=7;
         break;
       }
      while(temp!=0xf0)
       {
         temp=P0;
        temp=temp&0xf0;
       }
      didi(50);
              
     }
   }
   P0=0xfb;
   temp=P0;
   temp=temp&0xf0;
   if(temp!=0xf0)
    {
    yanshi(10);
    temp=P0;
    temp=temp&0xf0;
    if(temp!=0xf0)
     {
      linshi=linshi+1;
       temp=P0;
      switch(temp)
       {
         case 0xeb:
          key=8;
         break;
         case 0xdb:
          key=9;
         break;
         case 0xbb:
          key=0;
         break;
         case 0x7b:
          key=1;
         break;
       }
      while(temp!=0xf0)
       {
         temp=P0;
        temp=temp&0xf0;
       }
      didi(50);
              
     }
   }
   

 }

void main()
 {
  uchar pd[]={1,5,8,7,5,5};
  uchar haha[6];
  linshi=0;
  while(1)
   {
   
   scan();
   if(linshi==0)
    scan();
   if(linshi==1)
    {
     haha[0]=key;
     xianshi(haha[0]);
    }
   if(linshi==2)
    {
     haha[1]=key;
     xianshi(haha[1]);
    }
   if(linshi==3)
    {
     haha[2]=key;
     xianshi(haha[2]);
    }
   if(linshi==4)
    {
     haha[3]=key;
     xianshi(haha[3]);
    }
   if(linshi==5)
    {
     haha[4]=key;
     xianshi(haha[4]);
    }    
   if(linshi==6)
    {
     haha[5]=key;
     xianshi(haha[5]);
     if(haha[5]==pd[5]&&haha[4]==pd[4]&&haha[3]==pd[3]&&haha[2]==pd[2]&&haha[1]==pd[1]&&haha[0]==pd[0])
       didi(700);
     else
      {
       didi(700);
       didi(700);
      }
     linshi=0;
    }
       }
 }

[/sourcecode]