/*本项目按键及显示说明： b为开始键 a为确定键 c为修改键  P.显示为可以输入密码 open显示为输入密码正确 E显示为可输入修改密码 ok显示修改密码成功  lock显示为输入密码错误**/     
#include <reg52.h>  
#define uchar unsigned char 
#define uint unsigned int  
sbit P3_1=P3^1;//密码错误报警状态为// 
sbit P3_0=P3^0;//锁闭状态位// 
sbit P3_2=P3^2;//开锁状态位//  
sbit P3_3=P3^3;//修改密码状态位// 
uchar v;  
uchar dis_code[]={0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58};  //初始密码88888888 
uchar code mm[]={0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59};//0-9 ////////************* 显示代码数组 ***********//////// 
uchar code table1[]={0xC0,0xF9,0xA4,0xB0,0x99 ,0x92,0x82,0xF8,0x80,0x90,0x0c,0xff,0xa1,0x86 ,0xc6,0xbf,0x8e,0x8d,0xc8,0x8c,0x84,0x8f,0xc7,0xce,0x88};  
//共阳代码表格,分别对应：0,1,2,3,4,5,6,7,8,9,P.,灭,d,E,c,-,F,n,Y,P,e,K,L//  
uchar led[8]={2,0,9,0,3,15,2,2};   
 uchar ledp[8]={10,11,11,11,11,11,11,11} ; //p点显示 
uchar ledopen[8]={0,19,20,18,11,11,11,11};  //open显示 
uchar ledlock[8]={22,0,14,21,11,11,11,11};  //lock显示 
uchar ledok[8]={0,21,11,11,11,11,11,11};  //ok显示  
uchar ledenter[8]={20,18,23,20,24,11,11,11};//enter显示 
uchar table[8]; 
uchar n=0,i,x; //延时子程序  
void delay(uint ms)    
{uchar z;  
while(ms--)  
{    for(z=0;z<120;z++);  }   }  /////******延时程序 *******/////  
void dey ()       
{ uchar i;      
for(i=121;i!=0;i-- )    
 {}    }  
void display()         
{
uchar  p , q ,m=7,c,wei=0xfe,ws=8;  //显示程序 //       
while(ws)       
{
c=led[m];      //显示学号//     
ws--;     
 P2= wei ;     
P0=table1[c];     
dey ();         
p=wei<<1;    
 q=wei>>7;        
 wei=p|q;        
 m--;     }         }  //******键处理*****//
uchar keychuli() {   
P1=0xf0;       //发全列0扫描码 
 P1=P1&0xf0;   //若有键按下  
return (P1);  }  //*****键扫子程序****/// 
uchar key() 
{  
uchar scan,tmp,chizhi; //列，行 
chizhi=keychuli(); 
if(chizhi!=0xf0)     
{
delay(5);          // 延时去抖     
chizhi=keychuli();   //延时再判键是否还按下  
if(chizhi!=0xf0)       
{scan=0xfe;         
while((scan&0x10)!=0)  //逐列扫描
{           
P1=scan;               //输出列扫描码          
if((P1&0xf0)!=0xf0)      //本列有键按下             
{tmp=(P1&0xf0)|0x0f;               
return ((~scan)|(~tmp));}   //还回键值           
else scan=(scan<<1)|0x01;             //列扫描码左移一位         
 }       }      }                
 return (0); }                               //无键按下,还回0 }  
 void delay1(uint m) 
 {   while(m--);  }  //****输入密码函数******//
 void srmm() 
{ 
while(1)    
{    
v=key();   if(v==0x11||v==0x12||v==0x14||v==0x21||v==0x22||v==0x24||v==0x41||v==0x42||v==0x81 ||v==0x82)   
{     
delay(200);    
switch(v)     
{      
case 0x11:table[n]=mm[0];break;//显示一杠     
case 0x21:table[n]=mm[1];break;     
case 0x41:table[n]=mm[2];break;     
case 0x81:table[n]=mm[3];break;     
case 0x12:table[n]=mm[4];break;     
case 0x22:table[n]=mm[5];break;     
case 0x42:table[n]=mm[6];break;     
case 0x82:table[n]=mm[7];break;     
case 0x14:table[n]=mm[8];break;     
case 0x24:table[n]=mm[9];break;     
default:break;     }      
x=led[n]=15;display();delay(100);    
 n++;         
if(n==8) 
{n=0;break;}   }   } }//********密码处理函数****// 
void mmchuli()  
{
if(table[0]==dis_code[0]&&table[1]==dis_code[1]&&table[2]==dis_code[2]&&   table[3]==dis_code[3]&&table[4]==dis_code[4]&&table[5]==dis_code[5]&&   table[6]==dis_code[6]&&table[7]==dis_code[7])     
{
for(i=0;i<8;i++)     
led[i]=ledopen[i];     
P3_2=0;    
 P3_0=1;  
xianshi:display();//显示open，代表密码输入正确  
v=key();      
if( v!=0x18)      
goto xianshi; //修改密码键c  
led[0]=13;  
P3_3=0;   
display();//显示e      
delay(500);       
while(1)      
{      
v=key();  if(v==0x11||v==0x12||v==0x14||v==0x21||v==0x22||v==0x24||v==0x41||v==0x42||v==0x81 ||v==0x82)   
{     
delay(200);    
switch(v)     
{      
case 0x11:table[n]=mm[0];dis_code[n]=table[n];break;//显示一杠     
case 0x21:table[n]=mm[1];dis_code[n]=table[n];break;     
case 0x41:table[n]=mm[2];dis_code[n]=table[n];break;     
case 0x81:table[n]=mm[3];dis_code[n]=table[n];break;     
case 0x12:table[n]=mm[4];dis_code[n]=table[n];break;     
case 0x22:table[n]=mm[5];dis_code[n]=table[n];break;     
case 0x42:table[n]=mm[6];dis_code[n]=table[n];break;     
case 0x82:table[n]=mm[7];dis_code[n]=table[n];break;     
case 0x14:table[n]=mm[8];dis_code[n]=table[n];break;     
case 0x24:table[n]=mm[9];dis_code[n]=table[n];break;   }      
led[n]=15;display(); delay(100);     
n++;  
if(n==8)      
{      
n=0;break;} }     }     
do{v=key();}        
while(v!=0x44);//确定    
for(i=0;i<8;i++)       
led[i]=ledp[i];    
for(i=0;i<8;i++)    
led[i]=ledok[i];    
P3_3=1;    
P3_2=1;        
display();//修改成功，显示ok    
delay(120);       }     
else     
{ P3_1=0;      
delay(1000);     
P3_1=1;      
for(i=0;i<8;i++)        
led[i]=ledlock[i];    
display();//显示lock，代表输入错误   
delay(150);    }   }  //*************监控程序*********// 
void main()  
{    
while(1)   
{    
xuehao: display();    
v=key();     
if( v!=0x84)//等待按开始键，B为开始，键码为84H    
goto xuehao;    
for(i=0;i<8;i++)    
led[i]=ledp[i];    
P3_0=0;     
display( );//显示P点    
srmm();     
do{v=key();}         
while(v!=0x44);  //等待确定 ，A为确定键码为44H    
mmchuli();   }   }