*次程序主要实现的是:密码锁仿真,当输入的密码正确时,绿灯亮,当输入的密码超过三次错误,那么就发出报警声且红灯亮,当按下取消键时,只有led在闪烁,且此时只有复位/重开机,才能够重新开始程序*/          作者:dong(王铭东)
#include<reg52.h>
#define uchar unsigned char
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0x8c};//共阳极码型
uchar Count;
int password[4]={11,11,11,11};//要输入的秘密
int password_1[4]={3,3,2,0};//原始密码
int ii,k,key_key,disnum,button=0;//定义全局变量,在每个子函数中都通用
sbit ring=P2^0; //定义一个蜂鸣器,3个led灯
sbit led_red=P2^1;
sbit led_green=P2^2;
sbit led_yellow=P2^3;
sbit led_1=P2^7;//定义4个位选端
sbit led_2=P2^6;
sbit led_3=P2^5;
sbit led_4=P2^4;
sbit key_1=P1^0;//定义8个输入按键
sbit key_2=P1^1;
sbit key_3=P1^2;
sbit key_4=P1^3;
sbit key_5=P1^4;
sbit key_6=P1^5;
sbit key_7=P1^6;
sbit key_8=P1^7;
sbit key_9=P3^0;
sbit key_0=P3^1;
sbit key_set=P3^2;//定义设定密码按键
sbit key_clr=P3^3;//定义取消按键
//添加两首歌曲(网络摘取)
unsigned char code SONG[] ={      //祝你平安
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
                                  //路边的野花不要采
0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,
};
void delay(int a)//延时函数
{
int b;
for(;a>0;a--)
for(b=100;b>0;b--);
}
void display(void)//显示子模块
{
     if(disnum==0)//四位密码一块显示自模块
   { P0=table[password[0]];
    led_1=1;delay(5);led_1=0;
    P0=table[password[1]];
    led_2=1;delay(5);led_2=0;
    P0=table[password[2]];
    led_3=1;delay(5);led_3=0;
    P0=table[password[3]];
    led_4=1;delay(5);led_4=0;
   }
   else if(k==1)//四位密码,按照1,2,3,4个来显示
      {
       P0=table[password[0]];
      led_1=1;delay(5);led_1=0;
     }
     else if(k==2)
      {
       P0=table[password[0]];
       led_1=1;delay(5);led_1=0;
       P0=table[password[1]];
       led_2=1;delay(5);led_2=0; 
      }
      else if(k==3)
       {
        P0=table[password[0]];
        led_1=1;delay(5);led_1=0;
        P0=table[password[1]];
        led_2=1;delay(5);led_2=0;
        P0=table[password[2]];
        led_3=1;delay(5);led_3=0;
       }
       else
       {
        P0=table[password[0]];
        led_1=1;delay(5);led_1=0;
        P0=table[password[1]];
        led_2=1;delay(5);led_2=0;
        P0=table[password[2]];
        led_3=1;delay(5);led_3=0;
        P0=table[password[3]];
        led_4=1;delay(5);led_4=0;
       }

}
void dis()//开机显示部分

P0=table[10];
led_1=1;
led_2=1;
led_3=1;
led_4=1;
delay(1000);
}
void scanf(void)//扫描键盘程序
{
ii=0;
k=0;
do

   led_yellow=0;
   led_red=1;ring=1;
   led_green=1;
   display();
   if(ii>3)
   {
    ii=0;
    k=0;
   }
   if(key_0==0)
   {
    delay(5);
    if(key_0==0)
    {
     password[ii]=0;
     ii++;k++;
     while(key_0==0);
    }
   }
   if(key_1==0)
   {
    delay(5);
    if(key_1==0)
    {
     password[ii]=1;
     ii++;k++;
     while(key_1==0);
    }
   }if(key_2==0)
   {
    delay(5);
    if(key_2==0)
    {
     password[ii]=2; 
     ii++;k++;
     while(key_2==0);
    }
   }if(key_3==0)
   {
    delay(5);
    if(key_3==0)
    {
     password[ii]=3;
     ii++;k++;
     while(key_3==0);
    }
   }
   if(key_4==0)
   {
    delay(5);
    if(key_4==0)
    {
     password[ii]=4;
     ii++;k++;
     while(key_4==0);
    }
   }
   if(key_5==0)
   {
    delay(5);
    if(key_5==0)
    {
     password[ii]=5;
     ii++;k++;
     while(key_5==0);
    }
   }if(key_6==0)
   {
    delay(5);
    if(key_6==0)
    {
     password[ii]=6; 
     ii++;k++;
     while(key_6==0);
    }
   }if(key_7==0)
   {
    delay(5);
    if(key_7==0)
    {
     password[ii]=7;
     ii++;k++;
     while(key_7==0);
    }
   }
   if(key_8==0)
   {
    delay(5);
    if(key_8==0)
    {
     password[ii]=8;
     ii++;k++;
     while(key_8==0);
    }
   }
   if(key_9==0)
   {
    delay(5);
    if(key_9==0)
    {
     password[ii]=9;
     ii++;k++;
     while(key_9==0);
    }
   }
   if(key_set==0)
   {
    delay(5);
    if(key_set==0)
    {
     key_key=0;
     while(key_set==0);
    }
   }
}while(key_key);disnum=0;led_yellow=1;button++;//程序的重要设定数值部分**************

}
void Time0_Init()//定时中断
{
        TMOD = 0x01;
        IE          = 0x82;
        TH0 = 0xD8;
        TL0 = 0xEF;                //12MZ晶振,10ms
}
void Time0_Int() interrupt 1
{
        TH0 = 0xD8;
        TL0 = 0xEF;
        Count++;                        //长度加1
}
/*-------------------------------------------------
功能:1MS延时子程序(网络摘取)
-------------------------------------------------*/
void Delay_xMs(unsigned int x)
{
    unsigned int i,j;
    for( i =0;i < x;i++ )
    {
        for( j =0;j<3;j++ );
    }
}
/*-------------------------------------------------
功能:歌曲播放子程序i为播放哪一段曲目(网络摘取)
-------------------------------------------------*/
void Play_Song(uchar i)
{
        unsigned char Temp1,Temp2;
        unsigned int Addr;
        Count = 0;                                                //中断计数器清0
        Addr = i * 217;
        while(1)
        { 
                Temp1 = SONG[Addr++];
            if ( Temp1 == 0xFF )          //休止符
            {
                    TR0 = 0;
                    Delay_xMs(100);
            }
            else if ( Temp1 == 0x00 )          //歌曲结束符
            {
                        Addr++;
                         i++;
                        if(i==2)
                        {
                                i=0;
                            return;
                        }
            }
            else
            {
                    Temp2 = SONG[Addr++];
                    TR0 = 1;
                          while(1)
                          {
                            ring= ~ring;
                            Delay_xMs(Temp1);
                            if ( Temp2 == Count )
                            {
                                    Count = 0;
                                    break;
                            }
                    }
            }
        }
}
void baojing(void)//报警程序,此时进入到死循环,led(红)+ 蜂鸣器,会不断的发出声响来;
{
int pp1=0,pp2=1;
while(1)
{
  
  
   P0=0xfe;led_1=1;delay(100);led_1=0;if(key_clr==0){pp1=1;}
   P0=0xfe;led_2=1;delay(100);led_2=0;led_red=pp1;ring=pp1;
   P0=0xfe;led_3=1;delay(100);led_3=0;if(key_clr==0){pp1=1;} 
   P0=0xfe;led_4=1;delay(100);led_4=0;led_red=pp2;ring=pp2;
   P0=0xfd;led_4=1;delay(100);led_4=0;if(key_clr==0){pp1=1;}
   P0=0xfb;led_4=1;delay(100);led_4=0;led_red=pp1;ring=pp1;
   P0=0xf7;led_4=1;delay(100);led_4=0;if(key_clr==0){pp1=1;}
   P0=0xf7;led_3=1;delay(100);led_3=0;led_red=pp2;ring=pp2;
   P0=0xf7;led_2=1;delay(100);led_2=0;if(key_clr==0){pp1=1;}
   P0=0xf7;led_1=1;delay(100);led_1=0;led_red=pp1;ring=pp1;
   P0=0xef;led_1=1;delay(100);led_1=0;if(key_clr==0){pp1=1;}
   P0=0xdf;led_1=1;delay(100);led_1=0;led_red=pp2;ring=pp2;
}

}
void main()
{
      dis();
   disnum=0;
   Time0_Init();                        //定时器0中断初始化
   while(1)
   {
display();//显示“”
   if(key_set==0)//输入密码检测,当检测到“key_set”为0时,进入输入密码程序
    {
    delay(5);//延时消抖动
    if(key_set==0)
     {
     key_key=1;
     disnum=1;
     while(key_set==0);
     scanf();
     }
    }
   if((password[0]==password_1[0])&&(password[1]==password_1[1])&&(password[2]==password_1[2])&&(password[3]==password_1[3]))
    {
    led_green=0;
    led_red=1;
    ring=1;
    if(key_clr==0)
    {Play_Song(0);}
  
    }
   else
    {
    led_green=1;
    led_red=0;
    ring=0;
    if(button>=3)
     {
      baojing();//如果输入的密码超过5次,那么进入死循环“发出报警,并且闪烁led”,只能重新开机/复位电路
     }
    }
  
  
  
   }
}